查询多个索引时,仅检索 elasticsearch 命中中的特定 _index 文档

retrieve only a specific _index documents in the hits of elasticsearch when querying on multiple indices

我在 elasticsearch 的一个查询中查询两个索引,因此我可以同时对它们进行聚合。问题是我只希望其中一个索引文档在点击中,而不是两个。因此,我想过滤查询中的 _index 字段。

查询

http://localhost:9200/products,stores/_search

{
    query: {
        match_all: {}
    },
    aggs = {
        stores : { terms: { field: 'store_name' } }
    }
}

示例输出

{"hits" :[{"_index": "products",
 "_type": "_doc",
 "_id": "PFS0OTD5UE",
 "_score": 123.057205,
 "_source": {}},

{"_index": "stores",
 "_type": "_doc",
 "_id": "SXBT3ER",
 "_score": 53.057205,
 "_source": {}}]}

我只想检索产品索引。

使用post_filter绝对可以:

POST products,stores/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "stores": {
      "terms": {
        "field": "store_name"
      }
    }
  },
  "post_filter": {
    "term": {
      "_index": "products"
    }
  }
}

聚合将 运行 来自两个索引的所有文档,但只有来自 products 的文档将在命中中返回。