# Multi Valued fields in ElasticSearch

**URL:** <https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193>\
**Category:** Elasticsearch\
**Created:** [June 22, 2012, 12:12pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193 "2012-06-22T12:12:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saurabh](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Saurabh](https://discuss.elastic.co/u/Saurabh)\
**Post date:** [June 22, 2012, 12:12pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/1 "2012-06-22T12:12:55Z")

</div>

Hi,

How to create multi-valued fields in ElasticSearch, something like below:

{  
"name" : "abc",  
"favcolor" : "blue",  
"favcolor" : "black"  
}

I know that it can be created as

{  
"name" : "abc",  
"favcolor" : ["blue" , "black"]  
}

but is there any way to create it as specified?

---

<div class="post-metadata">

**Author:** ![AEvar\_Arnfjord\_Bjarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aevar_arnfjord_bjarm/32/2746_2.png) [@AEvar\_Arnfjord\_Bjarm](https://discuss.elastic.co/u/AEvar_Arnfjord_Bjarm)\
**Post date:** [June 22, 2012, 12:16pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/2 "2012-06-22T12:16:37Z")

</div>

You want a bool/should query that has two nested text queries for  
that, or use queryparser and AND or OR them, which'll resolve to the  
same thing.

---

<div class="post-metadata">

**Author:** ![Saurabh](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Saurabh](https://discuss.elastic.co/u/Saurabh)\
**Post date:** [June 22, 2012, 12:19pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/3 "2012-06-22T12:19:03Z")

</div>

Actually, i am not creating a query here, i want to index the values like  
this. Is there any way to do this?

On Friday, 22 June 2012 17:46:37 UTC+5:30, Ævar Arnfjörð Bjarmason wrote:

> You want a bool/should query that has two nested text queries for  
> that, or use queryparser and AND or OR them, which'll resolve to the  
> same thing.

---

<div class="post-metadata">

**Author:** ![AEvar\_Arnfjord\_Bjarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aevar_arnfjord_bjarm/32/2746_2.png) [@AEvar\_Arnfjord\_Bjarm](https://discuss.elastic.co/u/AEvar_Arnfjord_Bjarm)\
**Post date:** [June 22, 2012, 12:34pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/4 "2012-06-22T12:34:48Z")

</div>

On Fri, Jun 22, 2012 at 2:19 PM, Saurabh [saurabh.k1510@gmail.com](mailto:saurabh.k1510@gmail.com) wrote:

> Actually, i am not creating a query here, i want to index the values like  
> this. Is there any way to do this?

Right, I misunderstood your question.

The example you have is invalid JSON, when you have a hash two keys  
can't have the same keyname.

So no, you can't do it like that, and you have to explicitly create an  
array yourself.

---

<div class="post-metadata">

**Author:** ![Saurabh](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Saurabh](https://discuss.elastic.co/u/Saurabh)\
**Post date:** [June 22, 2012, 12:36pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/5 "2012-06-22T12:36:07Z")

</div>

Thanks, i thought the same.

On Friday, 22 June 2012 18:04:48 UTC+5:30, Ævar Arnfjörð Bjarmason wrote:

> On Fri, Jun 22, 2012 at 2:19 PM, Saurabh [saurabh.k1510@gmail.com](mailto:saurabh.k1510@gmail.com) wrote:
> 
> > Actually, i am not creating a query here, i want to index the values  
> > like  
> > this. Is there any way to do this?
> 
> Right, I misunderstood your question.
> 
> The example you have is invalid JSON, when you have a hash two keys  
> can't have the same keyname.
> 
> So no, you can't do it like that, and you have to explicitly create an  
> array yourself.

---

<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 22, 2012, 12:51pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/6 "2012-06-22T12:51:07Z")

</div>

On Fri, 2012-06-22 at 14:34 +0200, Ãvar ArnfjÃ¶rÃ° Bjarmason wrote:

> On Fri, Jun 22, 2012 at 2:19 PM, Saurabh [saurabh.k1510@gmail.com](mailto:saurabh.k1510@gmail.com) wrote:
> 
> > Actually, i am not creating a query here, i want to index the values like  
> > this. Is there any way to do this?
> 
> Right, I misunderstood your question.
> 
> The example you have is invalid JSON, when you have a hash two keys  
> can't have the same keyname.

This is correct.

> So no, you can't do it like that, and you have to explicitly create an  
> array yourself.

This isn't. Elasticsearch parses JSON in a stream and accepts repeated  
keys (ie invalid JSON) as multi-value fields. So your example doc would  
actually work - it would give you a multi-value field which you can  
search on.

However, I wouldn't recommend it, especially if you're going to use the  
JSON document that is returned from ES Any valid JSON parser would throw  
away one of your values. Abusing the streaming parser in this way is  
bound to cause hard-to-spot bugs later on.

clint

---

<div class="post-metadata">

**Author:** ![AEvar\_Arnfjord\_Bjarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aevar_arnfjord_bjarm/32/2746_2.png) [@AEvar\_Arnfjord\_Bjarm](https://discuss.elastic.co/u/AEvar_Arnfjord_Bjarm)\
**Post date:** [June 22, 2012, 2:09pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/7 "2012-06-22T14:09:27Z")

</div>

On Fri, Jun 22, 2012 at 2:51 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com) wrote:

> On Fri, 2012-06-22 at 14:34 +0200, Ævar Arnfjörð Bjarmason wrote:
> 
> > So no, you can't do it like that, and you have to explicitly create an  
> > array yourself.
> 
> This isn't. Elasticsearch parses JSON in a stream and accepts repeated  
> keys (ie invalid JSON) as multi-value fields. So your example doc would  
> actually work - it would give you a multi-value field which you can  
> search on.
> 
> However, I wouldn't recommend it, especially if you're going to use the  
> JSON document that is returned from ES Any valid JSON parser would throw  
> away one of your values. Abusing the streaming parser in this way is  
> bound to cause hard-to-spot bugs later on.

I didn't know that, I wonder whether this is intentional or a quirk of  
the current JSON parser that might go away.

---

<div class="post-metadata">

**Author:** ![Saurabh](https://avatars.discourse-cdn.com/v4/letter/s/ee7513/32.png) [@Saurabh](https://discuss.elastic.co/u/Saurabh)\
**Post date:** [June 22, 2012, 2:22pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/8 "2012-06-22T14:22:39Z")

</div>

I tried it out with my code, used bulkRequestBuilder to do so, the problem  
is that it ignores the first value, i.e in the example it will only index  
black not blue, a sort of overwrite i guess.

On Fri, Jun 22, 2012 at 7:39 PM, Ævar Arnfjörð Bjarmason  
[avarab@gmail.com](mailto:avarab@gmail.com)wrote:

> On Fri, Jun 22, 2012 at 2:51 PM, Clinton Gormley [clint@traveljury.com](mailto:clint@traveljury.com)  
> wrote:
> 
> > On Fri, 2012-06-22 at 14:34 +0200, Ævar Arnfjörð Bjarmason wrote:
> > 
> > > So no, you can't do it like that, and you have to explicitly create an  
> > > array yourself.
> > 
> > This isn't. Elasticsearch parses JSON in a stream and accepts repeated  
> > keys (ie invalid JSON) as multi-value fields. So your example doc would  
> > actually work - it would give you a multi-value field which you can  
> > search on.
> > 
> > However, I wouldn't recommend it, especially if you're going to use the  
> > JSON document that is returned from ES Any valid JSON parser would throw  
> > away one of your values. Abusing the streaming parser in this way is  
> > bound to cause hard-to-spot bugs later on.
> 
> I didn't know that, I wonder whether this is intentional or a quirk of  
> the current JSON parser that might go away.

--  
Saurabh Kumar  
M.Sc (Mathematics) B.E (Computer Science)  
Birla Institute of Technology and Science-Pilani

---

<div class="post-metadata">

**Author:** ![wishnaveen](https://avatars.discourse-cdn.com/v4/letter/w/dc4da7/32.png) [@wishnaveen](https://discuss.elastic.co/u/wishnaveen)\
**Post date:** [November 14, 2012, 1:29pm UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/9 "2012-11-14T13:29:41Z")

</div>

Hi Saurabh,

I am having the same problem while indexing multi value fields.  
BulkRequestBuilder is overwriting the first value.  
Do you have any solution for this ? Please help me in this regard.

Thanks,  
Naveen Vishwanatham.

---

<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:04am UTC](https://discuss.elastic.co/t/multi-valued-fields-in-elasticsearch/8193/10 "2017-07-06T03:04:20Z")

</div>


