如何在 spring 数据 Elasticsearch 中设置注释属性

How to set the annotation properties in spring data Elasticsearch

我在spring数据中有一个问题elasticsearch.I想知道我们是否可以设置@Document annotation[=的注释值20=] 来自属性文件或动态设置。

例如:-

@Document(indexName = "myindex",type="mytype")
public class DocumentModel {
    ......
}  

在这里,我想从 .properties 文件设置此注释的值,或者使用一些 setter 方法来设置此注释的值,而不是对其进行硬编码。有什么正确的方法可以做到这一点吗?请帮忙!

如果你使用的是 elasticsearchTemplate,有一个更简单的变体,你可以这样做:

IndexQuery indexQuery = new IndexQueryBuilder()
     .withId(docModel.getId())
     .withObject(docModel)
     .withIndex("myindex"+docModel.getUserId()).withTypes(<type_name>).build();

对 withIndex("...") 的调用将覆盖您在 @Document 注释中的任何索引名称