By default, elasticsearch tries to deduce field types from filed values. If
you will check mappings after the first PUT request, you will see something
like this:
$ curl 'http://localhost:9200/test/_mapping?pretty=true'
{
"test" : {
"tweet" : {
"properties" : {
* "content" : {*
-
"dynamic" : "true",*
-
"properties" : {*
-
"text" : { "type" : "string"},*
-
"title" : { "type" : "string"}*
-
}*
},
"contentType" : {"type" : "string"},
"user" : {"type" : "string"}
}
}
}
}
As you can see from this mapping, elasticsearch is now treating content as
an object type field
(Elasticsearch Platform — Find real-time answers at scale | Elastic) and
it will be expecting to see objects in this field until index is deleted.
If you ran the second requests first, it would have expected to see strings
there and failed on object.
There are a couple of ways around it. You can use different field names for
different types:
curl -XPUT 'http://localhost:9200/test/tweet/1' -d '{ "user": "jane doe",
"contentType": "article", "content*_article*": { "title": "some news",
"text": "blah blah" } }'
curl -XPUT 'http://localhost:9200/test/tweet/2' -d '{ "user": "john doe",
"contentType": "url", "content*_url*": "http://example.com/foo" }'
curl -XPUT 'http://localhost:9200/test/tweet/3' -d '{ "user": "john doe",
"contentType": "number", "content*_number*": 123 }'
Or you can assign different elasticsearch types to records with different
content types, (different elasticsearch types can have different mappings).
curl -XPUT 'http://localhost:9200/test/*article*/1' -d '{ "user": "jane
doe", "contentType": "article", "content": { "title": "some news", "text":
"blah blah" } }'
curl -XPUT 'http://localhost:9200/test/*url*/2' -d '{ "user": "john doe",
"contentType": "url", "content": "http://example.com/foo" }'
curl -XPUT 'http://localhost:9200/test/*number*/3' -d '{ "user": "john
doe", "contentType": "number", "content": 123 }'
On Monday, April 9, 2012 4:52:31 PM UTC-4, Jean-Sebastien Delfino wrote:
Hi,
I'm trying to put JSON data into Elastic Search like this:
curl -XPUT 'http://localhost:9200/test/tweet/1' -d '{ "user": "jane doe",
"contentType": "article", "content": { "title": "some news", "text": "blah
blah" } }'
curl -XPUT 'http://localhost:9200/test/tweet/2' -d '{ "user": "john doe",
"contentType": "url", "content": "http://example.com/foo" }'
curl -XPUT 'http://localhost:9200/test/tweet/3' -d '{ "user": "john doe",
"contentType": "number", "content": 123 }'
This is very similar to the example on the Elastic Search home page,
except that my message content can be of different types (a structured doc,
a url, or even just a number).
The first PUT works, but then I'm getting errors like this:
{"ok":true,"_index":"test","_type":"tweet","_id":"1","_version":2}
{"error":"MapperParsingException[object mapping for [tweet] tried to parse
as object, but got EOF, has a concrete value been provided to
it?]","status":400}
{"error":"MapperParsingException[object mapping for [tweet] tried to parse
as object, but got EOF, has a concrete value been provided to
it?]","status":400}
Any idea of what's going wrong here? Why is Elastic Search trying to parse
"http://example.com/foo" and 123 as objects?
Thanks!