Search code examples
elasticsearchelasticsearch-painless

Attempting to use Elasticsearch Bulk API when _id is equal to a specific field


I am attempting to bulk insert documents into an index. I need to have _id equal to a specific field that I am inserting. I'm using ES v6.6

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

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

When I run the bulk statement it runs without any error. However, when I run the search statement it is not getting any hits. Additionally, I have many additional documents that I would like to insert in the same manner.


Solution

  • What I suggest to do is to create an ingest pipeline that will set the _id of your document based on the value of the in_stock field.

    First create the pipeline:

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

    Then you can reference the pipeline in your bulk call:

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

    By calling GET productv9/_doc/2001 you will get your document.