Elasticsearch - 统计重复和唯一值的数量

5
我有以下的JSON数据:
[
 {"firstname": "john", "lastname": "doe"},
 {"firstname": "john", "lastname": "smith"},
 {"firstname": "jane", "lastname": "smith"},
 {"firstname": "jane", "lastname": "doe"},
 {"firstname": "joe", "lastname": "smith"},
 {"firstname": "joe", "lastname": "doe"},
 {"firstname": "steve", "lastname": "smith"},
 {"firstname": "jack", "lastname": "doe"}
]

我想获取重复名字的数量

重复的数量为3

非重复名字的数量统计

非重复的数量为2

我试图计算桶的数量,但似乎无论是重复还是非重复的都被计算了

GET mynames/_search
{
"aggs" : {
    "name_count" : {
        "terms" : {
            "field" : "firstname.keyword",
            "min_doc_count": 2
        }
    },
"count":{
  "cardinality": {
    "field": "firstname.keyword"
  }
}
}
1个回答

8

这里我使用了几个聚合。以下是我使用的列表,列表顺序是聚合执行的顺序。

对于重复项

对于非重复项

聚合查询:

POST <your_index_name>/_search
{  
   "size":0,
   "aggs":{  
      "duplicate_aggs":{  
         "terms":{  
            "field":"firstname.keyword",
            "min_doc_count":2
         }
      },
      "duplicate_bucketcount":{  
         "stats_bucket":{  
            "buckets_path":"duplicate_aggs._count"
         }
      },
      "nonduplicate_aggs":{  
         "terms":{  
            "field":"firstname.keyword"
         },
         "aggs":{  
            "equal_one":{  
               "bucket_selector":{  
                  "buckets_path":{  
                     "count":"_count"
                  },
                  "script":"params.count == 1"
               }
            }
         }
      },
      "nonduplicate_bucketcount":{  
         "sum_bucket":{  
            "buckets_path":"nonduplicate_aggs._count"
         }
      }
   }
}

响应

{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "duplicate_aggs": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "jane",
          "doc_count": 2
        },
        {
          "key": "joe",
          "doc_count": 2
        },
        {
          "key": "john",
          "doc_count": 2
        }
      ]
    },
    "nonduplicate_aggs": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "jack",
          "doc_count": 1
        },
        {
          "key": "steve",
          "doc_count": 1
        }
      ]
    },
    "duplicate_bucketcount": {
      "count": 3,
      "min": 2,
      "max": 2,
      "avg": 2,
      "sum": 6
    },
    "nonduplicate_bucketcount": {
      "value": 2
    }
  }
}

注意到上面的响应中,我们有一个名为duplicate_bucketcount.count的键,其值3就是显示重复键数目的桶计数。希望对您有所帮助!

你好,以上内容有帮助吗?它能够满足你的要求吗? - Kamal Kunjapur
有没有一种方法可以针对不只是一个单词的字段(例如 first name : "John"),而是整个句子/段落进行操作?例如:description : "Hello world!" - K-tan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接