Home > Software engineering >  Spring Elasticsearch - bulk save multiple indices in one line?
Spring Elasticsearch - bulk save multiple indices in one line?

Time:11-16

I have multiple documents with different index name that bulk saves in elasticsearch:

public void bulkCreateOrUpdate(List personUpdateList, List addressUpdateList, List positionUpdateList) {
    this.operations.bulkUpdate(personUpdateList,Person.class);
    this.operations.bulkUpdate(addressUpdateList, Address.class);
    this.operations.bulkUpdate(positionUpdateList, Position.class);
}

However, is this still possible to be optimized by calling just a single line, saving multiple list of different index types?

CodePudding user response:

Tldr;

The bulk api certainly allows for it.

This is a valid call

POST _bulk
{"index":{"_index":"index_1"}}
{"data":"data"}
{"index":{"_index":"index_2"}}
{"data":"data"}

How does your Java Client deal with it ... I am not sure.

Solution

This could be done:

BulkRequest.Builder br = new BulkRequest.Builder();

br.operations(op -> op           
        .index(idx -> idx            
            .index("index_1")       
            .id("1")
            .document(document)
        )
    );

br.operations(op -> op           
        .index(idx -> idx            
            .index("index_2")       
            .id("1")
            .document(document)
        )
    );

This could be done this way:

BulkRequest request = new BulkRequest(); 

request.add(new IndexRequest("index_1").id("1")  
        .source(XContentType.JSON,"data", "data"));
request.add(new IndexRequest("index_2").id("1")  
        .source(XContentType.JSON,"data", "data"));

CodePudding user response:

For Spring Data Elasticsearch :

The ElasticsearchOperations.bulkXXX() methods take a List<IndexQuery> as first parameter. You can set an index name on each of these objects to specify in which index the data should be written/updated. The index name taken from the last parameter (either the entity class or an IndexCoordinates object) is used in case that no index name is set in the IndexQuery.

  • Related