首页 > 解决方案 > 如何根据当前 agg 进行 Elasticsearch Agg 查询?

问题描述

我的文档看起来像这样

{ cool_things: [{ name, id }, { name, id }] }

我如何找到我目前在聚合中的 id。例如,这是我正在使用的查询。

params.body.aggs = {
    uniqueCoolThings: {
      terms: {
        field: 'cool_things.name.keyword'
      },
      aggs: {
        value: {
          top_hits: {
            size: 1,
            _source: {
              includes: ['cool_things.id', 'cool_things.name.keyword']
            }
          }
        }
      }
    }
  }
}

然而这会回来

...hits._source: {
    uniqueCoolThings: [
        {
            "id": 500,
            "name": "Bob Sagget"
        },
        {
            "id": 501,
            "name": "Matt Damon"
        }
     ]
} ...

我想知道如何做一个 where 条件,以便它只返回与它当前所在的唯一 cool_things.name.keyword 匹配的 ID。

我需要获取所有 unique_cool 事物的列表,然后让他们的 ID 与该响应一起返回(如果这有意义的话)

任何帮助将不胜感激!!!!

标签: node.jselasticsearchelasticsearch-aggregation

解决方案


推荐阅读