是否可以随机排序,并在字段上查询是否存在? [英] Is it possible to sort randomly, and to query on field if it exists?

查看:21
本文介绍了是否可以随机排序,并在字段上查询是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Algolia 上,我试图随机排序......这可能吗?
我也尝试查询字段是否不存在.
使用 facets 我尝试了类似 my_attribut:null 的东西......但我不确定它是否可能.

你能帮我吗?

谢谢

解决方案

随机排序

尽管在排名公式中使用随机顺序可能有意义,但它与引擎的构建方式不兼容.通过专注于速度 + 针对性,Algolia 的引擎做出了一些权衡,因为它在索引时预先计算了大量信息,因此无法在每个查询中随机化.此外,它会破坏任何分页.

但是,您可以在收到结果后进行打乱,这样您就可以拥有正确的分页.

使用这个问题的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);返回o;}

使用 JavaScript 客户端:

index.search('something', function searchDone(err, content) {content.hits = shuffle(content.hits);//使用它们});

使用 JavaScript 助手:

helper.on('result', function (data){data.hits = shuffle(data.hits);//使用它们});

查询不存在的属性

用 Algolia 做这种事情的唯一方法是推送一个你认为是 null 的值(例如 null 字符串或 -1 用于正整​​数).

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

解决方案

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).

这篇关于是否可以随机排序,并在字段上查询是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆