问题
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
回答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).
来源:https://stackoverflow.com/questions/33123488/is-it-possible-to-sort-randomly-and-to-query-on-field-if-it-exists