【问题标题】:How can we get counts of each group when grouped by a certain field in elasticsearch?在elasticsearch中按某个字段分组时,我们如何获得每个组的计数?
【发布时间】:2021-03-13 18:19:17
【问题描述】:

这是我的文档的样子。

{
    "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 multiple fields grouped by,只是我不必过滤。 我在我的 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."
      }
]

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

谢谢

【问题讨论】:

    标签: elasticsearch amazon-dynamodb kibana elasticsearch-aggregation elasticsearch-dsl


    【解决方案1】:

    您可以像示例中一样使用terms 聚合并且您可以在不使用过滤器的情况下这样做。将 Industry 字段的映射配置为 of type keyword 后,您就可以运行

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

    【讨论】:

    • 谢谢!代码按预期工作。您能否通过将其从“Industry”更改为“Industry.keyword”来解释它在框内带来了什么不同?想知道它在弹性搜索中是如何工作的。谢谢
    • 很高兴它成功了。关于这方面的文章很多,但说白了,text 用于部分搜索,keyword 用于精确匹配。现在,为了让 ES 做到这一点,必须分析此文本,从而增加磁盘空间等。另外,在此类文本字段上聚合(精确术语)是 very expensive too。因此,所做的是启用 keyword 字段类型,该类型采用原样的值,即保留大写、空格等的确切术语。
    • 无耻插件:我正在编写 Elasticsearch 手册。 What else would you like to learn about?
    猜你喜欢
    • 2011-10-21
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    相关资源
    最近更新 更多