zoukankan      html  css  js  c++  java
  • 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了解。

      

  • 相关阅读:
    【美团技术团队文章--学习笔记】之 Java动态追踪技术探究
    mq
    为啥要读写分离
    算法 数据结构
    对扩展开放,对修改关闭
    redis 事务
    准实时数仓设计方案
    Scala Puzzlers 系列(一):占位符的使用
    【面试题】大数据开发岗位
    Hive 分区和分桶
  • 原文地址:https://www.cnblogs.com/dibinbin/p/12091850.html
Copyright © 2011-2022 走看看