# May Elasticsearch nested query return only matched nested documents for nested fields?

**URL:** https://discuss.elastic.co/t/may-elasticsearch-nested-query-return-only-matched-nested-documents-for-nested-fields/950
**Category:** Elasticsearch
**Created:** [May 20, 2015, 8:15am UTC](https://discuss.elastic.co/t/may-elasticsearch-nested-query-return-only-matched-nested-documents-for-nested-fields/950 "2015-05-20T08:15:01Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jffifa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jffifa/32/44820_2.png) [@jffifa](https://discuss.elastic.co/u/jffifa)
#### Post date: [May 20, 2015, 8:15am UTC](https://discuss.elastic.co/t/may-elasticsearch-nested-query-return-only-matched-nested-documents-for-nested-fields/950/1 "2015-05-20T08:15:01Z")

</div>

I'm new to Elasticsearch, and come up with a question that whether Elasticsearch nested query may return only matched nested documents for nested fields or not.

For Example I have a type named `blog` with a nested field named `comments`

```
{
  "id": 1,
  ...
  "comments":[
    {"content":"Michael is a basketball player"},
    {"content":"David is a soccer player"}
  ]
}
{
  "id": 2,
  ...
  "comments":[
    {"content":"Wayne is a soccer player"},
    {"content":"Steven is also a soccer player"},
  ]
}

```

and the nested query

```
{"query":{
  "nested":{
    "path":"comments",
    "query":{"match":{"comments.content":"soccer"}}
  }
}

```

What I need is to search blog posts with comments which mentioned "soccer", with the count of comments that matched "soccer" (in the example it counts 1, since another comment just mentioned "basketball") for each blog post.

```
{"hits":[
  {
    "id":1,
    ...
    "count_for_comments_that_matches_query":1,
  },
  {
    "id":2,
    ...
    "count_for_comments_that_matches_query":2,
  }
]}

```

However it seems Elasticsearch always return the full document, so how could I achieve it, or I couldn't?

---

<div class="post-metadata">

### Author: ![Camilo\_Sierra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/camilo_sierra/32/14397_2.png) [@Camilo\_Sierra](https://discuss.elastic.co/u/Camilo_Sierra)
#### Post date: [May 20, 2015, 8:21am UTC](https://discuss.elastic.co/t/may-elasticsearch-nested-query-return-only-matched-nested-documents-for-nested-fields/950/2 "2015-05-20T08:21:16Z")

</div>

hello @jffifa, the only way that i know is the [top hits aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/1.5/search-aggregations-metrics-top-hits-aggregation.html#_top_hits_support_in_a_nested_or_reverse_nested_aggregator) this aggregation will return only nested hits !

---

<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 6, 2017, 12:13am UTC](https://discuss.elastic.co/t/may-elasticsearch-nested-query-return-only-matched-nested-documents-for-nested-fields/950/3 "2017-07-06T00:13:09Z")

</div>


