Filter inside of array of objects


(CodingDrunk) #1

Please help out a ES newbie here.

My data looks like

obj1 : {
foo : [ { name : "almond", amount : 4 }, {name: "cashew", amount : 5}]
}

obj2 : {
foo : [ { name : "almond", amount : 5 }, {name: "cashew", amount : 5}]
}

if I only want to use a filter query to retrieve data that has 5 almonds, how should my filter look like?

I tried

{filter : {"bool" : {"must" : [{"term" : {"foo.name" : "almond"}}, {"range" : {"foo.amount" : { "eq" : 5}}}]}}

and I would NOT want obj1 to be returned, but it will because one of the objects in the array satisfy the first must query, and another one of the objects in the array satisfy the second first query.

Thanks in advance.


(Ian Eure) #2

On Jun 16, 2011, at 11:27 AM, CodingDrunk wrote:

and I would NOT want obj1 to be returned, but it will because one of the
objects in the array satisfy the first must query, and another one of the
objects in the array satisfy the second first query.

Have you tried coding this sober?


(Jürgen kartnaller) #3

Your data should look like this:

{"foo":{"almond":4, "cashew":5}}
{"foo":{"almond":5, "cashew":5}}

Now you can access "foo.almond" as the amount and do the query like this:

{
"query": {
"bool": {
"must": [{
"term": {"foo.almond": 5}
}]
}
}
}

Where have you seen the "eq" parameter for the range query, I could not find
it and it even don't work.

Here's the range docu:
http://www.elasticsearch.org/guide/reference/query-dsl/range-query.html

Jürgen

On Thu, Jun 16, 2011 at 8:27 PM, CodingDrunk codingdrunk@gmail.com wrote:

Please help out a ES newbie here.

My data looks like

obj1 : {
foo : [ { name : "almond", amount : 4 }, {name: "cashew", amount : 5}]
}

obj2 : {
foo : [ { name : "almond", amount : 5 }, {name: "cashew", amount : 5}]
}

if I only want to use a filter query to retrieve data that has 5 almonds,
how should my filter look like?

I tried

{filter : {"bool" : {"must" : [{"term" : {"foo.name" : "almond"}},
{"range"
: {"foo.amount" : { "eq" : 5}}}]}}

and I would NOT want obj1 to be returned, but it will because one of the
objects in the array satisfy the first must query, and another one of the
objects in the array satisfy the second first query.

Thanks in advance.

--
View this message in context:
http://elasticsearch-users.115913.n3.nabble.com/filter-inside-of-array-of-objects-tp3073187p3073187.html
Sent from the ElasticSearch Users mailing list archive at Nabble.com.

--
http://www.sfgdornbirn.at
http://www.mcb-bregenz.at


(system) #4