Skip to content
Advertisement

Spring data elasticsearch query on multiple indices

I have multiple indices for every day in elasticsearch and I am using ElasticsearchRepository to query my documents. Documents definition:

@Getter
@Setter
@Document(indexName = "fraud-hub-digital-transactions-#{T(java.time.LocalDate).now().toString()}", createIndex = false)
@TypeAlias("fraud-hub-digital-transactions")
public class FraudDigitalTransactions {
    
    @Id
    @Field(name = "transaction_key",type = FieldType.Long)
    private Long transactionId;
    
    @Field(name = "client_no",type = FieldType.Integer)
    private Integer clientNo;
}

My index will create dynamically for every day with this definition

@Document(indexName = "fraud-hub-digital-transactions-#{T(java.time.LocalDate).now().toString()}"

My Repository definition:

@Repository
public interface FraudDigitalTransactionsRepository extends ElasticsearchRepository<FraudDigitalTransactions,String> {
    List<FraudDigitalTransactions> findByClientNo(Integer clientNo);
}

When I query by client no just returning current day values.

@Test
public void testFindByClientNo() {
    List<FraudDigitalTransactions> fraudDigitalTransactions = fraudDigitalTransactionsRepository.findByClientNo(88019237);
    Assert.assertNotEquals(fraudDigitalTransactions.size(),0);
}

Test failed. Current day values returned not searched all indices. Probably Spring data searching a document in current day indexes. Is there any way with spring data to search all indexes? I can do it with elasticsearch high-level client but I want to do with spring data features

Advertisement

Answer

You will need to search with a wildcard or alias and when writing use the index name produced by the bean.

I recently wrote a blog post how to do this with Spring Data Elasticsearch 4.1 using index templates.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement