【问题标题】:elasticsearch unique values aggregationelasticsearch 唯一值聚合
【发布时间】:2015-12-11 14:37:29
【问题描述】:

我想在名为“name”的字段中从 elasticsearch 获取唯一值, 我不知道如何设置值必须是唯一的条件。

这项工作的目的是从 elasticsearch 数据库中获取所有唯一名称。

So basically what i need is a aggregation query that fetch the unique values

谁能帮我解决这个问题,非常感谢。

【问题讨论】:

    标签: elasticsearch aggregation


    【解决方案1】:

    您可以在 not_analyzed 的字段上使用 terms 聚合。

    但是,默认情况下,这仅限于 10 个最流行的术语。您可以通过更新terms 聚合的size 参数来更改此设置。将其设置为0 将允许您拥有最多Integer.MAX_VALUE 不同的术语(请参阅文档here)。

    这是一个示例映射:

    POST terms
    {
      "mappings":{
        "test":{
          "properties":{
            "title":{
              "type":"string",
              "index":"not_analyzed"
            }
          }
        }
      }
    }
    

    添加一些文档:

    POST terms/test
    {
      "title":"Foundation"
    }
    
    
    POST terms/test
    {
      "title":"Foundation & Empire"
    }
    

    最后,请求:

    POST terms/_search?search_type=count
    {
      "aggs": {
        "By Title": {
          "terms": {
            "field": "title",
            "size": 0
          }
        }
      }
    }
    

    会给你你需要的:

    "aggregations": {
          "By Title": {
             "doc_count_error_upper_bound": 0,
             "sum_other_doc_count": 0,
             "buckets": [
                {
                   "key": "Foundation",
                   "doc_count": 1
                },
                {
                   "key": "Foundation & Empire",
                   "doc_count": 1
                }
             ]
          }
       }
    

    请注意,如果您有大量条款,则执行此请求将非常昂贵

    【讨论】:

      猜你喜欢
      • 2016-08-21
      • 2019-10-09
      • 2016-03-13
      • 1970-01-01
      • 1970-01-01
      • 2016-04-12
      • 2021-03-01
      • 1970-01-01
      • 2017-09-24
      相关资源
      最近更新 更多