简体   繁体   English

获取elasticsearch中字段中的唯一术语数

[英]Get the number of unique terms in a field in elasticsearch

Here are some sample documents that I have 以下是我的一些示例文档

doc1 DOC1

{
"occassion" : "Birthday",
"dessert": "gingerbread"
}

doc2 DOC2

 {
"occassion" : "Wedding",
"dessert": "friand"
}

doc3 doc3的

{
"occassion":"Bethrothal" ,
"dessert":"gingerbread"
}

When I give simple terms aggregation, on the field "dessert", i get like the results like below 当我给出简单的术语聚合时,在“甜点”字段上,我得到如下结果

"aggregations": {
  "desserts": {
    "doc_count_error_upper_bound": 0,
    "sum_other_doc_count": 0,
    "buckets": [
      {
        "key": "gingerbread",
        "doc_count": 2
      },
      {
        "key": "friand",
        "doc_count": 1
      }
    ]
  }
}
}

But if the issue here is if there are many documents and I need to know how many unique keywords were existing under the field name "desserts",it would take me a lot of time to figure it out. 但如果这里的问题是如果有很多文件,我需要知道字段名称“甜点”下有多少独特的关键字,我需要花很多时间来弄明白。 Is there a work around to get just the number of unique terms under the specified field name? 是否有解决方法只能获得指定字段名称下的唯一术语数量?

The cardinality aggregation seems to be what you're looking for: https://www.elastic.co/guide/en/elasticsearch/guide/current/cardinality.html 基数聚合似乎是您正在寻找的: https//www.elastic.co/guide/en/elasticsearch/guide/current/cardinality.html

Querying this: 查询:

{
    "size" : 0,
    "aggs" : {
        "distinct_desserts" : {
            "cardinality" : {
              "field" : "dessert"
            }
        }
    }
}

Would return something like this: 会返回这样的东西:

"aggregations": {
  "distinct_desserts": {
     "value": 2
  }
}

I would suggest cardinality with higher precision_threshold for accurate result. 我建议使用更高的precision_threshold基数来获得准确的结果。

GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "count_distinct_desserts" : {
            "cardinality" : {
              "field" : "dessert",
              "precision_threshold" : 100 
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM