How to set _id in elasticsearch 2.0

10,849

_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
Share:
10,849
gillyb
Author by

gillyb

Started out programming with php when I was 13 years old, mostly creating eCommerce applications for the web. Crossed over to .net programming during my service for the IDF. Was an engineer on the 'Core Performance' team at 'ShopYourWay.com' which is a site that belongs to 'Sears'. FED team leader at Logz.io Nov 2014 -> Nov 2017 (and first employee) Worked at Google. Currently at Palo Alto Networks. Check out My technical Blog! - www.DebuggerStepThrough.com or my personal blog - www.GillyBarr.com or my github account or my twitter account

Updated on August 17, 2022

Comments

  • gillyb
    gillyb over 1 year

    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)

  • gillyb
    gillyb over 8 years
    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)
  • Val
    Val over 8 years
    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".
  • GlenRSmith
    GlenRSmith over 8 years
    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.