在 elasticsearch 中聚合数据后的唯一键
Unique keys after aggregating the data in elasticsearch
我的索引中有以下文件。
{"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}
如何在弹性搜索中构建查询(聚合),使其return结果如下
{
"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
有人能帮帮我吗..?
我只需要通过聚合。
谢谢
POST your_index/_search
{
"size": 0,
"aggs": {
"keys": {
"terms": {
"field": "Ref"
}
}
}
}
您可以获得更多示例here
POST test/_search
{
"size": 0,
"aggs": {
"refs": {
"terms": {
"field": "Ref"
},
"aggs": {
"valueIdCount": {
"cardinality": {
"field": "valueId"
}
}
}
}
}
}
这应该可以解决问题(尽管 JSON 与您预期的不完全一样)。
- 首先,我们使用常规的 Terms Aggregation 将所有文档划分到桶中。
- 对于每个桶,我们使用基数聚合来找出我们在每个桶中找到了多少个不同的 valueId。
结果如下(事实证明,键 192 有 6 个不同的 valueId,而不是 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
}
}
]
}
}
}
我的索引中有以下文件。
{"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}
如何在弹性搜索中构建查询(聚合),使其return结果如下
{
"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
有人能帮帮我吗..?
我只需要通过聚合。
谢谢
POST your_index/_search
{
"size": 0,
"aggs": {
"keys": {
"terms": {
"field": "Ref"
}
}
}
}
您可以获得更多示例here
POST test/_search
{
"size": 0,
"aggs": {
"refs": {
"terms": {
"field": "Ref"
},
"aggs": {
"valueIdCount": {
"cardinality": {
"field": "valueId"
}
}
}
}
}
}
这应该可以解决问题(尽管 JSON 与您预期的不完全一样)。
- 首先,我们使用常规的 Terms Aggregation 将所有文档划分到桶中。
- 对于每个桶,我们使用基数聚合来找出我们在每个桶中找到了多少个不同的 valueId。
结果如下(事实证明,键 192 有 6 个不同的 valueId,而不是 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
}
}
]
}
}
}