Elasticsearch 5 - Return field from document when bulk insert

387 views Asked by At

I'm using Elasticsearch 5.1.1, bulk inserting documents in my index and I need to get in the response one of the document's fields along with the autogenerated _id to update a database.

I have been trying requests like the following ones:

curl -XPOST localhost:9200/_bulk?pretty -d '
{ "index" : { "_index" : "articles_201701", "_type" : "articles_type" , "_source_include" : "db_id"} }
{ "db_id" : "value1" }
{ "index" : { "_index" : "articles_201701", "_type" : "articles_type" , "_source_include" : "db_id"} }
{ "db_id" : "value2" }
'


curl -XPOST localhost:9200/_bulk?pretty -d '
{ "index" : { "_index" : "articles_201701", "_type" : "articles_type", "fields" : ["db_id"]} }
{ "db_id" : "value1" }
{ "index" : { "_index" : "articles_201701", "_type" : "articles_type", "fields" : ["db_id"]} }
{ "db_id" : "value2" }
'

curl -XPOST 'localhost:9200/_bulk?pretty&fields=db_id' -d '
{ "index" : { "_index" : "articles_201701", "_type" : "articles_type" } }
{ "db_id" : "value1" }
{ "index" : { "_index" : "articles_201701", "_type" : "articles_type"} }
{ "db_id" : "value2" }
' 

Some of them with slight variations and combinations but no luck.

Not sure if this is even possible...

1

There are 1 answers

0
Lax On

Create array of objects, each object contain db_Id and Id.before calling bulk set the db_id , after calling bulk set the id field with the _id that elasticsearch generate. Now when you want to update you have Id and db_id