Searching for key words within an article body

Jo_Dunne
Tera Contributor

Hi again! 

I have another query that I am hoping someone can help with! I am wondering the best way to identify articles that have key words within the main body of the text (not added as meta and not featuring within the short description). The reason being that we need to amend the names of some teams and software that are referenced within our articles - but not necessarily what the article is actually about. 

 

I know we could just add the terms in to the global search, but this will return lots of articles which we would then need to manually review - so wondering if anyone has a clever suggestions to make this task a little easier? 

 

Thanks in advance! 

Jo

4 REPLIES 4

Mark Manders
Mega Patron

I didn't try it, but something like this might work:

 

// Define the old and new text
var oldText = 'SecurityGroup';
var newText = 'ITSecurityGroup';

// Create a GlideRecord object to query the Knowledge table
var gr = new GlideRecord('kb_knowledge');
gr.addQuery('workflow_state', 'published'); // Modify this query if you want to target specific articles
gr.query();

while (gr.next()) {
    // Check if the article body contains the old text
    if (gr.text.indexOf(oldText) != -1) {
        // Replace old text with new text
        var updatedBody = gr.text.replace(new RegExp(oldText, 'g'), newText);
        
        // Update the article body
        gr.text = updatedBody;
        gr.update();
        
        // Log the update
        gs.info('Updated KB Article: ' + gr.number + ' - Replaced text.');
    }
}

gs.info('Search and replace operation completed.');

 

Please test this before using it and make sure your strings are unique, so the aren't replacing common words or something like that. 

As I said: I didn't try it myself, so it could be that the 'checkout/publish' functionality gets in the way and prevents you from updating articles even through script.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Thanks - will give it a go

 

DavidReynolds
Mega Contributor

My approach is a little different. I create a report listing the knowledge base, status, possibly the author if that is important and then Article Contains and input the key word or phrase. If I want to search exactly, I use double quotes. I mainly use this method to track down articles linked to others I'm going to retire. However, it can be used to search for anything.

All of these conditions must be met.

Active is True; Status is published; article body contains (example) Microsoft Authenticator.

This is the method I've also used in the past. The benefit is by creating a report you can see your progress as articles that no longer contain the term(s) will drop off the report. You can also have others help work through the report. 

 

Oh, how I was there was the possibility for knowledge admins to do bulk edits on articles - another KB tool I've used has a "find and replace" bulk edit capability, and a link validator, and...