Rental Index (Dynamic Index Name) In Spring Data Elasticsearch

I have a usecase in which I want to index my document in a separate index based on a specific condition. For example, I want to save an invoice document in an index with the suffix name with it.

@Document(indexName="store_{department}", indexStoreType="invoice") public class InvoiceES{ // fields @Id private String id; @Field private String department; } 

Is it possible to achieve this using Spring Data?

If not, is there a plan for future releases of Spring Data?

+6
source share
4 answers

Vishal

Spring data elasticsearch does not currently support this feature. we already have a request for a function (pull request), which will be added soon with the next version.

Look at this pull request, https://github.com/spring-projects/spring-data-elasticsearch/pull/56

0
source

@Document (indexName = "# {'$ {elasticsearch.index.name}'}", type = "category", shards = 1, replicas = 0, refreshInterval = "-1")

He created an index: "$ {elasticsearch.index.name}"

I tried using spring -data-elasticsearch version 1.1.2.RELEASE and 1.2.0.M1, but according to jira ( https://jira.spring.io/browse/DATAES-93 ) it is fixed in version 1.1 RC1

0
source

As for spring-boot-starter-data-elasticsearch-1.5, you can achieve this with the spring el expression:

 @Bean Department department() { return new Department(); } @Document(indexName="store_#{department.name()}", indexStoreType="invoice") public class InvoiceES{} 

You can change the bean property to change the index you want to save / search:

  invoiceRepo.save(new Invoice(...)); department.setName("newName"); invoiceRepo.save(new Invoice(...)); 

What should be noted is not to split this bean into multiple threads, which can ruin your index.

0
source

The only way I found for this is to do it manually without the @Document annotation:

  client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(nodeId, port)); IndexResponse response = client.prepareIndex(your_index, type, subid) .setSource(jsonBuilder() .startObject() .field("field1", field1)) .field("fileld2", field2) ).endObject()) .execute().actionGet(); 
-2
source

Source: https://habr.com/ru/post/971116/


All Articles