Hi, is it possible to query elastic for documents matching a filter criteria, grouped by a field and also get any sibling documents not matching the filter criteria?
By siblings I mean documents having the same value on the attribute by which results are being grouped by.
Here 's the scenario.
Mapping
PUT my-index-000001
{
"mappings": {
"properties": {
"groupId": { "type": "keyword" },
"attr": { "type": "keyword" }
}
}
}
Documents
POST my-index-000001/_doc
{
"groupId": "a",
"attr": "1"
}
POST my-index-000001/_doc
{
"groupId": "a",
"attr": "2"
}
POST my-index-000001/_doc
{
"groupId": "a",
"attr": "3"
}
POST my-index-000001/_doc
{
"groupId": "b",
"attr": "5"
}
POST my-index-000001/_doc
{
"groupId": "c",
"attr": "1"
}
POST my-index-000001/_doc
{
"groupId": "c",
"attr": "3"
}
Query
POST /my-index-000001/_search
{
"query": {
"term": {
"attr": "3"
}
}
}
The result I' d like to get is something like this.
[{
"groupId": "a",
"docs": [{
"groupId": "a",
"attr": "1"
},{
"groupId": "a",
"attr": "2"
},{
"groupId": "a",
"attr": "3"
}
]
},{
"groupId": "c",
"docs": [{
"groupId": "c",
"attr": "1"
},{
"groupId": "c",
"attr": "3"
}
]
}
]