【问题标题】:Where can I find the reference for the spring-data-elasticsearch configuration properties?在哪里可以找到 spring-data-elasticsearch 配置属性的参考?
【发布时间】:2021-07-05 20:52:42
【问题描述】:

在网上,我可以看到spring-data-elasticsearch 有一些配置属性,您可以在application.properties 中定义这些属性,例如:

spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.cluster-name=elasticsearch

elasticsearch.index.name=my_index
elasticsearch.user.type=user

但是,在 IntelliJ 中,我可以看到,例如:

spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.cluster-name=elasticsearch

... 实际上现在已被弃用。但是,我似乎无法在 spring-data-elasticsearch 文档中找到任何内容,这些文档要么列出了可用的属性,要么列出了应替换为已弃用的属性。

欢迎任何帮助。提前致谢!

【问题讨论】:

  • This 可能会有所帮助。

标签: java spring spring-boot spring-data spring-data-elasticsearch


【解决方案1】:

根据Spring docs for Spring Data Elasticsearch

TransportClientElasticsearch 7 起已弃用,并将 在 Elasticsearch 8 中删除。 Spring Data Elasticsearch 将支持 TransportClient 只要在使用过的 Elasticsearch 中可用 版本,但自版本 4.0 起已弃用使用它的类

注意: 这意味着 Spring 团队还将弃用 Elasticsearch 7 支持的旧属性。

现在,Spring 团队建议开发人员使用RestHighLevelClient,它现在是Elasticsearch 的默认客户端。它是TransportClient 的直接替代品,因为它接受并返回相同的请求/响应对象。

下面给出的演示代码:

@Configuration
public class ElasticSearchConfig extends AbstractElasticsearchConfiguration {

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {

       ClientConfiguration clientConfiguration = ClientConfiguration.builder()  
            .connectedTo("localhost:9300")
            .build();

        return RestClients.create(clientConfiguration).rest();                         
    }
}

以上配置需要通过覆盖默认bean来创建。此外,您不需要明确提供集群名称。它会自动找到它。

阅读以上链接文档以供参考,因为它包含所有必要信息。

【讨论】:

    【解决方案2】:

    这些属性来自spring-boot-starter-data-elasticsearch,而不是来自spring-data-elasticsearch

    正如@code_mechanic 所建议的,在Spring Boot Reference Documentation > Common Application properties > Data 中,您将找到可用于当前 版本的Spring Boot 的属性。这里有一些与 Elasticsearch 相关的属性:

    Key Default Value Description
    spring.data.elasticsearch.client.reactive.connection-timeout Connection timeout.
    spring.data.elasticsearch.client.reactive.endpoints Comma-separated list of the Elasticsearch endpoints to connect to.
    spring.data.elasticsearch.client.reactive.max-in-memory-size Limit on the number of bytes that can be buffered whenever the input stream needs to be aggregated.
    spring.data.elasticsearch.client.reactive.password Credentials password.
    spring.data.elasticsearch.client.reactive.socket-timeout Read and Write Socket timeout.
    spring.data.elasticsearch.client.reactive.use-ssl false Whether the client should use SSL to connect to the endpoints.
    spring.data.elasticsearch.client.reactive.username Credentials username.
    spring.data.elasticsearch.repositories.enabled true Whether to enable Elasticsearch repositories.
    spring.elasticsearch.rest.connection-timeout 1s Connection timeout.
    spring.elasticsearch.rest.password Credentials password.
    spring.elasticsearch.rest.read-timeout 30s Read timeout.
    spring.elasticsearch.rest.uris [http://localhost:9200] Comma-separated list of the Elasticsearch instances to use.
    spring.elasticsearch.rest.username Credentials username.

    Spring Boot之前版本的参考文档可以在https://spring.io/projects/spring-boot#learn找到。

    您可能还对Spring Boot Docs > Spring Boot Features > Working With Nosql Technologies > Elasticsearch 感兴趣,它描述了如何使用 REST 客户端和反应式 REST 客户端进行连接、您需要的依赖项和配置属性。

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 2013-04-12
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      相关资源
      最近更新 更多