# ElasticSearch array query (multiselect)

**URL:** https://discuss.elastic.co/t/elasticsearch-array-query-multiselect/8218
**Category:** Elasticsearch
**Created:** [June 25, 2012, 4:56pm UTC](https://discuss.elastic.co/t/elasticsearch-array-query-multiselect/8218 "2012-06-25T16:56:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![coys](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@coys](https://discuss.elastic.co/u/coys)
#### Post date: [June 25, 2012, 4:56pm UTC](https://discuss.elastic.co/t/elasticsearch-array-query-multiselect/8218/1 "2012-06-25T16:56:14Z")

</div>

Hello, I have a multi-select listbox e.g. Region - ["A","B","D"] (3  
selections out of 6) which is populated from an array entry in  
elasticsearch ["A","B",C","D","E","F"].  
If I do search for e.g. only on A elasticsearch returns correctly.  
If I want to search for the all entries which contain Region "A" and  
"B", I can combine it into a text query which returns the string "A,B"  
and it will give me all such entries..e.g. even return a field which  
contains ["A","B","D","F"].

I now want to return for e.g. all entries which contain "A", "B" and  
"D". In this case my text-query falls short as it can return an entry  
which contains ["A","B","D","F"] which is nice but cannot return  
entries which contain region for e.g. ["A","B","C","D","F"] (C in  
middle of text string "A","B" and "D"). and my approach looks terribly  
wrong.

Can you please tell me how I should formulate this query? I have no  
idea how to proceed and I'm stuck.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: [June 25, 2012, 5:28pm UTC](https://discuss.elastic.co/t/elasticsearch-array-query-multiselect/8218/2 "2012-06-25T17:28:08Z")

</div>

On Mon, 2012-06-25 at 09:56 -0700, coys wrote:

> Hello, I have a multi-select listbox e.g. Region - ["A","B","D"] (3  
> selections out of 6) which is populated from an array entry in  
> elasticsearch ["A","B",C","D","E","F"].  
> If I do search for e.g. only on A elasticsearch returns correctly.  
> If I want to search for the all entries which contain Region "A" and  
> "B", I can combine it into a text query which returns the string "A,B"  
> and it will give me all such entries..e.g. even return a field which  
> contains ["A","B","D","F"].
> 
> I now want to return for e.g. all entries which contain "A", "B" and  
> "D". In this case my text-query falls short as it can return an entry  
> which contains ["A","B","D","F"] which is nice but cannot return  
> entries which contain region for e.g. ["A","B","C","D","F"] (C in  
> middle of text string "A","B" and "D"). and my approach looks terribly  
> wrong.
> 
> Can you please tell me how I should formulate this query? I have no  
> idea how to proceed and I'm stuck.Thanks!

Bunging these all into a text query is probably not the best way to do  
this.

I'm assuming that your A..F values are 'enums', ie you want to match  
exactly what is in that field. So 'Foo' matches 'Foo' but not 'foo' or  
'FOO' etc.

In other words, you don't want your values to be analyzed - they're not  
full text, they are just values.

So create your index like this (note the 'tags' field is not\_analyzed):

curl -XPUT '[http://127.0.0.1:9200/test/?pretty=1](http://127.0.0.1:9200/test/?pretty=1)' -d '  
{  
"mappings" : {  
"test" : {  
"properties" : {  
"text" : {  
"type" : "string"  
},  
"tags" : {  
"index" : "not\_analyzed",  
"type" : "string"  
}  
}  
}  
}  
}  
'

Then we can add some data (I've added docs with multiple tags, but they  
could equally have a single tag each):

curl -XPUT '[http://127.0.0.1:9200/test/test/1?pretty=1](http://127.0.0.1:9200/test/test/1?pretty=1)' -d '  
{  
"text" : "the quick brown fox",  
"tags" : [  
"A",  
"C"  
]  
}  
'  
curl -XPUT '[http://127.0.0.1:9200/test/test/2?pretty=1](http://127.0.0.1:9200/test/test/2?pretty=1)' -d '  
{  
"text" : "the quick brown fox",  
"tags" : [  
"A",  
"C",  
"D"  
]  
}  
'  
curl -XPUT '[http://127.0.0.1:9200/test/test/3?pretty=1](http://127.0.0.1:9200/test/test/3?pretty=1)' -d '  
{  
"text" : "the quick brown fox",  
"tags" : [  
"D",  
"E"  
]  
}  
'

Then we can search for docs, using the A..F values as a filter. We use  
the 'term' filter because we want an exact match:

curl -XGET '[http://127.0.0.1:9200/test/test/\_search?pretty=1](http://127.0.0.1:9200/test/test/_search?pretty=1)' -d '  
{  
"query" : {  
"filtered" : {  
"filter" : {  
"terms" : {  
"tags" : [  
"A",  
"D"  
]  
}  
},  
"query" : {  
"text" : {  
"text" : "quick"  
}  
}  
}  
}  
}  
'

# [Mon Jun 25 19:23:19 2012] Response:

# {

# "hits" : {

# "hits" : [

# {

# "\_source" : {

# "text" : "the quick brown fox",

# "tags" : [

# "A",

# "C"

# ]

# },

# "\_score" : 0.15342641,

# "\_index" : "test",

# "\_id" : "1",

# "\_type" : "test"

# },

# {

# "\_source" : {

# "text" : "the quick brown fox",

# "tags" : [

# "A",

# "C",

# "D"

# ]

# },

# "\_score" : 0.15342641,

# "\_index" : "test",

# "\_id" : "2",

# "\_type" : "test"

# },

# {

# "\_source" : {

# "text" : "the quick brown fox",

# "tags" : [

# "D",

# "E"

# ]

# },

# "\_score" : 0.15342641,

# "\_index" : "test",

# "\_id" : "3",

# "\_type" : "test"

# }

# ],

# "max\_score" : 0.15342641,

# "total" : 3

# },

# "timed\_out" : false,

# "\_shards" : {

# "failed" : 0,

# "successful" : 5,

# "total" : 5

# },

# "took" : 5

# }

clint

---

<div class="post-metadata">

### Author: ![coys](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@coys](https://discuss.elastic.co/u/coys)
#### Post date: [June 26, 2012, 3:06pm UTC](https://discuss.elastic.co/t/elasticsearch-array-query-multiselect/8218/3 "2012-06-26T15:06:25Z")

</div>

Hi Clint, thanks for the quick reply, that's very close to what I'd  
like to return. Ideally on a search for ["A","D"] i'd only like to  
return entries which contain both A and D.For eg. Let's say I have 4  
entries A,B,C and D containing a field city populated from an enum  
["NY",LN","ZU","FF"]  
where A = ["NY","LN",ZU"]  
B = ["NY","ZU","FF"]  
C = ["NY", "LN"]  
D = ["LN", "ZU"]  
on a search for ["NY","ZU"] I'd only like to return A and B. Thanks!

Also, sorry to diverge from the topic but I noticed that if I have a  
query like this (I'm using a C# helper which provides the  
elasticSearch API)  
mustQuery.Text(text =\> text.Field("city")  
.Query("A AND D"));  
This would also return any entry which has either A or D or both A and  
D present. However, is this a correct approach or would the term  
filter be the right way to solve this? 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: [June 26, 2012, 3:18pm UTC](https://discuss.elastic.co/t/elasticsearch-array-query-multiselect/8218/4 "2012-06-26T15:18:39Z")

</div>

On Tue, 2012-06-26 at 08:06 -0700, coys wrote:

> Hi Clint, thanks for the quick reply, that's very close to what I'd  
> like to return. Ideally on a search for ["A","D"] i'd only like to  
> return entries which contain both A and D.For eg. Let's say I have 4  
> entries A,B,C and D containing a field city populated from an enum  
> ["NY",LN","ZU","FF"]  
> where A = ["NY","LN",ZU"]  
> B = ["NY","ZU","FF"]  
> C = ["NY", "LN"]  
> D = ["LN", "ZU"]  
> on a search for ["NY","ZU"] I'd only like to return A and B. Thanks!

Have a look at the docs for the terms filter:

> **[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.

or alternatively, you could use the and filter:

> **[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.

they query dsl is your friend 🙂

> **[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.

clint

---

<div class="post-metadata">

### Author: ![coys](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@coys](https://discuss.elastic.co/u/coys)
#### Post date: [June 26, 2012, 3:27pm UTC](https://discuss.elastic.co/t/elasticsearch-array-query-multiselect/8218/5 "2012-06-26T15:27:06Z")

</div>

Thanks! will go through the dsl:)

On Jun 26, 9:18 am, Clinton Gormley [cl...@traveljury.com](mailto:cl...@traveljury.com) wrote:

> On Tue, 2012-06-26 at 08:06 -0700, coys wrote:
> 
> > Hi Clint, thanks for the quick reply, that's very close to what I'd  
> > like to return. Ideally on a search for ["A","D"] i'd only like to  
> > return entries which contain both A and D.For eg. Let's say I have 4  
> > entries A,B,C and D containing a field city populated from an enum  
> > ["NY",LN","ZU","FF"]  
> > where A = ["NY","LN",ZU"]  
> > B = ["NY","ZU","FF"]  
> > C = ["NY", "LN"]  
> > D = ["LN", "ZU"]  
> > on a search for ["NY","ZU"] I'd only like to return A and B. Thanks!
> 
> Have a look at the docs for the terms filter:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html)
> 
> or alternatively, you could use the and filter:[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/and-filter.html)
> 
> they query dsl is your friend 🙂
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl)
> 
> clint

---

<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:22am UTC](https://discuss.elastic.co/t/elasticsearch-array-query-multiselect/8218/6 "2017-07-06T03:22:26Z")

</div>


