Here is some test data I have in an index:
{"name" : "almara" , "version" : "1" , "groups" : "blueHouse", "data1" : " value1"}
{"name" : "almara" , "version" : "2" , "groups" : "blueHouse", "data1" : " Something"}
{"name" : "almara" , "version" : "3" , "groups" : "blueHouse", "data1" : " Something"}
{"name" : "almara" , "version" : "4" , "groups" : "blueHouse", "data1" : " value1"}
{"name" : "donna" , "version" : "1" , "groups" : "redHouse", "data1" : " value1"}
{"name" : "donna" , "version" : "2" , "groups" : "redHouse", "data1" : " Something"}
{"name" : "donna" , "version" : "3" , "groups" : "redHouse", "data1" : " Something"}
{"name" : "donna" , "version" : "4" , "groups" : "redHouse", "data1" : " value1"}
{"name" : "maggie" , "version" : "1" , "groups" : "greenHouse", "data1" : " value1"}
{"name" : "maggie" , "version" : "2" , "groups" : "greenHouse", "data1" : " Something"}
{"name" : "maggie" , "version" : "3" , "groups" : "greenHouse", "data1" : " Something"}
{"name" : "maggie" , "version" : "4" , "groups" : "greenHouse", "data1" : " Something"}
I would like to have query that searches for "value1" in the "data1" field and have it return two matches:
{"name" : "donna" , "version" : "4" , "groups" : "redHouse", "data1" : " value1"}
{"name" : "almara" , "version" : "4" , "groups" : "blueHouse", "data1" : " value1"}
This is by first grouping all documents based on the "groups" field and then only considering the documents that have the highest version number in each of the grouped documents.
How would a query for that look like?
I have tried using collapse, but it collapses the results the search by searching for "value1" in all the documents and then collapsing.
{
"query": {
"match": {
"data1" :"value1"
}
},
"collapse": {
"field": "groups"
},
"sort": [
{
"version": {
"order": "desc"
}
}
]
}
Not able to figure this out using aggregations as well. Please assist.