Multi Valued fields in ElasticSearch

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?

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.

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.

On Fri, Jun 22, 2012 at 2:19 PM, Saurabh 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.

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

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 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

On Fri, Jun 22, 2012 at 2:51 PM, Clinton Gormley 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.

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.comwrote:

On Fri, Jun 22, 2012 at 2:51 PM, Clinton Gormley 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

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.