# Convert top\_children Query DSL to has\_child query for ES 2.x

**URL:** <https://discuss.elastic.co/t/convert-top-children-query-dsl-to-has-child-query-for-es-2-x/41226>\
**Category:** Elasticsearch\
**Created:** [February 8, 2016, 9:44pm UTC](https://discuss.elastic.co/t/convert-top-children-query-dsl-to-has-child-query-for-es-2-x/41226 "2016-02-08T21:44:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![apanimesh061](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apanimesh061/32/927_2.png) [@apanimesh061](https://discuss.elastic.co/u/apanimesh061)\
**Post date:** [February 8, 2016, 9:44pm UTC](https://discuss.elastic.co/t/convert-top-children-query-dsl-to-has-child-query-for-es-2-x/41226/1 "2016-02-08T21:44:48Z")

</div>

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"
          }
        }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:18pm UTC](https://discuss.elastic.co/t/convert-top-children-query-dsl-to-has-child-query-for-es-2-x/41226/2 "2017-07-05T23:18:10Z")

</div>


