使用Spring-Data Elasticsearch与@Document动态创建Indexname



我试图在

中动态获取indexname
@Document(indexName="Something",type="type")

我已经尝试使用下面的代码:

@Document(indexName="#{systemProperties['databaseName'] }", type = "dbtype")

但是在运行时,当我从UI发送两个请求从ES从不同的索引获取数据时,它不能正常工作。

我该怎么做才能解决这个问题?

一个解决方法是,使用nativesearchqueries。你可以把索引设置成任何你喜欢的值:

NativeSearchQuery query = new NativeSearchQueryBuilder().withIndices(indexName)
                .withQuery(QueryBuilders.matchPhraseQuery("_all", request.getSearchTerm().getSearchString()))
                .withPageable(new PageRequest(request.getPaging(), request.getMaxResults()))
....
                .build();
        result =elastic.query(query, query->query2Result(query));

最新更新