# Apply multiple filters on nested array of properties

**URL:** https://discuss.elastic.co/t/apply-multiple-filters-on-nested-array-of-properties/13541
**Category:** Elasticsearch
**Created:** [September 10, 2013, 4:35pm UTC](https://discuss.elastic.co/t/apply-multiple-filters-on-nested-array-of-properties/13541 "2013-09-10T16:35:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Cote](https://avatars.discourse-cdn.com/v4/letter/c/b38774/32.png) [@Christopher\_Cote](https://discuss.elastic.co/u/Christopher_Cote)
#### Post date: [September 10, 2013, 4:35pm UTC](https://discuss.elastic.co/t/apply-multiple-filters-on-nested-array-of-properties/13541/1 "2013-09-10T16:35:54Z")

</div>

I'm having an issue using multiple filters on an array of nested filters.

My document looks something like this.

{  
"name":"Python Test"  
"filters" : [  
{  
"name" : "duration",  
"value" : "2161"  
},  
{  
"name" : "media",  
"value" : "video"  
},  
{  
"name" : "content-type",  
"value" : "screencast"  
},  
{  
"name" : "timing",  
"value" : "anytime"  
},  
{  
"name" : "price",  
"value" : 0  
}  
],  
}

my mapping is along these lines.

"filters" : {  
"type" : "nested",  
"properties" : {  
"name" : {  
"type" : "string",  
"index" : "not\_analyzed",  
"omit\_norms" : true,  
"index\_options" : "docs"  
},  
"value" : {  
"type" : "string",  
"index" : "not\_analyzed",  
"omit\_norms" : true,  
"index\_options" : "docs"  
}  
}  
},

I'm trying to apply multiple filters to the search eg; only return  
documents that have `content-type: screencast` and `timing: anytime` also  
need to be able to apply a search term.

my current query looks something like this, about the 100th iteration.

{  
"fields":cls.Search.\_fields,  
"from":frm,  
"size":size,  
"query":{  
"filtered":{  
"query":{  
"query\_string": {  
"query": search\_term,  
"fields": cls.Search.\_search\_fields,  
}  
},  
"filter":{  
"nested":{  
"path":"filters",  
"query":{  
"match\_all" : {}  
},  
"filter":{  
"and":[  
{"and":[

{"term":{"filters.name":"content-type"}},  
{"term":{"filters.value":"lecture"}}  
]},  
{"and":[  
{"term":{"filters.name":"price"}},

{"range":{"filters.value":{"from":0, "to":9999}}}  
]}  
]  
}  
}  
}  
}  
}  
}

I'm able to use a bool query in the nested filter, almost exactly like the  
example  
here. [http://www.elasticsearch.org/guide/reference/query-dsl/nested-filter/](http://www.elasticsearch.org/guide/reference/query-dsl/nested-filter/)

and if I hard code multiple "must" keys I can get it to work with multiple  
filters, however Python (my language of choice) doesn't allow for multiple  
"must" keywords in a dictionary, so I'm not able to dynamically create the  
query, which is a must. I would also like to avoid raw string formatting if  
possible.

I feel like this document schema is pretty common, but I haven't found any  
examples of applying multiple filters on an array of nested objects.

I'm not totally against changing the schema, but again, I feel like this  
would be a pretty common approach.

Ideas or solutions?

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Christopher\_Cote](https://avatars.discourse-cdn.com/v4/letter/c/b38774/32.png) [@Christopher\_Cote](https://discuss.elastic.co/u/Christopher_Cote)
#### Post date: [September 10, 2013, 7:41pm UTC](https://discuss.elastic.co/t/apply-multiple-filters-on-nested-array-of-properties/13541/2 "2013-09-10T19:41:22Z")

</div>

I was able to get something working.

The secret was using "AND"ing multiple nested filters...

My final query looks like this.

After reading this post several times, I figured out his  
solution. [https://groups.google.com/forum/#!searchin/elasticsearch/multiple$20filters$20nested/elasticsearch/woVLXF\_pnM8/gOoqhg-9GT8J](https://groups.google.com/forum/#!searchin/elasticsearch/multiple%2420filters%2420nested/elasticsearch/woVLXF_pnM8/gOoqhg-9GT8J)

q = {  
"fields":cls.Search.\_fields,  
"from":frm,  
"size":size,  
"query":{  
"query\_string": {  
"query": search\_term,  
"fields": cls.Search.\_search\_fields,  
}  
},  
"filter":{  
"and":[  
{"nested": {  
"path": "filters",  
"query":{  
"filtered": {  
"query": { "match\_all": {}},  
"filter": {  
"and": [  
{"term": {"filters.name":  
"content-type"}},  
{"term": {"filters.value":  
"screencast"}}  
],  
}  
}  
}  
}},  
{"nested": {  
"path": "filters",  
"query":{  
"filtered": {  
"query": { "match\_all": {}},  
"filter": {  
"and": [  
{"term": {"filters.name": "price"}},  
{"range": {"filters.value":  
{"from":0, "to":99999}}}  
],  
}  
}  
}  
}}  
]  
},  
}

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:17am UTC](https://discuss.elastic.co/t/apply-multiple-filters-on-nested-array-of-properties/13541/3 "2017-07-06T02:17:14Z")

</div>


