spring boot与ElasticSearch的集成

本文主要介绍Spring boot与ElasticSearch的集成,因为Spring boot的教程以及ElasticSearch的学习其他博客可能更优秀,所以建议再看这篇文章前先学习学习一下Spring boot与ElasticSearch,这篇博客更着重实战

1.首先要引入依赖包

2.配置参数

     elasticsearch.cluster.name=集群名字

     elasticsearch.host=127.0.0.1

     elasticsearch.port=9300

3.配置类编写   

@Configuration
public class ElasticSearchConfig {
     @Value("${elasticsearch.host}")
     private String esHost;

    @Value("${elasticsearch.port}")
     private int esPort;

    @Value("${elasticsearch.cluster.name}")
    private String esName;

     @Bean
    public TransportClient esClient() throws UnknownHostException {
         Settings settings = Settings.builder().put("cluster.name", this.esName)
        // .put("cluster.name", "elasticsearch")
        .put("client.transport.sniff", true)
        .build();

        InetSocketTransportAddress master = new InetSocketTransportAddress(InetAddress.getByName(esHost), esPort
        // InetAddress.getByName("10.99.207.76"), 8999
       );

        TransportClient client = new PreBuiltTransportClient(settings)
       .addTransportAddress(master);

       return client;
     }
}

4,注入其相关配置信息

    @Autowired
           private TransportClient esClient;

常用方法:prepareSearch,prepareIndex,prepareUpdate等,请查询Api了解。

  

猜你喜欢

转载自www.cnblogs.com/dibinbin/p/12091850.html