0
votes

How to specify routing key in spring data elasticsearch during indexing.

I have made a spring boot application which uses spring data elasticsearch to index documents.

Below is my document structure(without getters setters).

@Document(indexName = Constants.ES_INDEX_NAME, type = Constants.ES_TYPE,createIndex = false)
public class EsDocument {

    @Id
    private String ID;

    private String group;

    private String request;

    private String response;

    private Filters filters;

    public EsDocument() {
    }
}

public class Filters {

    private String order_id;
    private String api_type;
    private int status;
    private String statusCode;
    private String createdAt;
}

This is the code for indexing a document. I'm using ElasticSearchTemplate to index my data.

public void saveAll(List<EsDocument> esDocuments) {
        List<IndexQuery> queries = new ArrayList<>();
        for(EsDocument esDocument : esDocuments) {
            IndexQuery indexQuery = new IndexQuery();
            indexQuery.setObject(esDocument);
            indexQuery.setIndexName(esDocument.getGroup());
            queries.add(indexQuery);
        }
        if (queries.size() > 0) {
            elasticsearchTemplate.bulkIndex(queries);
        }
        logger.info("Batch Documents Saved to elastic search");
    }

Now how to add custom routing key during indexing?. I want to add filters.order_id as my routing key during indexing.

2

2 Answers

0
votes

AFAIK ElasticsearchTemplate is missing support for routing in certain areas and you have to go down to the Client to provide the necessary routing info.

Try something along the lines of

Client client = elasticsearchTemplate.getClient();
IndexRequestBuilder builder = client.prepareIndex(...);
builder.setRouting(...);
0
votes

That is what I have figured out with BulkOptions and IndexCoordinates while using bulkIndex method of the Elasticsearch Template. Please, find complete code below:

public void indexAllData(final List<MediaResourceEsDto> mediaResources, final String indexName, final String routingId) {
    final List<IndexQuery> queries = new ArrayList<>();
    for(final MediaResourceEsDto mediaResource : mediaResources) {
        final IndexQuery indexQuery = new IndexQuery();
        indexQuery.setObject(mediaResource);
        queries.add(indexQuery);
    }

    // set index name
    final IndexCoordinates indexCoordinates = IndexCoordinates.of(indexName);

    // set routing id
    final BulkOptions.BulkOptionsBuilder bulkOptionsBuilder = BulkOptions.builder();
    bulkOptionsBuilder.withRoutingId(routingId);
    final BulkOptions bulkOptions = bulkOptionsBuilder.build();

    if (queries.size() > 0) {
        elasticsearchTemplate.bulkIndex(queries, bulkOptions, indexCoordinates);
    }
}

Since I have already data in the list which I have sorted from Database and here I just pass it to the method:

indexAllData(mediaResourceList, "media_resource_index_2", "kaufland");

Hope, it helps someone.

Cheers :)

Little info about environment:

  • spring-data-elasticsearch: 4.0.0.RELEASE (most current version as of today)

  • spring-boot: 2.3.0.RELEASE (most current version as of today)