How can I insert a data into a index in elasticsearch using java api in the most efficient way .First I need to check if the entry exists using a field lets say log_id ,if it exists that I will update that entry else i will insert a new entry into elasticsearch .Is there any method in elasticsearch which does both.
1 Answer
You can do it in BULK ( if you are doing insert and update in bulk) Following is a simple example :
check if the log_id exists already:
if existing create Update json string \n should be there,
{ "update" : {"_id" : "1", "_type" : "type1", "_index" : "index1"} }\n{ "doc" : {"field2" : "value2"} }
else create a Insert Json like below \n should be there,
{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } }\n{ "field1" : "value1" }
Have them in list and do the BULK API.