# Searching in arrays

**URL:** <https://discuss.elastic.co/t/searching-in-arrays/3801>\
**Category:** Elasticsearch\
**Created:** [January 18, 2011, 12:48pm UTC](https://discuss.elastic.co/t/searching-in-arrays/3801 "2011-01-18T12:48:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barak\_Yaish](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Barak\_Yaish](https://discuss.elastic.co/u/Barak_Yaish)\
**Post date:** [January 18, 2011, 12:48pm UTC](https://discuss.elastic.co/t/searching-in-arrays/3801/1 "2011-01-18T12:48:20Z")

</div>

Hello,

I'm indexing documents that contains arrays, for example:

{  
"record" :  
{  
"properties" :  
{  
"someId" : {"type" : "integer"},  
"categories" :  
{  
"properties" :  
{  
"cId" : {"type" : "integer"},  
"cScore" : {"type" : "integer"}  
}  
}  
}  
}  
}

I would like to search for categories that their id AND score matching  
some values. My problem is that the results contain categories that  
either their cId or cScore matches the criteria. This is the query I  
used:

curl -XGET '[http://localhost:9200/index/record/\_search](http://localhost:9200/index/record/_search)?  
q=categories.cId:70&categories.sScore:25'

Is there some other syntax I should use, or should I filter the  
results in the app?

Thanks.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 18, 2011, 6:25pm UTC](https://discuss.elastic.co/t/searching-in-arrays/3801/2 "2011-01-18T18:25:52Z")

</div>

This is how the search work. One way to solve this is to have a document per "array instance", then you can do this join.  
On Tuesday, January 18, 2011 at 2:48 PM, barak wrote:

> Hello,
> 
> I'm indexing documents that contains arrays, for example:
> 
> {  
> "record" :  
> {  
> "properties" :  
> {  
> "someId" : {"type" : "integer"},  
> "categories" :  
> {  
> "properties" :  
> {  
> "cId" : {"type" : "integer"},  
> "cScore" : {"type" : "integer"}  
> }  
> }  
> }  
> }  
> }
> 
> I would like to search for categories that their id AND score matching  
> some values. My problem is that the results contain categories that  
> either their cId or cScore matches the criteria. This is the query I  
> used:
> 
> curl -XGET '[http://localhost:9200/index/record/\_search](http://localhost:9200/index/record/_search)?  
> q=categories.cId:70&categories.sScore:25'
> 
> Is there some other syntax I should use, or should I filter the  
> results in the app?
> 
> Thanks.

---

<div class="post-metadata">

**Author:** ![Barak\_Yaish](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Barak\_Yaish](https://discuss.elastic.co/u/Barak_Yaish)\
**Post date:** [January 18, 2011, 6:52pm UTC](https://discuss.elastic.co/t/searching-in-arrays/3801/3 "2011-01-18T18:52:19Z")

</div>

Can you explain the "document per array instance"? Should the array be  
key?..

I'll be glad if you can an example...

Thanks!

On Jan 18, 8:25 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> This is how the search work. One way to solve this is to have a document per "array instance", then you can do this join.
> 
> On Tuesday, January 18, 2011 at 2:48 PM, barak wrote:
> 
> > Hello,
> 
> > I'm indexing documents that contains arrays, for example:
> 
> > {  
> > "record" :  
> > {  
> > "properties" :  
> > {  
> > "someId" : {"type" : "integer"},  
> > "categories" :  
> > {  
> > "properties" :  
> > {  
> > "cId" : {"type" : "integer"},  
> > "cScore" : {"type" : "integer"}  
> > }  
> > }  
> > }  
> > }  
> > }
> 
> > I would like to search for categories that their id AND score matching  
> > some values. My problem is that the results contain categories that  
> > either their cId or cScore matches the criteria. This is the query I  
> > used:
> 
> > curl -XGET '[http://localhost:9200/index/record/\_search](http://localhost:9200/index/record/_search)?  
> > q=categories.cId:70&categories.sScore:25'
> 
> > Is there some other syntax I should use, or should I filter the  
> > results in the app?
> 
> > Thanks.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 19, 2011, 10:25am UTC](https://discuss.elastic.co/t/searching-in-arrays/3801/4 "2011-01-19T10:25:03Z")

</div>

I mean that each index document will be an element in the array. For example, you will have a recover\_property as a document, and not a record.  
On Tuesday, January 18, 2011 at 8:52 PM, barak wrote:

> Can you explain the "document per array instance"? Should the array be  
> key?..
> 
> I'll be glad if you can an example...
> 
> Thanks!
> 
> On Jan 18, 8:25 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > This is how the search work. One way to solve this is to have a document per "array instance", then you can do this join.
> > 
> > On Tuesday, January 18, 2011 at 2:48 PM, barak wrote:
> > 
> > > Hello,
> > 
> > > I'm indexing documents that contains arrays, for example:
> > 
> > > {  
> > > "record" :  
> > > {  
> > > "properties" :  
> > > {  
> > > "someId" : {"type" : "integer"},  
> > > "categories" :  
> > > {  
> > > "properties" :  
> > > {  
> > > "cId" : {"type" : "integer"},  
> > > "cScore" : {"type" : "integer"}  
> > > }  
> > > }  
> > > }  
> > > }  
> > > }
> > 
> > > I would like to search for categories that their id AND score matching  
> > > some values. My problem is that the results contain categories that  
> > > either their cId or cScore matches the criteria. This is the query I  
> > > used:
> > 
> > > curl -XGET '[http://localhost:9200/index/record/\_search](http://localhost:9200/index/record/_search)?  
> > > q=categories.cId:70&categories.sScore:25'
> > 
> > > Is there some other syntax I should use, or should I filter the  
> > > results in the app?
> > 
> > > Thanks.

---

<div class="post-metadata">

**Author:** ![Barak\_Yaish](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Barak\_Yaish](https://discuss.elastic.co/u/Barak_Yaish)\
**Post date:** [January 19, 2011, 1:07pm UTC](https://discuss.elastic.co/t/searching-in-arrays/3801/5 "2011-01-19T13:07:30Z")

</div>

I think I lost you... For the mapping above, can you please guide me  
how to rephrase it to support the type of search I'm trying to do?

Thanks!

On Jan 19, 12:25 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> I mean that each index document will be an element in the array. For example, you will have a recover\_property as a document, and not a record.
> 
> On Tuesday, January 18, 2011 at 8:52 PM, barak wrote:
> 
> > Can you explain the "document per array instance"? Should the array be  
> > key?..
> 
> > I'll be glad if you can an example...
> 
> > Thanks!
> 
> > On Jan 18, 8:25 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > > This is how the search work. One way to solve this is to have a document per "array instance", then you can do this join.
> 
> > > On Tuesday, January 18, 2011 at 2:48 PM, barak wrote:
> 
> > > > Hello,
> 
> > > > I'm indexing documents that contains arrays, for example:
> 
> > > > {  
> > > > "record" :  
> > > > {  
> > > > "properties" :  
> > > > {  
> > > > "someId" : {"type" : "integer"},  
> > > > "categories" :  
> > > > {  
> > > > "properties" :  
> > > > {  
> > > > "cId" : {"type" : "integer"},  
> > > > "cScore" : {"type" : "integer"}  
> > > > }  
> > > > }  
> > > > }  
> > > > }  
> > > > }
> 
> > > > I would like to search for categories that their id AND score matching  
> > > > some values. My problem is that the results contain categories that  
> > > > either their cId or cScore matches the criteria. This is the query I  
> > > > used:
> 
> > > > curl -XGET '[http://localhost:9200/index/record/\_search](http://localhost:9200/index/record/_search)?  
> > > > q=categories.cId:70&categories.sScore:25'
> 
> > > > Is there some other syntax I should use, or should I filter the  
> > > > results in the app?
> 
> > > > Thanks.

---

<div class="post-metadata">

**Author:** ![Barak\_Yaish](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Barak\_Yaish](https://discuss.elastic.co/u/Barak_Yaish)\
**Post date:** [January 20, 2011, 2:14pm UTC](https://discuss.elastic.co/t/searching-in-arrays/3801/6 "2011-01-20T14:14:17Z")

</div>

I'm still trying to solve this issue, can anyone help with an example?  
I googled for it, but I can't find something helpful..

Thanks.

On Jan 19, 3:07 pm, barak [barak.ya...@gmail.com](mailto:barak.ya...@gmail.com) wrote:

> I think I lost you... For the mapping above, can you please guide me  
> how to rephrase it to support the type of search I'm trying to do?
> 
> Thanks!
> 
> On Jan 19, 12:25 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > I mean that each index document will be an element in the array. For example, you will have a recover\_property as a document, and not a record.
> 
> > On Tuesday, January 18, 2011 at 8:52 PM, barak wrote:
> > 
> > > Can you explain the "document per array instance"? Should the array be  
> > > key?..
> 
> > > I'll be glad if you can an example...
> 
> > > Thanks!
> 
> > > On Jan 18, 8:25 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> 
> > > > This is how the search work. One way to solve this is to have a document per "array instance", then you can do this join.
> 
> > > > On Tuesday, January 18, 2011 at 2:48 PM, barak wrote:
> 
> > > > > Hello,
> 
> > > > > I'm indexing documents that contains arrays, for example:
> 
> > > > > {  
> > > > > "record" :  
> > > > > {  
> > > > > "properties" :  
> > > > > {  
> > > > > "someId" : {"type" : "integer"},  
> > > > > "categories" :  
> > > > > {  
> > > > > "properties" :  
> > > > > {  
> > > > > "cId" : {"type" : "integer"},  
> > > > > "cScore" : {"type" : "integer"}  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }  
> > > > > }
> 
> > > > > I would like to search for categories that their id AND score matching  
> > > > > some values. My problem is that the results contain categories that  
> > > > > either their cId or cScore matches the criteria. This is the query I  
> > > > > used:
> 
> > > > > curl -XGET '[http://localhost:9200/index/record/\_search](http://localhost:9200/index/record/_search)?  
> > > > > q=categories.cId:70&categories.sScore:25'
> 
> > > > > Is there some other syntax I should use, or should I filter the  
> > > > > results in the app?
> 
> > > > > Thanks.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 20, 2011, 4:22pm UTC](https://discuss.elastic.co/t/searching-in-arrays/3801/7 "2011-01-20T16:22:41Z")

</div>

Lets say you index a document, and it has an array of two fields, field1, and field2, then you run into the problem mentioned in the first mail, where doing field1 AND field2 might return hits where the field1 value exists in one array element, and field2 exists in another.

A way to solve this is to index each array element as a different document. A single document that consists of a single value of field1 and a single value of field2.

-shay.banon

On Thursday, January 20, 2011 at 4:14 PM, barak wrote:

> I'm still trying to solve this issue, can anyone help with an example?  
> I googled for it, but I can't find something helpful..
> 
> Thanks.
> 
> On Jan 19, 3:07 pm, barak [barak.ya...@gmail.com](mailto:barak.ya...@gmail.com) wrote:
> 
> > I think I lost you... For the mapping above, can you please guide me  
> > how to rephrase it to support the type of search I'm trying to do?
> > 
> > Thanks!
> > 
> > On Jan 19, 12:25 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > I mean that each index document will be an element in the array. For example, you will have a recover\_property as a document, and not a record.
> > 
> > > On Tuesday, January 18, 2011 at 8:52 PM, barak wrote:
> > > 
> > > > Can you explain the "document per array instance"? Should the array be  
> > > > key?..
> > 
> > > > I'll be glad if you can an example...
> > 
> > > > Thanks!
> > 
> > > > On Jan 18, 8:25 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:
> > 
> > > > > This is how the search work. One way to solve this is to have a document per "array instance", then you can do this join.
> > 
> > > > > On Tuesday, January 18, 2011 at 2:48 PM, barak wrote:
> > 
> > > > > > Hello,
> > 
> > > > > > I'm indexing documents that contains arrays, for example:
> > 
> > > > > > {  
> > > > > > "record" :  
> > > > > > {  
> > > > > > "properties" :  
> > > > > > {  
> > > > > > "someId" : {"type" : "integer"},  
> > > > > > "categories" :  
> > > > > > {  
> > > > > > "properties" :  
> > > > > > {  
> > > > > > "cId" : {"type" : "integer"},  
> > > > > > "cScore" : {"type" : "integer"}  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }  
> > > > > > }
> > 
> > > > > > I would like to search for categories that their id AND score matching  
> > > > > > some values. My problem is that the results contain categories that  
> > > > > > either their cId or cScore matches the criteria. This is the query I  
> > > > > > used:
> > 
> > > > > > curl -XGET '[http://localhost:9200/index/record/\_search](http://localhost:9200/index/record/_search)?  
> > > > > > q=categories.cId:70&categories.sScore:25'
> > 
> > > > > > Is there some other syntax I should use, or should I filter the  
> > > > > > results in the app?
> > 
> > > > > > 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, 4:13am UTC](https://discuss.elastic.co/t/searching-in-arrays/3801/8 "2017-07-06T04:13:25Z")

</div>


