How to sort search results from a GlideRecord Query based on Relevancy?

axw1n
Kilo Explorer

Consider that my Task table has 10 records(Task type is not relevant).

Record #1: Short Description = "Hello bla bla bla World";

Record #2: Doesn't contain neither "Hello" nor "World"

Record #3: Short Description = "HELLO WORLD";

Record #4: Doesn't contain neither "Hello" nor "World"

Record   #5: Description = "World";

Record #6: Doesn't contain neither "Hello" nor "World"

Record #7: Description = "Hello bla bla bla World";

Record #8: Doesn't contain neither "Hello" nor "World"

Record #9: Short Description = "Hello";

Record   #10: Description = "World Hello";

Consider the following GlideRecord query,

var t = new GlideRecord('task')

t.addQuery('IR_AND_OR_QUERY','HELLO WORLD');

t.query();

while(t.next()){

        gs.print(t.number);

}

The Results returned would be something like,

***Script: Record#1

***Script: Record#3

***Script: Record#5

***Script: Record#7

***Script: Record#9

***Script: Record#10

Now,

What do I do --> in the sense -->   what orderBy query can I add to the GlideRecord query so that the Results returned are the most relevant Records? I want Record#3 to be my 1st Result.

Like,

var t = new GlideRecord('task')

t.addQuery('IR_AND_OR_QUERY','HELLO WORLD');

t.orderBy('relevance') --> What should this query be exactly --> Something like this is being done in Knowledge Searches I believe.

t.query();

while(t.next()){

        gs.print(t.number);

}

Please assist!

3 REPLIES 3

Deepa Srivastav
Kilo Sage

Please clarify where you want to use it exactly , Knowledge or any other module?


axw1n
Kilo Explorer

On my CMS to query on a Custom Table.


rituc
Kilo Expert

Hi Aswin



var t = new GlideRecord('task');


t.orderBy('relevance');


t.addQuery('IR_AND_OR_QUERY','HELLO WORLD');


t.query();


while(t.next()){


        gs.print(t.number);


}



Try using the orderBy() befoe the addQuery()



You can refer to Scoped GlideRecord API Reference - ServiceNow Wiki