# Is it possible to combine filters and nested filters?

**URL:** <https://discuss.elastic.co/t/is-it-possible-to-combine-filters-and-nested-filters/16627>\
**Category:** Elasticsearch\
**Created:** [March 26, 2014, 8:41pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-filters-and-nested-filters/16627 "2014-03-26T20:41:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmarz1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gmarz1/32/1695_2.png) [@gmarz1](https://discuss.elastic.co/u/gmarz1)\
**Post date:** [March 26, 2014, 8:41pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-filters-and-nested-filters/16627/1 "2014-03-26T20:41:34Z")

</div>

I have a document that contains nested documents. I know it is possible to  
combine multiple filters on fields in the root document, but I haven't  
figured out how to combine those filters with nested filters.

Take this query for example:

{  
"from": 0,  
"size": 20,  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"company\_id": 123  
}  
},  
{  
"nested": {  
"filter": {  
"term": {  
"categories.name": {  
"value": "Engineering"  
}  
}  
},  
"path": "categories"  
}  
}  
]  
}  
}  
}  
}  
}

company\_id is a field at the root of the document. categories.name is a  
field at the root of a nested category document. The following query  
returns 0 results, although there are documents that match this criteria.  
Is it not possible to combine filters like this, or am I just doing it  
wrong?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/39143c3b-f989-4f60-bce9-a59a3b857924%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/39143c3b-f989-4f60-bce9-a59a3b857924%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 26, 2014, 8:49pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-filters-and-nested-filters/16627/2 "2014-03-26T20:49:52Z")

</div>

Should work fine. Since you are using a term filter which is an exact  
case-sensitive match, is it possible that your categories.name field is  
indexed differently (like standard) that would make term filter not match?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/052801c4-7b6d-42a3-a40d-f841d09fc94b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/052801c4-7b6d-42a3-a40d-f841d09fc94b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![gmarz1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gmarz1/32/1695_2.png) [@gmarz1](https://discuss.elastic.co/u/gmarz1)\
**Post date:** [March 26, 2014, 9:10pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-filters-and-nested-filters/16627/3 "2014-03-26T21:10:06Z")

</div>

Hey Binh,

The field is indexed the same way I am searching on it, and should be an  
exact match. The term filter matches when I remove the company\_id filter,  
and the company\_id filter works when I remove the categories.name nested  
filter. However, when combined, it does not return results.

On Wednesday, March 26, 2014 4:49:52 PM UTC-4, Binh Ly wrote:

> Should work fine. Since you are using a term filter which is an exact  
> case-sensitive match, is it possible that your categories.name field is  
> indexed differently (like standard) that would make term filter not match?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/fd63952d-ac7d-4985-91d4-e42728be6b62%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/fd63952d-ac7d-4985-91d4-e42728be6b62%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 26, 2014, 9:22pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-filters-and-nested-filters/16627/4 "2014-03-26T21:22:44Z")

</div>

Hmmm, not sure. I just tried this and it works for me (ES 1.1):

1. PUT [http://localhost:9200/n](http://localhost:9200/n)  
{  
"mappings": {  
"doc": {  
"properties": {  
"n1": {  
"type": "nested",  
"properties": {  
}  
}  
}  
}  
}  
}

1. POST [http://localhost:9200/n/doc](http://localhost:9200/n/doc)  
{ "company\_id": 123, "n1": { "name": "engineering" } }

2. POST [http://localhost:9200/n/\_search](http://localhost:9200/n/_search)  
{  
"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"company\_id": 123  
}  
},  
{  
"nested": {  
"filter": {  
"term": {  
"n1.name": {  
"value": "engineering"  
}  
}  
},  
"path": "n1"  
}  
}  
]  
}  
}  
}  
}  
}

And it returned the document fine. Maybe you can duplicate my steps and see  
if it works for you.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/dcad6e92-e343-4d17-97b6-478332e5c808%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/dcad6e92-e343-4d17-97b6-478332e5c808%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![gmarz1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gmarz1/32/1695_2.png) [@gmarz1](https://discuss.elastic.co/u/gmarz1)\
**Post date:** [March 27, 2014, 3:33pm UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-filters-and-nested-filters/16627/5 "2014-03-27T15:33:29Z")

</div>

Hey Binh,

I tried your example and it does in fact work. I realized my query was  
just wrong...really stupid mistake. Thanks for your help!

On Wednesday, March 26, 2014 5:22:44 PM UTC-4, Binh Ly wrote:

> Hmmm, not sure. I just tried this and it works for me (ES 1.1):
> 
> 1. PUT [http://localhost:9200/n](http://localhost:9200/n)  
> {  
> "mappings": {  
> "doc": {  
> "properties": {  
> "n1": {  
> "type": "nested",  
> "properties": {  
> }  
> }  
> }  
> }  
> }  
> }
> 
> 1. POST [http://localhost:9200/n/doc](http://localhost:9200/n/doc)  
> { "company\_id": 123, "n1": { "name": "engineering" } }
> 
> 2. POST [http://localhost:9200/n/\_search](http://localhost:9200/n/_search)  
> {  
> "query": {  
> "filtered": {  
> "query": {  
> "match\_all": {}  
> },  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "company\_id": 123  
> }  
> },  
> {  
> "nested": {  
> "filter": {  
> "term": {  
> "n1.name": {  
> "value": "engineering"  
> }  
> }  
> },  
> "path": "n1"  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }
> 
> And it returned the document fine. Maybe you can duplicate my steps and  
> see if it works for you.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/087014f1-0344-4cbd-8d3d-af370ae79ee6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/087014f1-0344-4cbd-8d3d-af370ae79ee6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:40am UTC](https://discuss.elastic.co/t/is-it-possible-to-combine-filters-and-nested-filters/16627/6 "2017-07-06T01:40:08Z")

</div>


