当按弹性搜索中的某个字段分组时,我们如何获得每个组的计数?

How can we get counts of each group when grouped by a certain field in elasticsearch?

这是我的文档的样子。

{
    "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
}

我在这里找到了一个类似的post,只不过我不用过滤。 我在我的 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."
      }
]

如果有人知道解决方案,请告诉我。

谢谢

您可以像示例中那样使用 terms 聚合 并且 您可以使用 w/o 过滤器。一旦您将 Industry 字段的映射配置为 of type keyword,您就可以 运行

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