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

Brad Bowman
Kilo Patron
Kilo Patron

Have you tried the .search method with a regular expression using the word boundary assertion \b?

stringName.search(/\bING\b/);

Slava Savitsky
Giga Sage

What does your current code look like and where does it run?

dvdfernandez
Tera Contributor

Hi!

 

Thanks @Brad Bowman  for the answer. We tried it using RegEx but we found three problems:

 

1.- Speed: If we want to apply a RegEx, we do a string match search and then apply RegEx over the results. We couldn't to apply RegEx directly over first search.

 

2.- More than one exact word match: The RegEx have an order. If we've more than one word, we need to found all the patterns: first word1 and the word2 and, first word2 and then word1. With three words starts the complexity...

 

var word1 = 'ING';
var word2 = 'depósito';

var doc = new GlideRecord('u_document_attachment');
doc.addEncodedQuery('u_type=08f0a2081b8bc11006689753b24bcbef^sys_created_by=test.oauth^u_state=current^u_nameLIKE' + word1);
doc.query();
gs.print('FIRST search: ' + doc.getRowCount());
var count = 0;

while (doc.next()) {
    var title = doc.u_name;
    //var regex = new RegExp("\\b" + word1 + "\\b", "i");
    var regex = new RegExp("\\b(" + word1 + "\\b.*\\b" + word2 + "|" + word2 + "\\b.*\\b" + word1 + ")\\b", "i");

    if (regex.test(doc.u_name)) {
        count++;
        gs.print(title);
    }
}

gs.print('TOTAL post REGEX: ' + count);

3. RegEx with some special characters: Some words with characters like “á”, “é”, “í”, “ó” and “ú” in Spanish are different from words with “a, e, i, o ,u”. As far as we know, GlideRecord does not distinguish between “á” and “a” so, after the global query (result with and without accents), we have to prepare a RegEx to find all possibilities. If the word entered as string to search has accents, we only have to change it to without accent (clean it). But the opposite case is more complicated because we would have to know which words have accent or not to be able to incorporate it well written to the RegEx.

 

Sorry if something is not well understood 😉

 

 

I tried to test this and couldn't get the regex with \b to be recognized anyway, so that must not work in ServiceNow JavaScript, unless I wasn't testing correctly.

 

In any event, I think this approach may address your issues.  Using an out of the box table and field as an example, indexOf and GlideRecord seems to be distinguishing between depósito and deposito for me, so maybe this will work for you?

var word1 = 'ING';
var word2 = 'depósito';
var count = 0;
var incGr = new GlideRecord('incident');
incGr.query();
while (incGr.next()) {
	var sdArr = incGr.short_description.split(' ')
	for (var i=0; i<sdArr.length; i++) {
		if (sdArr[i] == word1) {
			count++;
			gs.print('word 1 ' + incGr.short_description);
		} else if (sdArr[i] == word2) {
			count++;
			gs.print('word 2 ' + incGr.short_description);
		}
	}
}
gs.print ('total count ' + count);

You can nest or combine the if statements - if you're looking for both whole words in the string - or whatever fits your requirements.