1

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.

cluis92
  • 664
  • 12
  • 35

1 Answers1

1

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.

Val
  • 207,596
  • 13
  • 358
  • 360
  • How _bulk without type can work on ES 6.6? Shouldn't it be `POST productv9/_doc/_bulk` ? – dejanmarich Oct 10 '19 at 08:42
  • Yeah, I tested on ES7... fixed now. – Val Oct 10 '19 at 08:45
  • @Val could you check out this question when you have time https://stackoverflow.com/questions/72001990/how-to-pass-a-list-of-values-to-an-elasticsearch-query-from-another-query – cluis92 Apr 25 '22 at 15:31