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


(Arny) #1

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


(Clinton Gormley) #2

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' -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


(lyes zaiko) #3

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 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


(Arny) #4

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 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 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


(system) #5