# Query/Filter with range on multiple values inside sub-objects

**URL:** <https://discuss.elastic.co/t/query-filter-with-range-on-multiple-values-inside-sub-objects/6166>\
**Category:** Elasticsearch\
**Created:** [December 15, 2011, 2:00pm UTC](https://discuss.elastic.co/t/query-filter-with-range-on-multiple-values-inside-sub-objects/6166 "2011-12-15T14:00:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arny](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@Arny](https://discuss.elastic.co/u/Arny)\
**Post date:** [December 15, 2011, 2:00pm UTC](https://discuss.elastic.co/t/query-filter-with-range-on-multiple-values-inside-sub-objects/6166/1 "2011-12-15T14:00:59Z")

</div>

Hello,

we're storing multiple colors (r,g,b) per document and would like to  
query for colors that are in specific range.

colors data looks like this:  
colors: [  
{  
r: 96  
g: 48  
b: 48  
}  
{  
r: 120  
g: 72  
b: 72  
}

Now how would I query for rgb(96,48,48) with range? e.g. rgb(90-100,  
45-50, 45-50).  
rgb(96,72,72) should not match.

We appreciate any help.  
Thanks

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [December 15, 2011, 2:11pm UTC](https://discuss.elastic.co/t/query-filter-with-range-on-multiple-values-inside-sub-objects/6166/2 "2011-12-15T14:11:31Z")

</div>

Hiya

> we're storing multiple colors (r,g,b) per document and would like to  
> query for colors that are in specific range.
> 
> colors data looks like this:  
> colors: [  
> {  
> r: 96  
> g: 48  
> b: 48  
> }  
> {  
> r: 120  
> g: 72  
> b: 72  
> }
> 
> Now how would I query for rgb(96,48,48) with range? e.g. rgb(90-100,  
> 45-50, 45-50).  
> rgb(96,72,72) should not match.

First, you need to map your 'colors' to be of type 'nested', not the  
default type 'object'. This allows you to query each colors object  
separately, otherwise all of your colors would be flattened to look  
something like:

colors: {  
r: [96,120],  
b: [48,72],  
g: [48,72]  
}

Then, you can use a nested filter on your documents (of type eg  
'my\_type') as follows:

curl -XGET '[http://127.0.0.1:9200/\_all/\_search?pretty=1](http://127.0.0.1:9200/_all/_search?pretty=1)' -d '  
{  
"query" : {  
"constant\_score" : {  
"filter" : {  
"nested" : {  
"path" : "my\_type.colors",  
"filter" : {  
"and" : [  
{  
"range" : {  
"r" : {  
"lte" : 100,  
"gte" : 90  
}  
}  
},  
{  
"range" : {  
"g" : {  
"lte" : 50,  
"gte" : 45  
}  
}  
},  
{  
"range" : {  
"b" : {  
"lte" : 50,  
"gte" : 45  
}  
}  
}  
]  
}  
}  
}  
}  
}  
}  
'  
clint

---

<div class="post-metadata">

**Author:** ![lyes\_zaiko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lyes_zaiko/32/11250_2.png) [@lyes\_zaiko](https://discuss.elastic.co/u/lyes_zaiko)\
**Post date:** [December 15, 2011, 2:12pm UTC](https://discuss.elastic.co/t/query-filter-with-range-on-multiple-values-inside-sub-objects/6166/3 "2011-12-15T14:12:04Z")

</div>

Try something like:

"color.r: [90 TO 100] AND color.g: [45 TO 50] AND color.b: [45-50]"

in your query.

Here I'm giving the URI Request version since I'm not very familiar with  
the JSON Query DSL, but finding the equivalent is very trivial.

Hope this will help!

On Thu, Dec 15, 2011 at 3:00 PM, Arny [arny.ok@googlemail.com](mailto:arny.ok@googlemail.com) wrote:

> Hello,
> 
> we're storing multiple colors (r,g,b) per document and would like to  
> query for colors that are in specific range.
> 
> colors data looks like this:  
> colors: [  
> {  
> r: 96  
> g: 48  
> b: 48  
> }  
> {  
> r: 120  
> g: 72  
> b: 72  
> }
> 
> Now how would I query for rgb(96,48,48) with range? e.g. rgb(90-100,  
> 45-50, 45-50).  
> rgb(96,72,72) should not match.
> 
> We appreciate any help.  
> Thanks

---

<div class="post-metadata">

**Author:** ![Arny](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@Arny](https://discuss.elastic.co/u/Arny)\
**Post date:** [December 15, 2011, 2:42pm UTC](https://discuss.elastic.co/t/query-filter-with-range-on-multiple-values-inside-sub-objects/6166/4 "2011-12-15T14:42:07Z")

</div>

Thanks Lyes, but I think Clint is right.  
Your solution is matching rgb(96,72,72) which should not.

Will try Clints solution.

Thanks

On Dec 15, 3:12 pm, Lyes zaiko [lyes...@gmail.com](mailto:lyes...@gmail.com) wrote:

> Try something like:
> 
> "color.r: [90 TO 100] AND color.g: [45 TO 50] AND color.b: [45-50]"
> 
> in your query.
> 
> Here I'm giving the URI Request version since I'm not very familiar with  
> the JSON Query DSL, but finding the equivalent is very trivial.
> 
> Hope this will help!
> 
> On Thu, Dec 15, 2011 at 3:00 PM, Arny [arny...@googlemail.com](mailto:arny...@googlemail.com) wrote:
> 
> > Hello,
> 
> > we're storing multiple colors (r,g,b) per document and would like to  
> > query for colors that are in specific range.
> 
> > colors data looks like this:  
> > colors: [  
> > {  
> > r: 96  
> > g: 48  
> > b: 48  
> > }  
> > {  
> > r: 120  
> > g: 72  
> > b: 72  
> > }
> 
> > Now how would I query for rgb(96,48,48) with range? e.g. rgb(90-100,  
> > 45-50, 45-50).  
> > rgb(96,72,72) should not match.
> 
> > We appreciate any help.  
> > Thanks

---

<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:45am UTC](https://discuss.elastic.co/t/query-filter-with-range-on-multiple-values-inside-sub-objects/6166/5 "2017-07-06T03:45:19Z")

</div>


