# Aggregrations - Filter on (nested value OR nested value) AND non nested value

**URL:** <https://discuss.elastic.co/t/aggregrations-filter-on-nested-value-or-nested-value-and-non-nested-value/21560>\
**Category:** Elasticsearch\
**Created:** [January 9, 2015, 3:22pm UTC](https://discuss.elastic.co/t/aggregrations-filter-on-nested-value-or-nested-value-and-non-nested-value/21560 "2015-01-09T15:22:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dev\_Day](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dev_day/32/1003_2.png) [@Dev\_Day](https://discuss.elastic.co/u/Dev_Day)\
**Post date:** [January 9, 2015, 3:22pm UTC](https://discuss.elastic.co/t/aggregrations-filter-on-nested-value-or-nested-value-and-non-nested-value/21560/1 "2015-01-09T15:22:09Z")

</div>

Hi All,

Please see a working example of a document.  
The document has variants as a nested document within each root document.  
The data has 3 records as can be seen below.  
I would like to do the following:

1. For the filter aggregation for colour, I would like to filter by two  
ranges, in a OR condition - gt 10 lte 20 and gt 30 and lte 40
2. For the same filter aggregation for colour, I would like to filter by  
the above AND size = small.

Can anyone help?  
I cannot seem to use AND / OR or BOOL logic with nested / non-nested  
combinations?

DELETE /testindex/  
PUT /testindex/  
{  
"mappings": {  
"products":{  
"properties": {  
"variants":{  
"type":"nested"  
}  
}  
}  
}  
}

POST /\_bulk  
{"create":{"\_index":"testindex","\_type":"products","\_id":"1"}}  
{"code":"1", "variants": [{"sku":"123", "price": 15}], "colour":"blue",  
"size":"small"}  
{"create":{"\_index":"testindex","\_type":"products","\_id":"2"}}  
{"code":"2", "variants": [{"sku":"456", "price": 30}], "colour":"red",  
"size":"small"}  
{"create":{"\_index":"testindex","\_type":"products","\_id":"3"}}  
{"code":"3", "variants": [{"sku":"789", "price": 35}], "colour":"red",  
"size":"large"}

POST /testindex/products/\_search  
{  
"aggs": {  
"colour":{  
"filter":{  
"nested": {  
"path": "variants",  
"filter": {  
"range": { "price": { "gt":10,"lte":20}}  
}  
}  
},  
"aggs":{  
"colour":{  
"terms":{ "field": "colour" }  
}  
}  
},  
"price":{  
"nested": {  
"path":"variants"  
},  
"aggs":{  
"price":{  
"terms":{ "field": "price" }  
}  
}  
}  
},  
"post\_filter" : {  
"nested": {  
"path":"variants",  
"filter": {  
"range": { "price": { "gte":20}}  
}  
}  
}  
}

--  
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/5c37741e-d559-4d6f-b7f3-58c648b33930%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5c37741e-d559-4d6f-b7f3-58c648b33930%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Dev\_Day](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dev_day/32/1003_2.png) [@Dev\_Day](https://discuss.elastic.co/u/Dev_Day)\
**Post date:** [January 9, 2015, 3:44pm UTC](https://discuss.elastic.co/t/aggregrations-filter-on-nested-value-or-nested-value-and-non-nested-value/21560/2 "2015-01-09T15:44:01Z")

</div>

Hi All,

I think I've figured it out:

```
        "filter": {
                "and": {
                "or": [    
                   { 
                       "nested": {
                            "path": "variants",
                            "filter": {
                                "range": { "price": { "gt":10,"lte":20}}
                            }
                        }
            
                   }
                ]
        }

```

It will be a lot of code as nested within the last node, but I think it  
will work.

Dev

On Friday, January 9, 2015 at 3:22:09 PM UTC, Dev Day wrote:

> Hi All,
> 
> Please see a working example of a document.  
> The document has variants as a nested document within each root document.  
> The data has 3 records as can be seen below.  
> I would like to do the following:
> 
> 1. For the filter aggregation for colour, I would like to filter by two  
> ranges, in a OR condition - gt 10 lte 20 and gt 30 and lte 40
> 2. For the same filter aggregation for colour, I would like to filter by  
> the above AND size = small.
> 
> Can anyone help?  
> I cannot seem to use AND / OR or BOOL logic with nested / non-nested  
> combinations?
> 
> DELETE /testindex/  
> PUT /testindex/  
> {  
> "mappings": {  
> "products":{  
> "properties": {  
> "variants":{  
> "type":"nested"  
> }  
> }  
> }  
> }  
> }
> 
> POST /\_bulk  
> {"create":{"\_index":"testindex","\_type":"products","\_id":"1"}}  
> {"code":"1", "variants": [{"sku":"123", "price": 15}], "colour":"blue",  
> "size":"small"}  
> {"create":{"\_index":"testindex","\_type":"products","\_id":"2"}}  
> {"code":"2", "variants": [{"sku":"456", "price": 30}], "colour":"red",  
> "size":"small"}  
> {"create":{"\_index":"testindex","\_type":"products","\_id":"3"}}  
> {"code":"3", "variants": [{"sku":"789", "price": 35}], "colour":"red",  
> "size":"large"}
> 
> POST /testindex/products/\_search  
> {  
> "aggs": {  
> "colour":{  
> "filter":{  
> "nested": {  
> "path": "variants",  
> "filter": {  
> "range": { "price": { "gt":10,"lte":20}}  
> }  
> }  
> },  
> "aggs":{  
> "colour":{  
> "terms":{ "field": "colour" }  
> }  
> }  
> },  
> "price":{  
> "nested": {  
> "path":"variants"  
> },  
> "aggs":{  
> "price":{  
> "terms":{ "field": "price" }  
> }  
> }  
> }  
> },  
> "post\_filter" : {  
> "nested": {  
> "path":"variants",  
> "filter": {  
> "range": { "price": { "gte":20}}  
> }  
> }  
> }  
> }

--  
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/0771a418-83a7-4e7a-9ccb-9faf76c60ade%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0771a418-83a7-4e7a-9ccb-9faf76c60ade%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:39am UTC](https://discuss.elastic.co/t/aggregrations-filter-on-nested-value-or-nested-value-and-non-nested-value/21560/3 "2017-07-06T00:39:50Z")

</div>


