随机顺序分页弹性搜索 [英] Random order & pagination Elasticsearch

查看:198
本文介绍了随机顺序分页弹性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题
是一个功能请求,可以选择种子进行排序为了娱乐随机顺序。

In this issue is a feature request for ordering with optional seed allowing for recreation of random order.

我需要能够分页随机排序的结果。
如何用Elasticsearch 0.19.1?

I need to be able to paginate random ordered results. How could this be be done with Elasticsearch 0.19.1 ?

谢谢。

推荐答案

您可以使用唯一字段的哈希函数(例如id)和随机盐进行排序。根据结果​​应该是多么真实随意,你可以做一些原始的例子:

You can sort using a hash function of a unique field (for example id) and a random salt. Depending on how truly random the results should be, you can do something as primitive as:

{
  "query" : { "query_string" : {"query" : "*:*"} },
  "sort" : {
    "_script" : { 
        "script" : "(doc['_id'].value + salt).hashCode()",
        "type" : "number",
        "params" : {
            "salt" : "some_random_string"
        },
        "order" : "asc"
    }
  }
}

或与

{
  "query" : { "query_string" : {"query" : "*:*"} },
  "sort" : {
    "_script" : { 
        "script" : "org.elasticsearch.common.Digest.md5Hex(doc['_id'].value + salt)",
        "type" : "string",
        "params" : {
            "salt" : "some_random_string"
        },
        "order" : "asc"
    }
  }
}

第二个例子将产生mor随机结果,但会稍慢一些。

The second example will produce more random results but will be somewhat slower.

对于这种工作方式,必须存储 _id 字段。否则,查询将以 NullPointerException 失败。

For this approach to work the field _id has to be stored. Otherwise, the query will fail with NullPointerException.

这篇关于随机顺序分页弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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