【问题标题】:How does ES multiple random queries without repeating the results?ES如何多次随机查询而不重复结果?
【发布时间】:2019-06-14 17:26:37
【问题描述】:

elasticsearch 5.0 版

我有需求随机多次查询用户信息,但最终结果不能有重复数据。

例如,

第一个随机查询结果

用户0 用户1 用户2

第二次随机查询结果

用户0 用户3 用户4

User0 是重复的。

这是我的随机查询,如何修改?

{
  "size" : 10,
  "query" : {
    "match_all" : {
      "boost" : 1.0
    }
  },
  "_source" : {
    "includes" : [

    ],
    "excludes" : [ ]
  },
  "sort" : [
      {
      "_script" : {
        "script" : {
          "inline" : "Math.random()",
          "lang" : "painless"
        },
        "type" : "number",
        "order" : "asc"
      }
    }
  ],
  "ext" : { }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:
     {
       "size": 1,
       "query": {
          "function_score": {
             "functions": [
                {
                   "random_score": {
                      "seed": "1477072619038"
                   }
                }
             ]
          }
       }
    }
    

    You can follow this https://www.elastic.co/guide/en/elasticsearch/reference/5.4/query-dsl-function-score-query.html#function-random

    【讨论】:

      【解决方案2】:

      您可以使用bool must_not 查询和id 查询来删除以前检索到的文档的ID。

      {
        "query": {
          "match_all": {
            "boost": 1.0
          },
          "bool": {
            "must_not": [
              {
                "ids": {
                  "values": [The set of previous Ids]
                }
              }
            ]
          }
        },
        ...
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多