Has Child Query on Nested Object field


(Brent Evans) #1

Hoping someone can help here and tell me if the query I'm attempting to run is possible/supported by Elasticsearch (I've read through the docs and forums and been unable to find any answer).

I have a child document (Child) that has a parent document set on it, with the child document having a number of nested fields that require nested queries to be run. I wish to run a has child query on the parent document, with the inner query on the child document being against a nested field. For example:

  "query" : {
    "has_child" : {
      "query" : {
        "bool" : {
          "must" : {
            "bool" : {
              "must" : {
                "nested" : {
                  "query" : {
                    "bool" : {
                      "must" : {
                        "filtered" : {
                          "query" : {
                            "match_all" : { }
                          },
                          "filter" : {
                            "term" : {
                              "Owner.Type" : "User",
                              "_cache" : true
                            }
                          }
                        }
                      }
                    }
                  },
                  "path" : "Owner"
                }
              }
            }
          }
        }
      },
      "child_type" : "Child"
    }
  }

This should return valid results based on the documents in Elasticsearch, however the query successfully runs with no results returned.

Any suggestions?

Thanks in advance


(Dan Tuffery) #2

I haven't tested it but your query should look something like this:

{
    "query": {
        "filtered": {
            "query": {
                "match_all": {}
            },
            "filter": {
                "has_child": {
                    "type": "Child",
                    "query": {
                        "nested": {
                            "path": "Owner",
                            "term": {
                                "Owner.Type": "User",
                                "_cache": true
                            }
                        }
                    }
                }
            }
        }
    }
}

(Brent Evans) #3

Just tried using the has_child filter in place of the has_child query and it does indeed work.

Thanks for the help.


(system) #4