# Correct way to apply multiple nested filters

**URL:** <https://discuss.elastic.co/t/correct-way-to-apply-multiple-nested-filters/23309>\
**Category:** Elasticsearch\
**Created:** [April 19, 2015, 9:21am UTC](https://discuss.elastic.co/t/correct-way-to-apply-multiple-nested-filters/23309 "2015-04-19T09:21:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barak\_Yaish](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Barak\_Yaish](https://discuss.elastic.co/u/Barak_Yaish)\
**Post date:** [April 19, 2015, 9:21am UTC](https://discuss.elastic.co/t/correct-way-to-apply-multiple-nested-filters/23309/1 "2015-04-19T09:21:03Z")

</div>

Hi all,

I'm having some difficulties running queries with multiple nested filters.  
Consider the following mapping:

{  
"template" : "products",  
"mappings" : {  
"Product" : {  
"dynamic" : "false",  
"properties" : {  
"id" : {  
"type" : "long"  
},  
"name" : {  
"type" : "string",  
"analyzer" : "standard"  
},  
"reviews" : {  
"type" : "nested",  
"dynamic" : "false",  
"properties" : {  
"positive" : {  
"type" : "nested",  
"dynamic" : "false",  
"properties" : {  
"reviewCount" : {  
"type" : "integer"  
},  
"keywords" : {  
"type" : "nested",  
"dynamic" : "false",  
"properties" : {  
"id" : {  
"type" : "long"  
},  
"name" : {  
"type" : "string",  
"analyzer" : "standard"  
},  
"score": {  
"type": "double"  
},  
"frequency": {  
"type": "integer"  
}  
}  
}  
}  
},  
"negative" : {  
"type" : "nested",  
"dynamic" : "false",  
"properties" : {  
"reviewCount" : {  
"type" : "integer"  
},  
"keywords" : {  
"type" : "nested",  
"dynamic" : "false",  
"properties" : {  
"id" : {  
"type" : "long"  
},  
"name" : {  
"type" : "string",  
"analyzer" : "standard"  
},  
"score": {  
"type": "double"  
},  
"frequency": {  
"type": "integer"  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}  
}

Running the following query:

NestedFilterBuilder idFilter = nestedFilter( "reviews.positive.keywords",  
filteredQuery( matchAllQuery(), termFilter( "reviews.positive.keywords.id",  
id ) ) );  
NestedFilterBuilder scoreFilter = nestedFilter(  
"reviews.positive.keywords", filteredQuery( matchAllQuery(), rangeFilter(  
"reviews.positive.keywords.score" ).from( score ).includeLower( true ) ) );

RangeFilterBuilder productScoreFilter = rangeFilter( "score" ).from(  
productScore ).includeLower( true );

SearchResponse searchResponse = esClient.prepareSearch( "products" ).  
setQuery( filteredQuery( matchAllQuery(), andFilter( productScoreFilter,  
idFilter, scoreFilter ) ) ).  
setSize( 40000 ).  
get();

The returned hits contain results with wrong score, i.e. score lower than  
what configured at scoreFilter. Seems like other filters are working  
perfect. Am I using the wrong way to apply multiple nested filters?

Thanks!

--  
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/cb007c0e-0aa2-4aec-b88c-db53407d114e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cb007c0e-0aa2-4aec-b88c-db53407d114e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Barak\_Yaish](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Barak\_Yaish](https://discuss.elastic.co/u/Barak_Yaish)\
**Post date:** [April 19, 2015, 12:19pm UTC](https://discuss.elastic.co/t/correct-way-to-apply-multiple-nested-filters/23309/2 "2015-04-19T12:19:30Z")

</div>

The following works, although I'm not sure why the previous query didn't..

AndFilterBuilder keywordFilter = andFilter( termFilter(  
"reviews.positive.keywords.id", id ), rangeFilter(  
"reviews.positive.keywords.score" ).from( score ).includeLower( true ) );

NestedFilterBuilder nestedKeywordFilter = nestedFilter(  
"reviews.positive.keywords", filteredQuery( matchAllQuery(), keywordFilter  
) );

RangeFilterBuilder productScoreFilter = rangeFilter( "score" ).from(  
productScore ).includeLower( true );

SearchResponse searchResponse = esClient.prepareSearch( "products" ).  
setQuery( filteredQuery( matchAllQuery(), andFilter( productScoreFilter,  
nestedKeywordFilter ) ) ).  
setSize( 40000 ).  
get();

On Sunday, April 19, 2015 at 12:21:04 PM UTC+3, barak wrote:

> Hi all,
> 
> I'm having some difficulties running queries with multiple nested filters.  
> Consider the following mapping:
> 
> {  
> "template" : "products",  
> "mappings" : {  
> "Product" : {  
> "dynamic" : "false",  
> "properties" : {  
> "id" : {  
> "type" : "long"  
> },  
> "name" : {  
> "type" : "string",  
> "analyzer" : "standard"  
> },  
> "reviews" : {  
> "type" : "nested",  
> "dynamic" : "false",  
> "properties" : {  
> "positive" : {  
> "type" : "nested",  
> "dynamic" : "false",  
> "properties" : {  
> "reviewCount" : {  
> "type" : "integer"  
> },  
> "keywords" : {  
> "type" : "nested",  
> "dynamic" : "false",  
> "properties" : {  
> "id" : {  
> "type" : "long"  
> },  
> "name" : {  
> "type" : "string",  
> "analyzer" : "standard"  
> },  
> "score": {  
> "type": "double"  
> },  
> "frequency": {  
> "type": "integer"  
> }  
> }  
> }  
> }  
> },  
> "negative" : {  
> "type" : "nested",  
> "dynamic" : "false",  
> "properties" : {  
> "reviewCount" : {  
> "type" : "integer"  
> },  
> "keywords" : {  
> "type" : "nested",  
> "dynamic" : "false",  
> "properties" : {  
> "id" : {  
> "type" : "long"  
> },  
> "name" : {  
> "type" : "string",  
> "analyzer" : "standard"  
> },  
> "score": {  
> "type": "double"  
> },  
> "frequency": {  
> "type": "integer"  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Running the following query:
> 
> NestedFilterBuilder idFilter = nestedFilter( "reviews.positive.keywords",  
> filteredQuery( matchAllQuery(), termFilter( "reviews.positive.keywords.id",  
> id ) ) );  
> NestedFilterBuilder scoreFilter = nestedFilter(  
> "reviews.positive.keywords", filteredQuery( matchAllQuery(), rangeFilter(  
> "reviews.positive.keywords.score" ).from( score ).includeLower( true ) ) );
> 
> RangeFilterBuilder productScoreFilter = rangeFilter( "score" ).from(  
> productScore ).includeLower( true );
> 
> SearchResponse searchResponse = esClient.prepareSearch( "products" ).  
> setQuery( filteredQuery( matchAllQuery(), andFilter( productScoreFilter,  
> idFilter, scoreFilter ) ) ).  
> setSize( 40000 ).  
> get();
> 
> The returned hits contain results with wrong score, i.e. score lower than  
> what configured at scoreFilter. Seems like other filters are working  
> perfect. Am I using the wrong way to apply multiple nested filters?
> 
> Thanks!

--  
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/ede31922-dadc-4003-bd04-650cb3340371%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/ede31922-dadc-4003-bd04-650cb3340371%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, 12:18am UTC](https://discuss.elastic.co/t/correct-way-to-apply-multiple-nested-filters/23309/3 "2017-07-06T00:18:52Z")

</div>


