【问题标题】:Filter Aggregated Data Using Elastic Search 1.7使用 Elastic Search 1.7 过滤聚合数据
【发布时间】:2017-09-02 11:44:42
【问题描述】:

假设我有一个帖子索引,其数据如下所示:

{
  "title": "Some Recipe",
  "book_id": 123,
  "content": "A list of ingredients",
  "tags": ["apple", "cinnamon", "honey"]
}

我想要一个查询来检索以给定短语开头的所有标签以及包含该标签的帖子数。

到目前为止,我已经尝试了以下方法:

{
  "size": 0,
  "query": {
    "bool": {
      "must": {
        "term": {
          "book_id": 123
        }
      }
    }
  },
  "aggs": {
    "tags": {
      "filter": { "prefix": { "tags": "app" } },
      "aggs":   {
        "tags" : { "terms": { "field": "tags" } }
      }
    }
  }
}

我相信它会返回所有文档中的所有标签以及具有以app 开头的标签的文档数量。

是否可以让它拉回所有以app 开头的标签,在本例中为apple,以及具有该标签的文档数?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    terms 聚合允许您使用include 设置来filter the values 的术语。试试这个:

    {
      "size": 0,
      "query": {
        "bool": {
          "must": {
            "term": {
              "book_id": 123
            }
          }
        }
      },
      "aggs": {
        "tags": {
          "filter": { "prefix": { "tags": "app" } },
          "aggs":   {
            "tags" : { 
              "terms": { 
                "field": "tags",
                "include": "app.*"            <--- add this
              } 
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 2014-06-29
      • 2018-01-14
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多