Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在elasticsearch中聚合数据后的唯一键 - Fatal编程技术网

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

在elasticsearch中聚合数据后的唯一键,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我有以下文件在我的索引 {"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,"val

我有以下文件在我的索引

{"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}
如何在弹性搜索中构建查询(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 
谁能帮帮我吗?
我只需要通过聚合进行操作。
谢谢

你可以得到更多的例子

这应该可以做到(尽管JSON并不完全是您所期望的)

  • 首先,我们使用常规术语聚合将所有文档划分为多个存储桶
  • 对于每个bucket,我们使用基数聚合来确定每个bucket中有多少个不同的valueid
结果如下(结果是,我们有6个不同的ValueID用于键192,而不是5):

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

      }
    }
  }
}
POST test/_search
{
   "size": 0,
   "aggs": {
      "refs": {
         "terms": {
            "field": "Ref"
         },
         "aggs": {
            "valueIdCount": {
               "cardinality": {
                  "field": "valueId"
               }
            }
         }
      }
   }
}
{
   [...]
   "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
               }
            }
         ]
      }
   }
}