I have written below ES Query for fetching data Using filter and Must: I can use only ElasticSearch V5 for writing queries.
{
"bool" : {
"must" : [
{
"term" : {
"Name" : {
"value" : "Self",
"boost" : 1.0
}
}
}
],
"filter" : [
{
"terms" : {
"Number" : [
"111",
"222"
]
}
}
]
}
}
I am getting output Like this with Duplicate value. I want to use aggregate query.
"hits": [
{ "Name": "Self","Number": 111},
{ "Name": "Self","Number": 222},
{ "Name": "Self","Number": 222},
{ "Name": "Self","Number": 111}
]
I want to select only Top 1 value for Each Number,So my Result out will be less.
O/P Should be:
"hits": [
{ "Name": "Self","Number": 222},
{ "Name": "Self","Number": 111}
]
You can use field collapsing to remove duplicate results
{
"query": {
"bool": {
"must": [
{
"term": {
"Name": {
"value": "Self",
"boost": 1.0
}
}
}
],
"filter": [
{
"terms": {
"Number": [
"111",
"222"
]
}
}
]
}
},
"collapse": {
"field": "Number"
}
}
Search Results will be
"hits": [
{
"_index": "68849562",
"_type": "_doc",
"_id": "1",
"_score": 0.10536051,
"_source": {
"Name": "Self",
"Number": 111
},
"fields": {
"Number": [
111
]
}
},
{
"_index": "68849562",
"_type": "_doc",
"_id": "2",
"_score": 0.10536051,
"_source": {
"Name": "Self",
"Number": 222
},
"fields": {
"Number": [
222
]
}
}
]