【问题标题】:retrieve only a specific _index documents in the hits of elasticsearch when querying on multiple indices查询多个索引时,仅检索弹性搜索命中中的特定 _index 文档
【发布时间】:2020-10-02 19:07:16
【问题描述】:

我在一个 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": {}}]}

我只想检索产品索引。

【问题讨论】:

  • 您能否用您的映射、这两个索引的示例文档以及您尝试过的查询来更新问题。那会很有帮助。
  • 或者,您能否让我知道您正在对其进行聚合的 my_field 的字段是否存在于两个索引中?
  • 我说的是弹性搜索的每个命中结果中包含的_index 字段。如果可能的话,我只是想过滤一下。
  • 您可以发布您的查询吗?那会很有帮助。
  • 因此,如果我理解正确,您希望从两个索引返回聚合结果,但是您只想从两个索引之一返回文档/命中。我不认为这是可能的。您可能必须在应用程序层对此进行管理。无论您在查询中提到什么,聚合都将应用于属于该查询的文档。因此,如果您在查询中的 _index 上添加过滤器,聚合也将仅应用于该索引中的文档。

标签: elasticsearch elasticsearch-dsl elasticsearch-query


【解决方案1】:

使用post_filter绝对可以:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2014-06-22
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多