Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch: Difference between an IndexRequest and an UpdateRequest

Could someone explain the difference between an IndexRequest and an UpdateRequest for Elasticsearch? The javadoc for the UpdateRequest (class level) is blank and I can't find any documentaion for it.

I found some code that wraps an IndexRequest inside an UpdateRequest before adding it to a bulk operation, but I found that the BulkRequestBuilder does not need the UpdateRequest and can take an IndexRequest directly, is there any advantage to doing this one way or the other?

IndexRequest indexRequest = new IndexRequest(indexName, typeName, docId)
    .source(doc);
UpdateRequest updateRequest = new UpdateRequest(indexName, typeName, docId)
    .doc(doc)
    .upsert(indexRequest);

I have also observed that you can set the document timestamp on an IndexRequest, but not on an UpdateRequest. If the IndexRequest is wrapped in an UpdateRequest, and the doc is a new doc, then the timestamp will be written into Elasticsearch, but if the doc already exists, then the timestamp will be ignored and will be set to the current time. Is there any documentation anywhere that describes this behavior?

like image 441
Mike Rylander Avatar asked Nov 16 '15 19:11

Mike Rylander


1 Answers

IndexRequest defines the document to add to ElasticSearch, as opposed to UpdateRequest which actually performs the addition into ElasticSearch.

Note: UpdateRequest.upsert() expects a separate IndexRequest to be used just if the document does not exist. This enables you to use a partial doc for cases where the doc already exists.

like image 126
nak Avatar answered Nov 07 '22 12:11

nak