Spring Data ElasticSearch TransportClient Java 配置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27053365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:07:29  来源:igfitidea点击:

Spring Data ElasticSearch TransportClient Java Config

javaspringelasticsearchspring-data-elasticsearch

提问by Nitin Arora

Does anybody know what's Java Config equivalent of :

有谁知道 Java Config 相当于:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
    <elasticsearch:transport-client id="client" cluster-nodes="localhost:9300,someip:9300"
/>
</beans>

I specifically want to use nodeBuilder()to do it.

我特别想用nodeBuilder()它来做。

回答by ?zbek

Take a look into Spring Data documentations for ElasticSearch:

查看 ElasticSearch 的 Spring Data 文档:

   @Configuration
   @EnableElasticsearchRepositories(basePackages = "org/springframework/data/elasticsearch/repositories")
        static class Config {

        @Value("${esearch.port}") int port;
        @Value("${esearch.host}") String hostname;

        @Bean
        public ElasticsearchOperations elasticsearchTemplate() {
        return new ElasticsearchTemplate(client());
         }

        @Bean
        public Client client(){
            TransportClient client= new TransportClient();
            TransportAddress address = new InetSocketTransportAddress(hostname, port); 
            client.addTransportAddress(address);
            return client;
        }
   }

Elasticsearch Repositories2.1.2 Annotation based configuration

Elasticsearch Repositories2.1.2 基于注解的配置

The Spring Data Elasticsearch repositories support cannot only be activated through an XML namespace but also using an annotation through JavaConfig.

Spring Data Elasticsearch 存储库支持不仅可以通过 XML 命名空间激活,还可以通过 JavaConfig 使用注释激活。