在elasticsearch中,当按特定字段分组时,我们如何获得每个组的计数?

xv8emn3q  于 2021-06-09  发布在  ElasticSearch
关注(0)|答案(1)|浏览(475)

这是我的医生的样子。

{
    "Summary": "The One Way You're Putting Pressure on Your Partner Without Realizing It=20",
    "Industry" : "Lifestyle and Fitness",
    "Name": "Kali Coleman",
    "Email" : "query-bixh@helpareporter.net",
    "Media Outlet": "Best Life Online"
},
{
    "Summary": "The One Way You're Putting Pressure on",
    "Industry" : "High Tech",
    "Name": "John Smith",
    "Email" : "query-tech@helpareporter.net",
    "Media Outlet": "Anonymous"
}

我要计算每种类型的“行业”字段的文档数。这是我想要的输出。

{
    "key": "Lifestyle and Fitness",
    "count": 1200
},
{
    "key": "High Tech",
    "count": 590
}

我在这里发现了一个类似的帖子elasticsearch count多个字段分组,只是我不需要过滤。我在我的kibana控制台上试了一下,出现了以下错误。

"root_cause" : [
      {
          "type" : "illegal_argument_exception",
          "reason" : "Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [Industry] in order to load field data by uninverting the inverted index. Note that this can use significant memory."
      }
]

如果有人知道解决办法,请告诉我。
谢谢

y1aodyip

y1aodyip1#

你可以使用 terms 聚合就像示例中的一样,您可以在不使用过滤器的情况下进行聚合。一旦您配置了 Industry 字段的Map类型为 keyword ,然后您可以运行

GET index_name/_search
{
  "size": 0,
  "aggs": {
    "by_industry": {
      "terms": {
        "field": "Industry.keyword"
      }
    }
  }
}

相关问题