动态修改拼接ElasticSearch的@Document中的index

ElasticSearch中多环境使用不同的索引,如:

dummy_point索引:

在co环境是:co_dummy_point

在dev环境是:dev_dummy_point

以下是DummyPoint类:

@Data
@NoArgsConstructor
@Document(indexName = "#{@nacosConfig.profile}" + "_" + "dummy_point")
public class DummyPointES {

    @Id
    private Long id;

    @Field(type = FieldType.Date)
    private Date gmtCreate; // 创建时间

    @Field(type = FieldType.Date)
    private Date gmtModified; // 修改时间

...
}

nacosConfig配置类如下:
 

@Component
@RefreshScope
@Data
public class NacosConfig {

    /**
     * 当前环境
     */
    @Value("${spring.cloud.nacos.config.namespace}")
    private String profile;
}

我做的就是将nacosConfig中profile拼接到es类索引中,

@Document(indexName = "#{@nacosConfig.profile}" + "_" + "dummy_point")

注意:es

elasticsearch7.x之后使用”#{@student.name}“这种引用方式,

我看其他人还有这种引用方式"#{student.name}",(但是我使用不了,会报错获取为null}

你可能感兴趣的:(elasticsearch,大数据,搜索引擎)