我正在尝试在query_string上进行基数汇总。我是Elastic Search的新手,遇到了一些错误。
每个文档看起来像这样-
jpackage
我有查询来获取我需要的所有记录-
{
name: 'Jon Doe'
customId: 'x123yz'
prevAddressMap : {'NY':'2nd St', 'DC':'1st St', 'Chicago':'1st St' ... }
},
{ ...
},
.....
我想计算所有具有唯一名称的记录。因此,我尝试使用基数-
{'query':{'query_string':{'fields':['prevAddressMap.*'],'query': '1st St'}}}
我得到的错误是-
{
"query": {
"query_string": {
"fields": [
"prevAddressMap.*"
],
"query": "1st St"
}
},
"aggs": {
"distinct_count": {
"filter": {
"query": {
"query_string": {
"fields": [
"prevAddressMap.*"
],
"query": "1st St"
}
}
},
"aggs": {
"snapId_count": {
"cardinality": {
"field": "customId"
}
}
}
}
}
}
有人可以帮我吗?
答案 0 :(得分:0)
该错误是由于您应用filter aggregation的方式引起的。实施汇总的正确方法是:
"aggs": {
"distinct_count": {
"filter": {
"query_string": {
"fields": [
"prevAddressMap.*"
],
"query": "1st St"
}
},
"aggs": {
"snapId_count": {
"cardinality": {
"field": "customid"
}
}
}
}
}
第二,由于您已经使用查询来过滤文档,因此不再需要将其添加到聚合中。聚合在查询的结果文档中起作用。因此,以下总体查询将起作用:
{
"query": {
"query_string": {
"fields": [
"prevAddressMap.*"
],
"query": "1st St"
}
},
"aggs": {
"distinct_count": {
"cardinality": {
"field": "customId"
}
}
}
}