2016-09-27 93 views
1

我在我的indice中有以下文档。聚合弹性搜索中的数据后的唯一键

{"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 

能有人请帮我..?
我只需要通过聚合。
感谢

回答

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

这应该做的伎俩(虽然JSON是你预期的不完全是)。

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

这里的结果(事实证明,我们有6个不同valueIds重点192,而不是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 
       } 
      } 
     ] 
     } 
    } 
} 
0
POST your_index/_search 
{ 
    "size": 0, 
    "aggs": { 
    "keys": { 
     "terms": { 
     "field": "Ref" 

     } 
    } 
    } 
} 

你可以得到更多的例子here