How to set _id in elasticsearch 2.0
Asked Answered
U

1

9

Since the configuration of path on the _id field of a specific mapping is deprecated (as noted in the documentation here),

How can I set the _id field for a specific document in elasticsearch 2.0 ?

(In my specific use-case, I want to index all documents with my own id. I know they're all unique)

Unlucky answered 1/9, 2015 at 14:50 Comment(0)
F
20

_id being deprecated simply means that you have to specify the id explicitly and ES won't offer you to parse your document a first time just to retrieve the field you've specified as the id field.

So all the current ways of indexing your documents are still valid as long as you specify the id explicitly:

curl -XPUT localhost:9200/index/type/your_id -d '{"field1": "value1"}'
                                        ^
                                        |
                                 your id goes here

or in a bulk query

curl -XPOST localhost:9200/_bulk -d '
{"index": {"_index": "index", "_type": "type", "_id": "your_id"}}
{"field1": "value1"}                                     ^
'                                                        |
                                                 your id goes here
Furfur answered 1/9, 2015 at 14:56 Comment(3)
Damn! That's so trivial, I don't know why I didn't think of trying that. :/ (And i still think they should mention it in their documentation)Unlucky
It's somehow briefly mentioned in this blog post announcing upcoming changes in ES 2.0. Though I admit it wouldn't hurt if the doc mentioned it "officially".Furfur
It's documented with the Index API, the very first thing. "The following example inserts the JSON document into the "twitter" index, under a type called "tweet" with an id of 1" Only later in the page is the method of auto-generating the ID by using an HTTP POST introduced. EDIT: limited markdown support in comments.Decosta

© 2022 - 2024 — McMap. All rights reserved.