es動態index查詢

運行時設定index

配置文件

es.index.name=demo

配置bean

@Value("${es.index.name}")
    String indexName;

    @Bean
    public String indexName(){
        return indexName;
    }

配置domain

@Document(indexName="#{@indexName}", type = "log")

dao

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);
    }

doc

相關文章
相關標籤/搜索