如何在弹性搜索中对多个索引的结果进行排序和限制 [英] How to sort and limit result from multiple indices in elastic search

查看:78
本文介绍了如何在弹性搜索中对多个索引的结果进行排序和限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个索引,分别称为index1,idndex2,index3和index4

I've 4 indices called index1, idndex2, index3 and index4

让我们说结果集中有数百个结果
现在我只想要

Lets say there are hundreds result in resultset Now I want only

top 10 rows/result from index1
+
top 10 rows/result from index2
+
top 10 rows/result from index3
+
top 10 rows/result from index4


推荐答案

您可以使用Elasticsearch的Multi-Search API。
不要在url中提供任何索引名称,而应在url中使用_msearch关键字,如下所示:

You can use the Multi-Search API of Elasticsearch. Don't provide any index name in the url , instead use the _msearch keyword in the url as below :

GET _msearch
{"index" : "index1"}
{"query" : {}, "from" : 0, "size" : 10}
{"index" : "index2"}
{"query" : {}, "from" : 0, "size" : 10}

reference- https:/ /www.elastic.co/guide/zh-CN/elasticsearch/reference/current/search-multi-search.html

reference - https://www.elastic.co/guide/en/elasticsearch/reference/current/search-multi-search.html

或者,您也可以执行跨多个索引的单个查询,然后使用按索引名称的聚合将结果分组;将top_hits大小指定为10,以从每个索引中获取前10个匹配项。

Alternatively, you can also perform a single query across multiple indices and then group the results using aggregations by index name;specify the top_hits size as 10 to get the top 10 hits from each index.

GET index1,index2,index3/_search
{
  "size": 0,
  "query": { ... },
  "aggs": {
    "indexes": {
      "terms": {
        "field": "_index",
        "size": 50
      },
      "aggs": {
        "hits": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}

这篇关于如何在弹性搜索中对多个索引的结果进行排序和限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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