# Filter for no nested documents

**URL:** <https://discuss.elastic.co/t/filter-for-no-nested-documents/8972>\
**Category:** Elasticsearch\
**Created:** [September 10, 2012, 8:56am UTC](https://discuss.elastic.co/t/filter-for-no-nested-documents/8972 "2012-09-10T08:56:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lisinge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lisinge/32/2729_2.png) [@Lisinge](https://discuss.elastic.co/u/Lisinge)\
**Post date:** [September 10, 2012, 8:56am UTC](https://discuss.elastic.co/t/filter-for-no-nested-documents/8972/1 "2012-09-10T08:56:27Z")

</div>

I have a mapping where we have N nested documents. Is there a way to filter  
for documents which have no nested documents?  
I've tried with the missing[http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.html](http://www.elasticsearch.org/guide/reference/query-dsl/missing-filter.html) filter  
without success.

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 10, 2012, 9:11am UTC](https://discuss.elastic.co/t/filter-for-no-nested-documents/8972/2 "2012-09-10T09:11:21Z")

</div>

I think if you wrap the nested filter in a not filter, you get what you need.

not filter:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Martijn

On 10 September 2012 10:56, Lisinge [lisinge@rabble.se](mailto:lisinge@rabble.se) wrote:

> I have a mapping where we have N nested documents. Is there a way to filter  
> for documents which have no nested documents?  
> I've tried with the missing filter without success.
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Lisinge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lisinge/32/2729_2.png) [@Lisinge](https://discuss.elastic.co/u/Lisinge)\
**Post date:** [September 10, 2012, 9:27am UTC](https://discuss.elastic.co/t/filter-for-no-nested-documents/8972/3 "2012-09-10T09:27:22Z")

</div>

Thanks for your reply Martijn.  
I thought about that too. Could you give me an example of what you mean?

Den måndagen den 10:e september 2012 kl. 11:11:43 UTC+2 skrev Martijn v  
Groningen:

> I think if you wrap the nested filter in a not filter, you get what you  
> need.
> 
> not filter:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/not-filter.html)
> 
> Martijn
> 
> On 10 September 2012 10:56, Lisinge \<lis...@rabble.se \<javascript:\>\>  
> wrote:
> 
> > I have a mapping where we have N nested documents. Is there a way to  
> > filter  
> > for documents which have no nested documents?  
> > I've tried with the missing filter without success.
> > 
> > --
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Lisinge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lisinge/32/2729_2.png) [@Lisinge](https://discuss.elastic.co/u/Lisinge)\
**Post date:** [September 10, 2012, 9:29am UTC](https://discuss.elastic.co/t/filter-for-no-nested-documents/8972/4 "2012-09-10T09:29:07Z")

</div>

Here is also a simple example off the mapping that is used:

{

> "created\_at": {  
> "type": "date"  
> },  
> "checkouts": {  
> "type": "nested",  
> "properties": {  
> "location": {  
> "type": "geo\_point"  
> },  
> "created\_at": {  
> "type": "date"  
> }  
> }  
> }

Den måndagen den 10:e september 2012 kl. 11:11:43 UTC+2 skrev Martijn v  
Groningen:

> I think if you wrap the nested filter in a not filter, you get what you  
> need.
> 
> not filter:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/not-filter.html)
> 
> Martijn
> 
> On 10 September 2012 10:56, Lisinge \<lis...@rabble.se \<javascript:\>\>  
> wrote:
> 
> > I have a mapping where we have N nested documents. Is there a way to  
> > filter  
> > for documents which have no nested documents?  
> > I've tried with the missing filter without success.
> > 
> > --
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 10, 2012, 9:37am UTC](https://discuss.elastic.co/t/filter-for-no-nested-documents/8972/5 "2012-09-10T09:37:57Z")

</div>

An example using the filtered query  
([Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)):  
{  
"filtered" : {  
"query" : { "match\_all" : {} },  
"filter" : {  
"not" : {  
"filter" : {  
"nested" : {  
"path" : "checkouts",  
"query" : {  
"match\_all" : {}  
}  
}  
},  
"\_cache" : true  
}  
}  
}  
}

The above query should return all docs that don't have nested documents.

On 10 September 2012 11:29, Lisinge [lisinge@rabble.se](mailto:lisinge@rabble.se) wrote:

> Here is also a simple example off the mapping that is used:
> 
> > {  
> > "created\_at": {  
> > "type": "date"  
> > },  
> > "checkouts": {  
> > "type": "nested",  
> > "properties": {  
> > "location": {  
> > "type": "geo\_point"  
> > },  
> > "created\_at": {  
> > "type": "date"  
> > }  
> > }  
> > }
> 
> Den måndagen den 10:e september 2012 kl. 11:11:43 UTC+2 skrev Martijn v  
> Groningen:
> 
> > I think if you wrap the nested filter in a not filter, you get what you  
> > need.
> > 
> > not filter:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/not-filter.html)
> > 
> > Martijn
> > 
> > On 10 September 2012 10:56, Lisinge [lis...@rabble.se](mailto:lis...@rabble.se) wrote:
> > 
> > > I have a mapping where we have N nested documents. Is there a way to  
> > > filter  
> > > for documents which have no nested documents?  
> > > I've tried with the missing filter without success.
> > > 
> > > --
> > 
> > --  
> > Met vriendelijke groet,
> > 
> > Martijn van Groningen
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Lisinge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lisinge/32/2729_2.png) [@Lisinge](https://discuss.elastic.co/u/Lisinge)\
**Post date:** [September 10, 2012, 10:53am UTC](https://discuss.elastic.co/t/filter-for-no-nested-documents/8972/6 "2012-09-10T10:53:31Z")

</div>

Thanks Martijn.

I ended up with this query:  
{  
"filter": {  
"not": {  
"nested": {  
"path": "checkouts",  
"filter": {  
"match\_all": {}  
}  
}  
}  
}  
}

But i believe the "match\_all" is kinda misleading, is there some other way  
to do it?

Den måndagen den 10:e september 2012 kl. 11:38:20 UTC+2 skrev Martijn v  
Groningen:

> An example using the filtered query  
> (  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)):
> 
> {  
> "filtered" : {  
> "query" : { "match\_all" : {} },  
> "filter" : {  
> "not" : {  
> "filter" : {  
> "nested" : {  
> "path" : "checkouts",  
> "query" : {  
> "match\_all" : {}  
> }  
> }  
> },  
> "\_cache" : true  
> }  
> }  
> }  
> }
> 
> The above query should return all docs that don't have nested documents.
> 
> On 10 September 2012 11:29, Lisinge \<lis...@rabble.se \<javascript:\>\>  
> wrote:
> 
> > Here is also a simple example off the mapping that is used:
> > 
> > > {  
> > > "created\_at": {  
> > > "type": "date"  
> > > },  
> > > "checkouts": {  
> > > "type": "nested",  
> > > "properties": {  
> > > "location": {  
> > > "type": "geo\_point"  
> > > },  
> > > "created\_at": {  
> > > "type": "date"  
> > > }  
> > > }  
> > > }
> > 
> > Den måndagen den 10:e september 2012 kl. 11:11:43 UTC+2 skrev Martijn v  
> > Groningen:
> > 
> > > I think if you wrap the nested filter in a not filter, you get what you  
> > > need.
> > > 
> > > not filter:  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/not-filter.html)
> > > 
> > > Martijn
> > > 
> > > On 10 September 2012 10:56, Lisinge [lis...@rabble.se](mailto:lis...@rabble.se) wrote:
> > > 
> > > > I have a mapping where we have N nested documents. Is there a way to  
> > > > filter  
> > > > for documents which have no nested documents?  
> > > > I've tried with the missing filter without success.
> > > > 
> > > > --
> > > 
> > > --  
> > > Met vriendelijke groet,
> > > 
> > > Martijn van Groningen
> > 
> > --
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 10, 2012, 12:42pm UTC](https://discuss.elastic.co/t/filter-for-no-nested-documents/8972/7 "2012-09-10T12:42:53Z")

</div>

> But i believe the "match\_all" is kinda misleading, is there some other way  
> to do it?  
> What do you find misleading about the "match\_all"? The match all  
> basically selects all documents on the nested checkouts level. I think  
> this is the best way to do this.

--

---

<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, 3:13am UTC](https://discuss.elastic.co/t/filter-for-no-nested-documents/8972/8 "2017-07-06T03:13:22Z")

</div>


