Is it possible to sort randomly and query in a field if it exists?

In Algolia, I try to sort randomly ... Is this possible?
I am also trying to query if a field does not exist.
With facets I tried something like my_attribut:null ... but I'm not sure if this is possible.

Can you help me?

thanks

+2
source share
1 answer

Random sort order

Although it may make sense to have a random order in the ranking formula, it is incompatible with how the engine was built. Focusing on speed + relevance, the Algolia engine made some trade-offs as it predicts a lot of information when indexing, which makes randomization impossible with every request. In addition, it breaks any pagination.

However, you can shuffle the results after receiving them, which allows you to have the correct pagination.

Using the shuffle method of this question How to shuffle an array? :

 function shuffle (o){ for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); return o; } 

With JavaScript client :

 index.search('something', function searchDone(err, content) { content.hits = shuffle(content.hits); // Use them }); 

With javascript helper :

 helper.on('result', function (data){ data.hits = shuffle(data.hits); // Use them }); 

Request for a nonexistent attribute

The only way to do this with Algolia is to press a value that you consider null (for example, a string of null or -1 for positive integers).

+2
source

Source: https://habr.com/ru/post/1247537/


All Articles