首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ElasticSearch中按随机顺序生成2组

如何在ElasticSearch中按随机顺序生成2组
EN

Stack Overflow用户
提问于 2014-08-10 15:07:00
回答 1查看 296关注 0票数 0

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

在下面的条件下,我如何用弹性搜索来做到这一点呢?

  1. 在搜索结果的前3位显示带有随机用户in的3个插槽的广告
  2. 显示同样随机的用户标识的正常搜索结果

如何做一个单一的查询,将这两个条件集成在一起的搜索结果。

给定样本数据,使用“a”查询,然后返回随机(付费1-3),然后随机(普通4-6)。

代码语言:javascript
复制
{"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"}
EN

回答 1

Stack Overflow用户

发布于 2014-08-11 06:11:15

嗨,看看你的问题,我猜你是想去取文件。

  1. 按支付金额计算的前三位随机文档
  2. 前三位随机文档(默认)

因此,我成功地为您提出了一个请求,我使用了命中聚合,

这是我的请求

代码语言:javascript
复制
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"
                  }
               }
            ]
         }
      }
   }
}'

谢谢,希望这能帮上忙!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25229898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档