Elasticsearch具有逐字段文档计数的多字段聚合



在一种情况下,我必须搜索以'861'开头的电话号码和许可证号码。我需要获得字段匹配数据和字段文档总数。

为此,我使用了多字段聚合。

在输出中,我可以看到字段数据。但我也想看看场上的总计数。下面是搜索和聚合查询。

我的查询:

GET emp_details_new/_search 
{
"_source": [],
"size": 0,
"min_score": 1,
"query": {
"multi_match": {
"query": "861",
"fields": ["licence_num","phone"],
"type": "phrase_prefix"
}
},
"aggs": {
"licence_num": {
"terms": {
"field": "licence_num.keyword",
"include": "86.*"
}
},
"phone": {
"terms": {
"field": "phone.keyword",
"include": "86.*"
}
}
}
}

输出:在输出中,我只能获得逐字段的数据,也可以查找逐字段的计数。

{
"took" : 31,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"phone" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "8613789726",
"doc_count" : 1
},
{
"key" : "8617323318",
"doc_count" : 1
}
]
},
"licence_num" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "8616203799",
"doc_count" : 1
},
{
"key" : "8616829169",
"doc_count" : 1
}
]
}
}
}

您可以使用值计数聚合来进行字段计数。

{
"_source": [],
"size": 0,
"min_score": 1,
"query": {
"multi_match": {
"query": "861",
"fields": [
"licence_num",
"phone"
],
"type": "phrase_prefix"
}
},
"aggs": {
"licence_num": {
"terms": {
"field": "licence_num.keyword",
"include": "86.*"
}
},
"phone": {
"terms": {
"field": "phone.keyword",
"include": "86.*"
}
},
"licence_num_count": {
"value_count": {
"field": "licence_num.keyword"
}
},
"phone_count": {
"value_count": {
"field": "phone.keyword"
}
}
}
}

最新更新