I believe the expected behavior when has_child is within a must expression is that when zero children are returned, the expression evaluates as "falsey", which makes sense. However, if you set min_children: 0 intuitively I would expect this expression to now evaluate as "truthy" regardless of the number of returned results.
In our case, it makes a lot more sense for us to dynamically adjust the min_children parameter rather than dynamically changing the entire boolean query expression.
Here's an example query demonstrating the issue. We're attempting to find a parent or child document by ID and return both the parent and child. The query fails to match when we find a parent with no children despite min_children: 0:
Details
{
"size": 1,
"query": {
"bool": {
"should": {
"terms": {
"_id": [ "9b6ca518-39be-48e8-b885-bee6fd7ea207" ]
}
},
"must": {
"has_child": {
"type": "lab_tests",
"min_children": 0,
"query": {
"bool": {
"should": [
{
"terms": {
"_id": [ "9b6ca518-39be-48e8-b885-bee6fd7ea207" ]
}
},
{
"terms": {
"_parent": [ "9b6ca518-39be-48e8-b885-bee6fd7ea207" ]
}
},
{
"terms": {
"status": [ "active", "deprecated" ]
}
}
],
"minimum_should_match": 1
}
}
}
}
}
}
}
I believe the expected behavior when
has_childis within amustexpression is that when zero children are returned, the expression evaluates as "falsey", which makes sense. However, if you setmin_children: 0intuitively I would expect this expression to now evaluate as "truthy" regardless of the number of returned results.In our case, it makes a lot more sense for us to dynamically adjust the
min_childrenparameter rather than dynamically changing the entire boolean query expression.Here's an example query demonstrating the issue. We're attempting to find a parent or child document by ID and return both the parent and child. The query fails to match when we find a parent with no children despite
min_children: 0:Details
{ "size": 1, "query": { "bool": { "should": { "terms": { "_id": [ "9b6ca518-39be-48e8-b885-bee6fd7ea207" ] } }, "must": { "has_child": { "type": "lab_tests", "min_children": 0, "query": { "bool": { "should": [ { "terms": { "_id": [ "9b6ca518-39be-48e8-b885-bee6fd7ea207" ] } }, { "terms": { "_parent": [ "9b6ca518-39be-48e8-b885-bee6fd7ea207" ] } }, { "terms": { "status": [ "active", "deprecated" ] } } ], "minimum_should_match": 1 } } } } } } }