Search code examples
spring-bootelasticsearchspring-data-elasticsearch

How to do bulk update using elasticsearchOperations in Spring data elasticsearch?


We were doing bullk insert by using bulkIndex as follow:

List<IndexQuery> indexQueries = new ArrayList<>();
        for (FooDocument fooDocument : fooDocuments) {
            IndexQuery query = new IndexQueryBuilder()
                    .withId(String.valueOf(fooDocument.getId()))
                    .withObject(fooDocument)
                    .build();
            indexQueries.add(query);
        }
    elasticsearchOperations.bulkIndex(indexQueries, IndexCoordinates.of("foo_index_3"));

We also want to use for bulk updates something like:

elasticsearchOperations.bulkUpdate(updateQueries, IndexCoordinates.of("foo_index_3"));

But bulkUpdate requires list of UpdateQuery.I am trying to create it by doing something like:

List<IndexQuery> updateQueries = new ArrayList<>();
        for (FooDocument fooDocument : fooDocuments) {
            UpdateQuery updateQuery = new UpdateQueryBuilder()//which Builder class and method is required?
                    .withId(String.valueOf(fooDocument.getId()))
                    .withObject(fooDocument)
                    .build();
            updateQueries.add(query);
        }

But unlike IndexQueryBuilder there is no UpdateQueryBuilder() available, what is the correct way to build the UpdateQuery and which Builder class should we use? I am wondering whether UpdateQueryBuilder class has been deprecated.

P.S: we are using 4.0.2.RELEASE version of spring-data-elasticsearch


Solution

  • You create an UpdateQuery with a builder like this:

    UpdateQuerybuilder builder = UpdateQuery.builder(id)
       .with(...)
       .build();
    

    Here the builder is a nested class and not a separate one.