Spring Data ElasticSearch TransportClient Java配置

时间:2014-11-21 02:53:28

标签: java spring elasticsearch spring-data-elasticsearch

有人知道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>

我特别想使用nodeBuilder()来做这件事。

1 个答案:

答案 0 :(得分:11)

查看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 Repositories 2.1.2基于注释的配置

  

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