Search a whole and exact word in Gliderecord Query

dvdfernandez
Tera Contributor

Hello community:


We are looking for a way to search for a complete and exact word using #GlideRecord query. We have a search engine to search for information in the product tables (Case, Issue, etc). We are using GlideRecord but we detect that when we want to search for a whole word, the results also include words containing the string. Examples:


String to search for: “ING” to get the data of a bank called “ING Direct”.
Results: MarketING, INGenio, etc.


Does anyone know how to do exact and whole word searches?


Thanks in advance!

 

8 REPLIES 8

Rajesh Chopade1
Mega Sage

hi @dvdfernandez 

You could write a custom Script Include that performs the filtering logic, checking for exact matches by splitting the string into words and performing searches on each word.

var CustomSearchUtils = Class.create();
CustomSearchUtils.prototype = {
    initialize: function() {},

    searchExactWord: function(table, field, word) {
        var gr = new GlideRecord(table);
        var encodedQuery = field + "LIKE " + word + " ^OR" + field + "LIKE " + word + " %^OR" + field + "LIKE % " + word;
        gr.addEncodedQuery(encodedQuery);
        gr.query();
        return gr;
    },

    type: 'CustomSearchUtils'
};

 

You can then call this Script Include in a GlideRecord search to filter only those records that match the exact word.

 

i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

rajesh

dvdfernandez
Tera Contributor

Thank @Rajesh Chopade1 but we tested it before and it doesn't works because the filter search Word, Word% or %Word so you´ll obtain, in the example looking for "ing", results as "ING" (correct), marketING (incorrect) and INGoing (incorrect).

 

To clarify, we need to find the exact words in a text, not only the string. 

 

Thanks anyway por your help!

Unfortunately, out-of-the-box GlideRecord doesn’t provide an exact "word boundary" search option.

 

you can approximate this functionality by using regular expressions (regex) within GlideRecord.

But Regular expressions can impact query performance, so use them judiciously, especially if your tables have a lot of data.

 

Sample regex you can use:

var gr = new GlideRecord('your_table');
var searchWord = 'ING';
var regex = '^' + searchWord + '$';  // Ensure it's an exact match

gr.addEncodedQuery('your_fieldREGEXP' + regex);
gr.query();

while (gr.next()) {
    gs.print(gr.your_field);
}

Rohit99
Mega Sage

Hi @dvdfernandez,
I tried with following code and it worked for me.

You may try with this.

var string_v = 'Test';
var gr = new GlideRecord('incident');
gr.addQuery('short_description','LIKE','% '+ string_v).addOrCondition('short_description','LIKE',string_v + ' %').addOrCondition('short_description','LIKE','% '+ string_v + ' %');
gr.query();
while(gr.next())
{
    gs.print(gr.short_description);
}
 

Please mark my response as correct and helpful if it helped solved your question.

 

Thanks,

Rohit Suryawanshi