Set _id


(Baptiste Rochez) #1

Hello

I would like to set the value of the _id field (when i create a document) with the value of a specific field. It try this method https://www.elastic.co/guide/en/elasticsearch/reference/1.4/mapping-id-field.html with the path but it doesn't work..

This is my json code
{  
     "document":{
	 "_id" : {
            "path" : "Service-Card.Service-Name",
	        "index" : "not_analyzed",
            "store" : true
        },
      	    "properties":{ 
                      "Service-Card":{"type":"object",
		 	       "properties":{
                                  "ID":{"type":"string"},
                                  "Service-Name":{"type":"string"},
                                  "LiveWebUrl":{"type":"string"},
                                  "Categories":{"type":"nested",
                                      "properties":{
                                           "Selection":{"type" : "string"}
				             }
                        },
                    "Staff":{"type":"nested",
                        "properties":{
                            "Responsible":{"type" : "string"},
                            "Developers" : {"type" : "string"},
                            "Users":{"type": "string"}
                            }
			},	
                     "RSS":{"type":"nested",
                        "properties":{
                            "JiraRRS":{"type" : "string"},
                            "LiveStatusRSS": {"type" : "string"}
                            }
			    }	
                   }
	         }
        }
    }
}

And this is the exemple to create a document

{ "index" : { "_index" : "myindex", "_type" : "document" } }

{"ID":"T4" ,"Service-Name":"Test4","LiveWebUrl":"http://test4.ch" ,"Categories": {"Selection":"Cat4"},"Staff":{"Responsible":"Admin4","Developers":"Test4IT","User":"Ice"},"RSS":{"JiraRSS": "http://test4.ch/jirarss","LiveStatusRSS":"http://test4.ch/LiveRSS"}}

When I create I document I would like to set the _id value with the Service-Name value how can I do this ?

I'm using a Chrome extension called Advence Rest Client

Cheers


(Colin Goodheart-Smithe) #2

You have set the path to Service-Card.Service-Name but that field doe not exist in your example document. I think you mean to set path to Service-Name.

However, in general, it is advisable to explicitly set the id in your application code rather than extracting it from a field in the document. The reason for this is that the bulk API works by parsing the index instruction line on the receiving node and then passing the document onto the relevant shard to parse the document. If the id needs to be extracted from the document it means the receiving node must parse the document to work out which shard to sent it, and then the shard must arse it again to index it. I advise would update your application code to set the id and the same value as 'Service-Name' when building the bulk request.


(Baptiste Rochez) #3

Oh okay so If I set the id in my application code then I just need to adjust my request to look for the id and not the _id field right ?


(Colin Goodheart-Smithe) #4

Not quite, what I mean is your application should produce the following in the bulk request:

{ "index" : { "_index" : "myindex", "_type" : "document", "_id": "Test4" } }

{"ID":"T4" ,"Service-Name":"Test4","LiveWebUrl":"http://test4.ch" ,"Categories": {"Selection":"Cat4"},"Staff":{"Responsible":"Admin4","Developers":"Test4IT","User":"Ice"},"RSS":{"JiraRSS": "http://test4.ch/jirarss","LiveStatusRSS":"http://test4.ch/LiveRSS"}}

i.e. your application gets the value of Service-Name and sets '_id' to this value. If you are using one of the language clients then you look at its documentation to see how to set the _id for each operation in the bulk request.

When retrieving a document it will have it's _id field set to that value you passed in on the index request.


(Baptiste Rochez) #5

Okay , I see it

I understand now !

Tanks a lot ! ;D


(system) #6