I am trying to fine tune the query on a nested collection. The nested collection should match all the criteria and the size of the collection. I created a sample data set to the mimic my use case.
Index
{
"mappings": {
"properties": {
"family": {
"type": "nested",
"properties": {
"name": {
"type": "text"
},
"child": {
"type": "nested",
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "short"
}
}
}
}
}
}
}
}
Data
"hits": [
{
"_index": "family",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"family": {
"name": "Smith",
"child": [
{
"name": "Alpha",
"age": 10
},
{
"name": "Beta",
"age": 15
}
]
}
}
},
{
"_index": "family",
"_type": "_doc",
"_id": "3",
"_score": 1.0,
"_source": {
"family": {
"name": "Williamson",
"child": [
{
"name": "Epsilon",
"age": 10
}
]
}
}
},
{
"_index": "family",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"family": {
"name": "Johnson",
"child": [
{
"name": "Gamma",
"age": 5
},
{
"name": "Delta",
"age": 10
},
{
"name": "Zeta",
"age": 15
}
]
}
}
}
]
Query
{
"query": {
"nested": {
"path": "family",
"query": {
"nested": {
"path": "family.child",
"query": {
"bool": {
"should": [
{
"match": { "family.child.age": 10 }
},
{
"match": { "family.child.age": 15 }
}
]
}
}
}
}
}
}
}
The intent is to fetch the families that have at least two children with ages 10 and 15. The above query returns the Williamson family which is undesirable. I don't know how to express the criteria so that is excluded. (New to ES as well). Any help would be greatly appreciated.