2
votes
{
  "query":
  {
    "bool":
    {
      "should": [
       {   
          "has_child": {
            "inner_hits":{},
            "type": "emp",
            "query": {
                "match": {
                  "name": "xyz"
                }
              }
            }
          },
          {
            "bool":
            {
              "must":
              [
                  {
                    "match" : { "name" : "xyz" }
                  },
                  {
                    "match" : { "_type" : "emps" }
                  }
              ],
              "must_not": [
                {
                  "has_child": {
                    "type": "emp",
                    "query": {
                      "exists":
                            {
                              "field": "name"
                            }
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
}
1

1 Answers

2
votes

No, unfortunately.

Actually, yes. I was wrong. OP posted his question on Bodybuilder's github and received the following answer:

It's a bit quirky but this is how I'd write it:

var inner = bodybuilder()
    .query('match', 'name', 'xyz')  
    .query('match', '_type', 'emps')    
    .notQuery('has_child', {type: 'emp'}, (q) => {
        return q.query('exists', 'field', 'name')
    })
    .build()

bodybuilder()
    .orQuery('bool', inner.query.bool)
        .orQuery('has_child', {inner_hits: {}, type: 'emp'}, (q) => {
        return q.query('match', 'name', 'xyz')  
    })
    .build()

Something to watch out for: the order of the orQuerys matters here, it doesn't work if these are switched, which isn't ideal. I think that's because of how the bool clauses are being merged. Let me know is this isn't working for you.