Search code examples
spring-bootelasticsearchspring-data-elasticsearch

How to create an Elasticseearch index with index sorting via Spring annotation


I'm using Spring Data for Elasticsearch. I need to create an index with an index sorting as it is described here

Is there a way to define a POJO field to be used as a sorting field during indexing? I'm using annotations, and that would be a preferred way, but any other options would be Ok too.


Solution

  • Currently this is not possible. Index sorting must be defined when the index is created, and as it is currently possible to define a json file with index settings and add that with @Setting to the entity, this fails in this case. The reason is, that when an index sorting is defined, the corresponding field must be defined in the mappings definition on index creation as well. Spring Data Elasticsearch first creates the index with the settings and after that it writes the mappings - which then is too late.

    Please open an issue in the issue tracker that the index creation with index sorting should be possible, we have to think about how to define the sort fields.

    Edit 28.03.2021:

    From Spring Data Elasticsearch 4.2.0.RC1 on index creation will always be in one step with writing the mapping, so it's possible to provide a settings file that will be used along with the mapping.

    It is as well possible now to define the index sorting parameters with arguments of the @Setting annotation, so no need for a json file at all.