Convert top_children Query DSL to has_child query for ES 2.x


(apanimesh061) #1

Following is the old version of the Query DSL (Elasticsearch 1.4) that I have been using:

PUT /new_index/_search
{
  "query" : {
    "top_children" : {
      "query" : {
        "query_string" : {
          "query" : "some random text"
        }
      },
      "type" : "offspring",
      "score" : "sum"
    }
  }
}

which I define in Java by:

SearchRequestBuilder request = client.prepareSearch("new_index").setQuery(
        topChildrenQuery("offspring", queryString("some random text")).score("sum")
);

In the documentation for Elasticsearch 2.x, it is mentioned that top_children has been removed and we have to use has_child. So, is the following a correct version for query DSL for ES 2.x ?

{
  "query" : {
    "has_child" : {
      "query" : {
        "query_string" : {
          "query" : "some random text"
        }
      },
      "child_type" : "offspring",
      "score_mode" : "sum"
    }
  }
}

The mappings of the new_index is:

{
  "new_index": {
    "mappings": {
      "offspring": {
        "_parent": {
          "type": "father"
        },
        "_routing": {
          "required": true
        },
        "properties": {
          "attached_text": {
            "type": "string"
          }
        }
      },
      "father": {
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

(system) #2