如何在弹性搜索中对多个索引的结果进行排序和限制

2024-03-09

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

假设结果集中有数百个结果 现在我只想

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 关键字,如下所示:

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

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

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

GET index1,index2,index3/_search
{
  "size": 0,
  "query": { ... },
  "aggs": {
    "indexes": {
      "terms": {
        "field": "_index",
        "size": 50
      },
      "aggs": {
        "hits": {
          "top_hits": {
            "size": 10
          }
        }
      }
    }
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在弹性搜索中对多个索引的结果进行排序和限制 的相关文章

随机推荐