【问题标题】:ElasticSearch group by documents field and count occurencesElasticSearch 按文档字段分组并计算出现次数
【发布时间】:2020-03-03 03:48:08
【问题描述】:

我的 ElasticSearch 6.5.2 索引看起来像:

      {
    "_index" : "searches",
    "_type" : "searches",
    "_id" : "cCYuHW4BvwH6Y3jL87ul",
    "_score" : 1.0,
    "_source" : {
      "querySearched" : "telecom",
    }
  },
  {
    "_index" : "searches",
    "_type" : "searches",
    "_id" : "cSYuHW4BvwH6Y3jL_Lvt",
    "_score" : 1.0,
    "_source" : {
      "querySearched" : "telecom",
    }
  },
  {
    "_index" : "searches",
    "_type" : "searches",
    "_id" : "eCb6O24BvwH6Y3jLP7tM",
    "_score" : 1.0,
    "_source" : {
      "querySearched" : "industry",
    }

我想要一个返回此结果的查询:

"result": 
{
"querySearched" : "telecom",
"number" : 2
},
{
"querySearched" : "industry",
"number" : 1
}

我只想按出现次数分组并获取每个的数量,限制为十个最大的数字。我尝试使用聚合,但存储桶是空的。 谢谢!

【问题讨论】:

    标签: elasticsearch elastic-stack elasticsearch-6 elastica


    【解决方案1】:

    案例你的映射

    PUT /index
    {
      "mappings": {
        "doc": {
          "properties": {
            "querySearched": {
              "type": "text",
              "fielddata": true
            }
          }
        }
      }
    }
    

    您的查询应该是这样的

    GET index/_search
    {
      "size": 0,
      "aggs": {
        "result": {
          "terms": {
            "field": "querySearched",
            "size": 10
          }
        }
      }
    }
    

    您应该添加 fielddata:true 以启用 text 类型字段 more of that

    的聚合
        "size": 10, => limit to 10
        
    

    在与@Kamal 进行简短讨论后,我有义务让您知道,如果您选择启用fielddata:true,您必须知道 它会消耗大量的堆空间。

    来自我分享的链接:

    Fielddata 会消耗大量堆空间,尤其是在加载高基数文本字段时。一旦 fielddata 被加载到堆中,它就会在段的生命周期内保持在那里。此外,加载字段数据是一个昂贵的过程,可能会导致用户遇到延迟问题。这就是为什么默认禁用 fielddata 的原因。

    另一种选择(更有效的一种):

    PUT /index
    {
      "mappings": {
        "doc": {
          "properties": {
            "querySearched": {
              "type": "text",
              "fields": {
               "keyword": {
                 "type": "keyword",
                 "ignore_above": 256
               }
             }
            }
          }
        }
      }
    }
    

    然后是你的聚合查询

    GET index/_search
    {
      "size": 0,
      "aggs": {
        "result": {
          "terms": {
            "field": "querySearched.keyword",
            "size": 10
          }
        }
      }
    }
    

    两种解决方案都有效,但您应该考虑this

    希望对你有帮助

    【讨论】:

    • 非常感谢它有效,但订单无效。关于 fieldata 我已经做过了 ;-)。
    • 我编辑了您的帖子以删除 order by,默认情况下它按 doc_count 排序 ;-)。
    • 当然。这只是为了向您展示如何按 aggs 结果排序。
    • @Kamal 我明白你的意思,我改进了我的答案,让未来的观众知道fielddata:true 可能不是最好的解决方案。
    • 我在没有 fielddata true 的情况下做得很好 ;-)。谢谢大家。
    【解决方案2】:

    你尝试了什么?

    发布 /searches/_search

       {
          "size": 0,
          "aggs": {
            "byquerySearched": {
              "terms": {
                "field": "querySearched",
                 "size": 10
              }
            }
          }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-25
      • 2014-11-17
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多