Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring数据文档注释中使用环境变量定义elasticsearch索引名_Java_Spring Boot_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Java,Spring Boot,elasticsearch" /> elasticsearch,Java,Spring Boot,elasticsearch" />

Java 在spring数据文档注释中使用环境变量定义elasticsearch索引名

Java 在spring数据文档注释中使用环境变量定义elasticsearch索引名,java,spring-boot,elasticsearch,Java,Spring Boot,elasticsearch,我有一个spring boot(v2.4.1)REST应用程序,它使用spring数据ElasticSearch(v4.1.2)和ElasticSearch客户端v7.9.3连接到ElasticSearch索引。我没有SpringXML配置文件,所有这些都是在注释或代码中完成的 我想从环境属性中获取索引的名称 使用org.springframework.data.ElasticSearch.core.elasticsearchrestemplate完成与ElasticSearch的交互,该模板是

我有一个spring boot(v2.4.1)REST应用程序,它使用spring数据ElasticSearch(v4.1.2)和ElasticSearch客户端v7.9.3连接到ElasticSearch索引。我没有SpringXML配置文件,所有这些都是在注释或代码中完成的

我想从环境属性中获取索引的名称

使用
org.springframework.data.ElasticSearch.core.elasticsearchrestemplate
完成与ElasticSearch的交互,该模板是使用
RestHighLevelClient
实例创建的

索引的名称未在模板或客户端中指定,而是在实体的文档注释中定义:

@Document(indexName="cars", createIndex=false)
public class Car{
}
因此,我要的不是硬编码的“cars”值,而是来自系统属性ES_INDEX_NAME的值

失败尝试的示例:

@Document(indexName="#{ systemProperties['es.index.name'] }", createIndex=false)
错误:
EL1007E:在null上找不到属性或字段“systemProperties”

@Document(indexName="#{@environment.getProperty('es.index.name')}", createIndex=false)
错误:
EL1057E:没有在上下文中注册bean解析器来解析对bean“环境”的访问。

在组件字段中获取环境变量并在注释中引用此bean的尝试也失败:

@Component
public class ElasticSearchConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchConfig.class);
 
    @Value("${es.index.name}")
    private String esIndexName;

    @PostConstruct
    public void afterInit() {
        LOGGER.info("Initialized ElasticSearchConfig: esIndexName="+esIndexName);
    }

    public String getEsIndexName() {
        return esIndexName;
    }

    public void setEsIndexName(String esIndexName) {
        this.esIndexName = esIndexName;
    }
}
日志打印正确的值,但:

@Document(indexName="#{@elasticSearchConfig.esIndexName}", createIndex=false)
错误:
EL1057E:上下文中没有注册bean解析器来解析对bean'elasticSearchConfig'的访问。

@Document(indexName="#{elasticSearchConfig.esIndexName}", createIndex=false)
错误:
EL1007E:在null上找不到属性或字段“elasticSearchConfig”

@Document(indexName="#{@environment.getProperty('es.index.name')}", createIndex=false)
所有这些尝试都是研究这个问题的结果,但都没有成功


还有什么办法解决这个问题吗?

我不知道如何解决,但在另一次搜索尝试后,我找到了以下链接:

它解释了如何使用带有单独索引信息的
ElasticsearchRestTemplate

在我的文档中,我更改了标题注释,以确保它未使用此配置:

@Document(indexName="notused", createIndex=false)
我添加了一个新组件,用于处理与ElasticSearchTemplate的所有交互: (包括所有导入,因为我不喜欢答案,而不知道要使用的正确类)

import org.elasticsearch.client.RestHighLevelClient;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Value;
导入org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
导入org.springframework.data.elasticsearch.core.SearchHit;
导入org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
导入org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
导入org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
导入org.springframework.stereotype.Component;
不要进口你的商务车;
导入not.your.business.CarRef;
@组成部分
公共类弹性搜索服务{
私有静态最终记录器Logger=LoggerFactory.getLogger(ElasticSearchService.class);
@值(${bascorp.elasticsearch.index.name})
私有字符串esIndexName;
@自动连线
私有RestHighLevelClient RestHighLevelClient;
私有索引协调索引信息;
私有ElasticsearchRestTemplate elasticsearchTemplate;
@施工后
public void afterInit(){
LOGGER.info(“初始化的ElasticSearchConfig:esIndexName=“+esIndexName”);
elasticsearchTemplate=新的ElasticsearchRestTemplate(restHighLevelClient);
indexInfo=IndexCoordinates.of(esIndexName);
}
公共车辆(长车)
{
elasticsearchTemplate.delete(新CarRef(carId),indexInfo);
}
公共空间拯救(汽车){
elasticsearchTemplate.index(新的IndexQueryBuilder()
.withId(car.getId().toString())
.withObject(car.build(),indexInfo);
}
公共列表搜索(NativeSearchQuery查询,类resultType){
返回elasticsearchTemplate.search(查询、结果类型、索引信息).getSearchHits();
}
}
诀窍在于额外的参数
IndexCoordinates
,它保存要使用的索引的名称

我用什么好方法结束这周