title: Elastic search has_child query
body: <p>We have a parent-child (one to many) relation in elastic search, and we want to check for all parent objects where it's child object attribute(child_attr) has any value in it.</p>
  
  <p>we are generating json-queries as below:</p>
  
  <p>1) For Has value condition.</p>
  
  <pre><code>{
                  "has_child" : {
                    "query" : {
                      "filtered" : {
                        "query" : {
                          "match_all" : { }
                        },
                        "filter" : {
                          "and" : {
                            "filters" : [ {
                              "exists" : {
                                "field" : "child_attr"
                              }
                            }, {
                              "not" : {
                                "filter" : {
                                  "term" : {
                                    "child_attr" : ""
                                  }
                                }
                              }
                            } ]
                          }
                        }
                      }
                    },
                    "type" : "child"
                  }
                }
  </code></pre>
  
  <p>2) For Has No Value Condition</p>
  
  <pre><code>{
                  "has_child" : {
                    "query" : {
                      "filtered" : {
                        "query" : {
                          "match_all" : { }
                        },
                        "filter" : {
                          "or" : {
                            "filters" : [ {
                              "missing" : {
                                "field" : "child_attr"
                              }
                            }, {
                              "term" : {
                                "child_attr" : ""
                              }
                            } ]
                          }
                        }
                      }
                    },
                    "type" : "child"
                  }
                } 
  </code></pre>
  
  <p>These queries are returning only those parent objects where either all child objects have some value or all child objects have no value the searched attribute.</p>
  
  <p>It doesn't return anything where this condition is met partially which covers majority of data.</p>
  
  <p>I have also toyed with keyword analyzer to index this child_attribute but no joy.</p>
  
  <p>Look forward to your expert suggestions please.</p>