ES 6.2.3 restClient客户端 整合SSM项目

ES  6.2.3  restClient客户端 

由于项目需求,要做一个海量数据的搜索应用,考虑过solr ,最后因为聚合和地理位置的查询处理,遂选择使用 ES,

下面开始了,我的采坑之旅:

首先对于初次接触es的人大部分估计是感觉一片迷茫无从下手,怎么去连接、操作es,。。。。。。

简单说一下我的认知:

                        es的连接方式有两种协议

                        1:基于socket  的连接  比如可以使用  TransportClient 客户端连接 端口就要采用9300

               2:基于http 的连接 比如 restClient客户端连接 端口就要采用9200

选择不同的连接客户端,就要引入对应的坐标


  1. pom.xml引入坐标   :


        org.elasticsearch.client
        elasticsearch-rest-high-level-client
        6.2.3


  2. 编写restClient客户端
     import org.apache.http.HttpHost;
    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestClientBuilder;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.index.query.QueryStringQueryBuilder;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;

    public class ESClientFactory {


       private static  String HOST="127.0.0.1";  
        private static  int PORT = 9200;  
        private static final String SCHEMA = "http";  
        private static final int CONNECT_TIME_OUT = 1000;  
        private static final int SOCKET_TIME_OUT = 30000;  
        private static final int CONNECTION_REQUEST_TIME_OUT = 500;  
      
        private static final int MAX_CONNECT_NUM = 100;  
        private static final int MAX_CONNECT_PER_ROUTE = 100;  
      
        private static HttpHost HTTP_HOST=null;  
        private static boolean uniqueConnectTimeConfig = false;  
        private static boolean uniqueConnectNumConfig = true;  
        private static RestClientBuilder builder;  
        private static RestClient restClient;  
        private static RestHighLevelClient restHighLevelClient;  
         
                //初始化
        public static void init(){  
            if(HTTP_HOST==null){  
                HTTP_HOST=new HttpHost(HOST,PORT,SCHEMA);  
            }  
            builder = RestClient.builder(HTTP_HOST);  
            if(uniqueConnectTimeConfig){  
                setConnectTimeOutConfig();  
            }  
            if(uniqueConnectNumConfig){  
                setMutiConnectConfig();  
            }  
            restClient = builder.build();  
            restHighLevelClient = new RestHighLevelClient(builder);
        }  
      
        /** 
         *     主要关于异步httpclient的连接延时配置 
         */  
        public static void setConnectTimeOutConfig(){  
            builder.setRequestConfigCallback(requestConfigBuilder -> {  
                requestConfigBuilder.setConnectTimeout(CONNECT_TIME_OUT);  
                requestConfigBuilder.setSocketTimeout(SOCKET_TIME_OUT);  
                requestConfigBuilder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIME_OUT);  
                return requestConfigBuilder;  
            });  
        }  
        
        /** 
         *    主要关于异步httpclient的连接数配置 
         */  
        public static void setMutiConnectConfig(){  
            builder.setHttpClientConfigCallback(httpClientBuilder -> {  
                httpClientBuilder.setMaxConnTotal(MAX_CONNECT_NUM);  
                httpClientBuilder.setMaxConnPerRoute(MAX_CONNECT_PER_ROUTE);  
                return httpClientBuilder;  
            });  
        }  
      
        public static RestClient getClient(){  
            return restClient;  
        }  
      
        public static RestHighLevelClient getHighLevelClient(){  
            if(restHighLevelClient==null){  
                init();  
            }  
            return restHighLevelClient;  
        }  
      
        public static void close() {  
            if (restHighLevelClient != null) {  
                try {  
                    restHighLevelClient.close();  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }






你可能感兴趣的:(ES)