Tom
Tom

Reputation: 116

How to make upsert call using Spring Data Elasticsearch?

I am moving code from directly using ES API to Spring Data Elasticsearch. Couldn't find an equivalent of upsert in Spring ES UpdateQuery. Below is my current ES call.

    IndexRequest indexRequest = new IndexRequest(indexName, type, id);
    indexRequest.source(jsonForIndex, XContentType.JSON);

    UpdateRequest updateRequest = new UpdateRequest(indexName, type, id);
    updateRequest.doc(jsonForUpdate, XContentType.JSON);
    updateRequest.upsert(indexRequest);
    updateRequest.fetchSource(true);

How to make this call using UpdateQuery?

Upvotes: 0

Views: 391

Answers (1)

Abacus
Abacus

Reputation: 19451

You can do this with an UpdateQuery.

Check out the tests in ElasticsearchIntegrationTests.shouldDoUpsertIfDocumentDoesNotExist() (https://github.com/spring-projects/spring-data-elasticsearch/blob/main/src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchIntegrationTests.java#L1609-L1630)

Upvotes: 1

Related Questions