使用elasticsearch nodejs时如何设置文档id API

How to set document id when using elasticsearch nodejs API

我的要求很简单,我需要用给定的 ID 在 ES 中索引文档,在开发工具中,我可以通过在路径参数中传递 id 来实现 PUT my-index-000001/_doc/1? 当我尝试做同样的事情时使用节点 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

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

如果您看到上面的错误消息,这清楚地表明您不能将 _id 传递到您的文档正文中。

您需要提供 id 外部文档正文,如下所示(这只是示例代码,因为您尚未提供示例代码,不确定您是如何索引文档的):

 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);
 });