Home > Net >  How to set document id when using elasticsearch nodejs API
How to set document id when using elasticsearch nodejs API

Time:06-01

My requirements are simple, I need to index a document in ES with a given ID, in the dev tools I can do so by passing the id in path parameters PUT my-index-000001/_doc/1? when I try to do the same using nodejs API by passing _id ... I get the error mapper_parsing_exception: [mapper_parsing_exception] Reason: Field [_id] is a metadata field and cannot be added inside a document. Use the index API request parameters. the same applies if I try in dev tools too, so how can I set a document _id noting am doing so in order to set a parent:child relationship within the same index like explained here

CodePudding user response:

Field [_id] is a metadata field and cannot be added inside a document.

If you see above error message, it is clearly indicated that you cannot pass _id into your document body.

You need to provide id outside document body as shown below (this is just example code as you have not provided sample code not sure how you are indexing document):

 client.index({
     index: 'blog',
     id: '1',
     type: 'posts',
     body: {
         "PostName": "Integrating Elasticsearch Into Your Node.js Application",
         "PostType": "Tutorial",
         "PostBody": "This is the text of our tutorial about using Elasticsearch in your Node.js application.",
     }
 }, function(err, resp, status) {
     console.log(resp);
 });
  • Related