In a scenario, I have to search for phone numbers and license numbers that start with '861'. I need to get field-wise matching data and field-wise total document count.
For that, I used multiple field aggregation.
In the output, I can see the field-wise data. But I also want to see the field-wise total count. Below is the search and aggregation query.
My Query:
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.*"
}
}
}
}
Output: In the output, I can get only field wise data, also looking for field-wise count.
{
"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
}
]
}
}
}
You can use Value Count aggregation for field-wise total count.
{
"_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"
}
}
}
}