【问题标题】:How to make 2 groups in random order in ElasticSearch如何在 ElasticSearch 中以随机顺序创建 2 个组
【发布时间】:2014-10-03 11:48:08
【问题描述】:

例如,在 google.com 中,有 2 种搜索结果,1. 与关键字相关的广告和 2. 正常搜索结果。

如何在以下条件下使用弹性搜索来做到这一点

  1. 在搜索结果的前 3 名中随机显示 3 个广告位,按用户 ID 随机显示
  2. 显示同样由用户 ID 随机的正常搜索结果

如何通过单个查询将这两个条件整合到搜索结果中。

给定样本数据,用'a'查询,然后它应该返回随机(支付1-3),然后是随机(正常4-6)

{"type" : "paid", "document" : "paid random 1"}
{"type" : "paid", "document" : "paid random 2"}
{"type" : "paid", "document" : "paid random 3"}
{"type" : "normal", "document" : "normal random 4"}
{"type" : "normal", "document" : "normal random 5"}
{"type" : "normal", "document" : "normal random 6"}

【问题讨论】:

  • 您尝试过stackoverflow.com/questions/9796470/… 获得随机结果吗??
  • 嗨,这将只查询单个组。已经试过了。 :)
  • 所以,您想在单个请求中获取搜索项目的前 3 个项目和其他三个项目(随机)?你有什么疑问吗??
  • 让我们说 top3 是随机付费搜索结果。下面是随机的正常搜索结果。我找不到这样子查询的方法。
  • 那么,搜索查询是一样的吗? ?

标签: elasticsearch


【解决方案1】:

您好,看到您的问题, 我猜您正在尝试获取文档。

  1. 支付金额排名前三的随机文件
  2. 前三个随机文档(默认)

所以,我设法为你提出了一个请求, 我用过top_hits聚合,

这是请求,

curl -XPOST "http://localhost:9200/x3/_search" -d'
{
   "query": {
      "match": {
         "doc": "xxx"
      }
   },
   "aggs": {
      "tophits_random_paid": {
         "top_hits": {
            "from": 0,
            "size": 3,
            "sort": [
               {
                  "paidAmount": {
                     "order": "desc"
                  }
               },
               {
                  "_script": {
                     "script": "(doc[\"userid\"].value + salt).hashCode()",
                     "type": "number",
                     "params": {
                        "salt": "some_random_string"
                     },
                     "order": "asc"
                  }
               }
            ]
         }
      },
      "tophits_random_default": {
         "top_hits": {
            "from": 0,
            "size": 3,
            "sort": [
               {
                  "_script": {
                     "script": "(doc[\"userid\"].value + salt).hashCode()",
                     "type": "number",
                     "params": {
                        "salt": "some_random_string"
                     },
                     "order": "asc"
                  }
               }
            ]
         }
      }
   }
}'

谢谢,希望对你有帮助!!

【讨论】:

    猜你喜欢
    • 2012-11-19
    • 2011-08-06
    • 2018-11-28
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2020-10-11
    • 2021-03-18
    • 1970-01-01
    相关资源
    最近更新 更多