2

On Algolia, I'm trying to sort randomly ... Is it possible?
I also try to query if a field doesn't exists.
With facets I tried something like my_attribut:null ... but I'm not sure if it's possible.

Can you help me?

Thank

FBHY
  • 1,004
  • 1
  • 15
  • 35

1 Answers1

1

Random sort order

Even though it could possibly make sense to have a random order in the ranking formula, it is incompatible with the way the engine has been built. By focusing on speed + pertinence, Algolia's engine has made some tradeoffs since it is precalculating a lot of information at indexing time making it impossible to randomize at each query. Furthermore, it would break any pagination.

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

Using the shuffle method of this question How can I 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 the JavaScript client:

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

With the JavaScript helper :

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

Querying a non-existing attribute

The only way to do such a thing with Algolia is to push a value that you'll consider as null (e.g. the null string or -1 for positive integers).

Community
  • 1
  • 1
Jerska
  • 11,722
  • 4
  • 35
  • 54
  • 1
    @Jerksa Unless I'm mistaken, shuffling like this _on the client-side_ will always only result in the results of _each page_ shuffled, individually (assuming you are using some kind of pagination). So if your index is sorted alphabetically by default, your pages would still be alpha sorted, but the hits within each one shuffled, which is probably not what one wants. – Tim Kelty Feb 23 '17 at 15:59
  • 2
    @TimKelty Definitely. I thought I was clear with the introduction stating that you can't do that with Algolia. However, randomizing doesn't make much sense (according to the use cases I've seen) except to change the top 2/3 results to see with Analytics if others answer better to the query. Also, you can always retrieve a big amount of results, shuffle them and reimplement pagination on top of those. – Jerska Feb 23 '17 at 20:00
  • 1
    Algolia suggests an alternative : Periodically shuffle results. "You can add an attribute with a random integer value to all your records. Then, you can use this attribute as part of the custom ranking in your ranking formula. The results are still predictable in the sense that all users get identical results for the same query. Still, you may periodically change the random value and reindex the objects, thus changing the order of the results. This is useful if you want to implement a random daily selection of items, for example." Obviously this cannot be done for every single request. – nicolas Jan 16 '23 at 08:41