Is it possible to sort randomly, and to query on field if it exists?

送分小仙女□ 提交于 2020-01-23 13:06:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!