Gravatar for

Question by kmelkez, Jul 4, 2014 5:18 PM

Set priority in sort criteria


I use Javascript Search Framework for my project and I sort my results by a custom field (@mycustomfield ascending), according to the documentation here : My custom field is an integer value 0 to 3. It works well but if my results have the same value for this field (for instance all 0), I would like to apply a sort by relevancy.

I don't know if it's possible ?

1 Reply
Gravatar for

Answer by Martin Laporte, Jul 7, 2014 7:04 AM

Hmm I don't think you can do that using only the Sort component, but there is a new index feature called "ranking functions" that I think you could use to achieve the same result. Ranking functions are different from query ranking expressions (or QRE) by the way.

A ranking function works by computing a expression for each matching result and adding the result of the function to the score of this document. So if you have a numeric custom field you can simulate a sort on that field by putting this in your advanced query expression:

$qrf(expression: '@myfield * 1000')

$qrf is the syntax used to define a query ranking function. If you always want to sort on the field just put that in the filter your Tab component, otherwise you'll need some custom JS to inject the expression in the buildingQuery event when appropriate.

This works because 1000 is a high enough value that it overrides all the combined factors coming from the stock relevancy engine. So the results with a higher value for @myfield will appear first, and those with the same value be sorted against each other using the stock relevancy score.

I do think we should have a more direct way to get this result, maybe by introducing a new kind of sort order. But in the meanwhile this might work for you.

As I mentioned, it's a a very recent features, and depending on the version you're using it might not be available. Also I can't yet find much public documentation about it (I'll see with the right person), but our internal wiki mentions that the field you're "sorting" on should be in the ComputedFieldsInMemory list from the index settings, otherwise performance might be pretty bad.

I hope this helps :)

Gravatar for

Comment by kmelkez, Jul 7, 2014 11:11 AM

Thanks for the answer. Is ComputedFieldsInMemory list an option in coveo admin ? because I don't know where it is.

Gravatar for

Comment by Martin Laporte, Jul 7, 2014 11:33 AM

I think it must be change directly in the config.txt file (that's typically in your index folder). So stop your index service, open the file, locate the XML element of this name, edit it, save, and start the service again.

Gravatar for

Comment by kmelkez, Jul 7, 2014 2:58 PM

Ok but i'm using PHP and Symfony 2 framework with the Javascript Search Interface, is it specific for sitecore ?

Gravatar for

Comment by Martin Laporte, Jul 8, 2014 3:49 AM

No, it's not related to Sitecore at all. Look for the folder where you put your index (default: C:\CES7), in there you'll find a config folder with config.txt in it.

Gravatar for

Comment by kmelkez, Jul 8, 2014 10:12 AM

Ok thank you very much !

Ask a question