es.index.name=demo
@Value("${es.index.name}") String indexName; @Bean public String indexName(){ return indexName; }
@Document(indexName="#{@indexName}", type = "log")
public interface DemoDao extends ElasticsearchRepository<Demo,String>{ Page<Demo> findByApp(String app, Pageable pageable); }
好處是運行時能夠指定,可是沒法動態查詢不一樣index的spring
@Autowired ElasticsearchTemplate elasticsearchTemplate; public void demo(String index,String type,String app,Pageable page){ MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("app",app); SearchQuery query = new NativeSearchQueryBuilder() .withIndices(index) .withTypes(type) .withQuery(matchQueryBuilder) .withPageable(page) .build(); Page<Demo> data = elasticsearchTemplate.queryForPage(query,Demo.class); }