# Filter inside of array of objects

**URL:** <https://discuss.elastic.co/t/filter-inside-of-array-of-objects/4641>\
**Category:** Elasticsearch\
**Created:** [June 16, 2011, 6:27pm UTC](https://discuss.elastic.co/t/filter-inside-of-array-of-objects/4641 "2011-06-16T18:27:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodingDrunk](https://avatars.discourse-cdn.com/v4/letter/c/a88e4f/32.png) [@CodingDrunk](https://discuss.elastic.co/u/CodingDrunk)\
**Post date:** [June 16, 2011, 6:27pm UTC](https://discuss.elastic.co/t/filter-inside-of-array-of-objects/4641/1 "2011-06-16T18:27:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ian\_Eure](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ian_eure/32/3157_2.png) [@Ian\_Eure](https://discuss.elastic.co/u/Ian_Eure)\
**Post date:** [June 17, 2011, 10:13pm UTC](https://discuss.elastic.co/t/filter-inside-of-array-of-objects/4641/2 "2011-06-17T22:13:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jurgen\_kartnaller](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jurgen_kartnaller/32/3086_2.png) [@Jurgen\_kartnaller](https://discuss.elastic.co/u/Jurgen_kartnaller)\
**Post date:** [June 18, 2011, 5:45am UTC](https://discuss.elastic.co/t/filter-inside-of-array-of-objects/4641/3 "2011-06-18T05:45:07Z")

</div>

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:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Jürgen

On Thu, Jun 16, 2011 at 8:27 PM, CodingDrunk [codingdrunk@gmail.com](mailto: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](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://Nabble.com).

--  
[http://www.sfgdornbirn.at](http://www.sfgdornbirn.at)  
[http://www.mcb-bregenz.at](http://www.mcb-bregenz.at)

---

<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, 4:03am UTC](https://discuss.elastic.co/t/filter-inside-of-array-of-objects/4641/4 "2017-07-06T04:03:12Z")

</div>


