# Filtering nested documents

**URL:** https://discuss.elastic.co/t/filtering-nested-documents/110975
**Category:** Elasticsearch
**Created:** [December 10, 2017, 2:51pm UTC](https://discuss.elastic.co/t/filtering-nested-documents/110975 "2017-12-10T14:51:01Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jakobdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakobdo/32/23475_2.png) [@jakobdo](https://discuss.elastic.co/u/jakobdo)
#### Post date: [December 10, 2017, 2:51pm UTC](https://discuss.elastic.co/t/filtering-nested-documents/110975/1 "2017-12-10T14:51:01Z")

</div>

Hello,  
i have this data (simplified):

```
{
  id: 1,
  subs: [
    {
      date: "DUMMY_DATE_YESTERDAY"
    },{
      date: "DUMMY_DATE_TODAY"
    },{
      date: "DUMMY_DATE_TOMORROW"
    }
  ]
}

```

Is it possible to create a query, that will query:

id=1 and only show the "subs" where date \>= today ?

Hope this makes sense.

I have tried to look into nested query and nester filter, but as i read it, it will "only" query nested as a document, but still return the parent doc, if matched.

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 10, 2017, 3:07pm UTC](https://discuss.elastic.co/t/filtering-nested-documents/110975/2 "2017-12-10T15:07:16Z")

</div>

So the question you are asking to elasticsearch is "can you search for subs"?  
In that case, why not indexing `subs`?

Like:

```auto
PUT subs/doc/1
{
  "id": 1,
  "date": "DUMMY_DATE_YESTERDAY"
}
PUT subs/doc/2
{
  "id": 2,
  "date": "DUMMY_DATE_TODAY"
}

```

...

---

<div class="post-metadata">

### Author: ![jakobdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakobdo/32/23475_2.png) [@jakobdo](https://discuss.elastic.co/u/jakobdo)
#### Post date: [December 10, 2017, 4:33pm UTC](https://discuss.elastic.co/t/filtering-nested-documents/110975/3 "2017-12-10T16:33:33Z")

</div>

Hello David,  
no my question is:

Can i query the "document" by id = 1, and then i will get:

```
{ 
  id: 1, 
  subs: [ 
    { date: "DUMMY_DATE_YESTERDAY" },
    { date: "DUMMY_DATE_TODAY" },
    { date: "DUMMY_DATE_TOMORROW" }
  ]
}

```

But instead of looping all the subs afterwards, i will also "filter/query" the subs.  
As i said, i have simplified the dataset, i have data "outside" subs, that i also need.

So instead of this the data above, i would "like" if i could get this:

```
{ 
  id: 1, 
  subs: [ 
    { date: "DUMMY_DATE_TODAY" },
    { date: "DUMMY_DATE_TOMORROW" }
  ]
}

```

Cause they "match" id=1 and date \>= TODAY

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 10, 2017, 5:11pm UTC](https://discuss.elastic.co/t/filtering-nested-documents/110975/4 "2017-12-10T17:11:14Z")

</div>

I understood your question.

I was just telling you that may be the way you are modelling your data might be incorrect.

But, if you really want to do it your way, may be inner hits could help: [https://www.elastic.co/guide/en/elasticsearch/reference/6.0/search-request-inner-hits.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.0/search-request-inner-hits.html)

---

<div class="post-metadata">

### Author: ![jakobdo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jakobdo/32/23475_2.png) [@jakobdo](https://discuss.elastic.co/u/jakobdo)
#### Post date: [December 11, 2017, 12:18pm UTC](https://discuss.elastic.co/t/filtering-nested-documents/110975/5 "2017-12-11T12:18:33Z")

</div>

Hello David,  
it is not my data. 🙂  
I'm just tried to access some data through the rest api.  
So i cant change the data.  
The data is the way it is.

And the data is running on ES 1.7.6

---

<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: [January 8, 2018, 12:19pm UTC](https://discuss.elastic.co/t/filtering-nested-documents/110975/6 "2018-01-08T12:19:02Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
