Skip to content

min_children does not play nice with boolean queries #21224

Closed
@marshall007

Description

@marshall007

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:

{
  "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
            }
          }
        }
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    :Search/SearchSearch-related issues that do not fall into other categories>enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions