Query within a query


(Sundar V) #1

I’m using parent-child relationship.
Before 2.0.0, the filtered query used to first filter the query and then run the second query. So this code was working

{
"query": {
"filtered": {
"filter": {
"term": {
"kind": "rock"
}
},
"query": {
"has_child": {
"type": "song",
"score_mode": "sum",
"query": {
"match_all": {}
}
}
}
}
}
}

However, now since filtered query is deprecated, I’m trying with the bool query and this one fails

{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"term": {
"kind": "rock"
}
}
},
"has_child": {
"type": "song",
"score_mode": "sum",
"query": {
"match_all": {}
}
}
}
}

How to write this?


(Shaunak Kashyap) #2

Try this instead:

{
  "query": {
    "bool": {
      "must": [
        {
          "has_child": {
            "type": "song",
            "score_mode": "sum", 
            "query": {
              "match_all": {}
            }
          }
        }
      ],
      "filter": {
        "term": {
          "kind": "rock"
        }
      }
    }
  }
}

(Sundar V) #3

Thanks @shaunak. That works!


(system) #4