Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use existing field as id in elasticsearch

Just started using elasticSearch today. I was wondering if it would be possible to set in some kind of global parameter to use a certain field within a document as the ID always?

My JSON documents will always have it's own unique ID

{
  "Record ID": "a06b0000004SWbdAAG",
  "System Modstamp": "01/31/2013T07:46:02.000Z",
  "body": "Test Body"
}

Here I would like to use Record ID as the ID field.

Regards

like image 728
chri Avatar asked May 20 '13 15:05

chri


People also ask

Can Id be a string in Elasticsearch?

ES document ids are always stored as strings, even if you give an integer at indexing time.

How does Elasticsearch generate ID?

Elasticsearch generates IDs by hashing the values of the fields that are used to identify documents. This ensures that documents are always identified by the same ID, even if the values of the fields change.

How do I get the field name in Elasticsearch?

You may use _field_names field. The _field_names field indexes the names of every field in a document that contains any value other than null.


1 Answers

You want to use the path setting, see the docs here:

http://www.elasticsearch.org/guide/reference/mapping/id-field/

specifically something like this should work in your mapping:

{
    "your_mapping" : {
        "_id" : {
            "path" : "Record ID"
        }
    }
}

I've never tried having variable names split up though. You might want to camelcase or underscore them if you run into wierdness.

like image 55
James R Avatar answered Nov 08 '22 08:11

James R