首页 > 解决方案 > 当 _id 等于特定字段时尝试使用 Elasticsearch Bulk API

问题描述

我正在尝试将文档批量插入索引。我需要让 _id 等于我要插入的特定字段。我正在使用 ES v6.6

POST productv9/_bulk
{ "index" : { "_index" : "productv9", "_id": "in_stock"}}
{ "description" : "test", "in_stock" : "2001"}

GET productv9/_search
{
  "query": {
    "match": {
      "_id": "2001"
    }
  }
}

当我运行批量语句时,它运行时没有任何错误。但是,当我运行搜索语句时,它没有得到任何点击。此外,我还有许多其他文档,我想以相同的方式插入。

标签: elasticsearchelasticsearch-painless

解决方案


我建议做的是创建一个摄取管道,它将根据字段_id的值设置您的文档。in_stock

首先创建管道:

PUT _ingest/pipeline/set_id
{
  "description" : "Sets the id of the document based on a field value",
  "processors" : [
    {
      "set" : {
        "field": "_id",
        "value": "{{in_stock}}"
      }
    }
  ]
}

然后您可以在批量调用中引用管道:

POST productv9/doc/_bulk?pipeline=set_id
{ "index" : {}}
{ "description" : "test", "in_stock" : "2001"}

通过打电话GET productv9/_doc/2001,你会得到你的文件。


推荐阅读