聚合elasticsearch中的数据后的唯一键

时间:2016-09-27 16:21:21

标签: elasticsearch elasticsearch-plugin

我的指标中有以下文件。

{"id":"1","Ref":192,"valueId":596,"locationId":45}
{"id":"21","Ref":192,"valueId":596,"locationId":323}
{"id":"31","Ref":192,"valueId":5596,"locationId":5435}
{"id":"41","Ref":192,"valueId":5596,"locationId":535}
{"id":"51","Ref":192,"valueId":5996,"locationId":78}
{"id":"61","Ref":192,"valueId":5996,"locationId":6565}
{"id":"71","Ref":192,"valueId":5196,"locationId":868}
{"id":"81","Ref":192,"valueId":5296,"locationId":68687}
{"id":"91","Ref":192,"valueId":5296,"locationId":6836}
{"id":"101","Ref":192,"valueId":5296,"locationId":96}
{"id":"111","Ref":192,"valueId":5396,"locationId":56}

{"id":"121","Ref":576,"valueId":5396,"locationId":5}
{"id":"131","Ref":576,"valueId":5496,"locationId":8}
{"id":"141","Ref":576,"valueId":5496,"locationId":5356}
{"id":"151","Ref":576,"valueId":5496,"locationId":896}
{"id":"261","Ref":576,"valueId":5896,"locationId":99}
{"id":"271","Ref":576,"valueId":5896,"locationId":8589}
{"id":"671","Ref":576,"valueId":5896,"locationId":999}
{"id":"431","Ref":576,"valueId":5896,"locationId":3565868}
{"id":"241","Ref":576,"valueId":5896,"locationId":9998}

如何在弹性搜索中建立一个querty(aggreagtions),使其返回结果如下

{
    "key" : 192, "Count" : 5,
    "key" : 576, "Count" : 3
}

Count 5 for the key 192 implies number of distinct valueIds for the "Ref"= 192,
Count 3 for the key 576 implies number of distinct valueIds for the "Ref" =576 

有人可以帮帮我..?
我只需要通过聚合 感谢

2 个答案:

答案 0 :(得分:1)

POST test/_search
{
   "size": 0,
   "aggs": {
      "refs": {
         "terms": {
            "field": "Ref"
         },
         "aggs": {
            "valueIdCount": {
               "cardinality": {
                  "field": "valueId"
               }
            }
         }
      }
   }
}

这应该可以解决问题(尽管JSON并不完全符合您的预期)。

  • 首先,我们使用常规术语聚合将所有文档划分为存储桶。
  • 对于每个存储桶,我们使用基数聚合来查找每个存储桶中找到多少个不同的valueIds。

这是结果(事实证明,对于密钥192,我们有6个不同的valueIds,而不是5):

{
   [...]
   "aggregations": {
      "refs": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": 192,
               "doc_count": 11,
               "valueIdCount": {
                  "value": 6
               }
            },
            {
               "key": 576,
               "doc_count": 9,
               "valueIdCount": {
                  "value": 3
               }
            }
         ]
      }
   }
}

答案 1 :(得分:0)

POST your_index/_search
{
  "size": 0,
  "aggs": {
    "keys": {
      "terms": {
        "field": "Ref"

      }
    }
  }
}

您可以获得更多示例here