How to make partial pattern matches in keyword queries?

Maxim8
Giga Contributor

Hello all,

As the question states, I was wondering if there is a way to make partial pattern matches for keyword queries using GlideRecord?

Let's say I have records/text like:

Apple

Orange

Apple and Orange

I know that if I do something like

gr.addQuery("123TEXTQUERY321", keyword);

I have to match the entire word for a result for a result to show up, and if I make keyword something that is a partial match, like

gr.addQuery("123TEXTQUERY321", "Appl");

no results will show. Is there a way to get results back using partial matches and if so, what is the best way?

I've tried using wildcards like *keyword and keyword* but that doesn't really solve my issue because there are cases like searching the last X digits of a record's number field where the result won't show up. Individually, these also don't help the partial matching issue either. Querying Appl* helps with the partial matching issue but it doesn't help if the query is for the last X digits of a record's number field (000001* isn't going to return results for RITM0000001). And switching from keyword* to *keyword just switches the type of queries that won't show up.

/*
Example records
RITM0000001 - Oranges
TASK0000001 - Apples
*/

// keyword*
gr.addQuery("123TEXT321", "Appl*")
// TASK0000001 - Apples appears
// RITM0000001 - Oranges doesn't appear

gr.addQuery("123TEXTQUERY321", "0000001*")
// Nothings appears


// *keyword
gr.addQuery("123TEXTQUERY321", "*Appl");
// Nothing appears

gr.addQuery("123TEXTQUERY321", "*0000001");
// TASK0000001 appears
// RITM0000001 appears

I've also tried doing things like *keyword* but I get an error message saying how the wildcards are too ambiguous/too many results (or something like that).

 

Is there any way to achieve partial pattern matching when querying keywords? Or do I have to resort to some type of workaround?

10 REPLIES 10

IF there's a need to do a full text query, maybe use "IR_AND_OR_QUERY"?

https://codecreative.io/blog/how-to-do-full-text-search-with-gliderecord/

"123TEXTQUERY321" is synonymous with "IR_AND_QUERY" and does a exact match on all terms. "IR_OR_QUERY" would do a match on any terms. IR_AND_OR_QUERY would do a match on any terms but place the exact match on top.

I've tried IR_AND_OR_QUERY, but I still run into the issue where I need a full word match in order for a result to appear.

 

If the record has the text "applejack", even if I query "apple" using IR_AND_OR_QUERY, it won't show up in the results. I would need to query "applejack" for it to appear in the results.

I just created an article in knowledge with content "applejack". Unfortunately, searching for "apple" resulted in finding articles with "Apple" of MacOS. So, I tried with '*jack" and was able to find the article with the following code.

var gr = new GlideRecord('kb_knowledge');
gr.addQuery('IR_AND_OR_QUERY', '*jack'); // or '%%%%%jack'
gr.query();
while(gr.next()) {
  gs.info(gr.number);
}

Changed to show more output.

var gr = new GlideRecord('kb_knowledge');
gr.addQuery('IR_AND_OR_QUERY', '%%%%%jack');
gr.query();
while(gr.next()) {
  gs.info(gr.number + ' ' + gr.short_description + ' ' + gr.text);
}

Result from executing in Script Background

*** Script: KB0010088 Test article <p>applejack</p>

 

EDIT: BTW, my knowledge is setup to Enable wildcard searches.

https://docs.servicenow.com/bundle/paris-platform-administration/page/administer/search-administrati...