Elasticsearch

写在前面的话:读书破万卷,编码如有神
--------------------------------------------------------------------

最简单的在java客户端连接es服务器

(仅作为快速简单使用demo)

 1 package com.es.util.elasticsearch;
 2 
 3 import lombok.Data;
 4 import org.apache.log4j.Logger;
 5 import org.elasticsearch.action.search.SearchRequestBuilder;
 6 import org.elasticsearch.action.search.SearchResponse;
 7 import org.elasticsearch.client.transport.TransportClient;
 8 import org.elasticsearch.common.settings.Settings;
 9 import org.elasticsearch.common.transport.InetSocketTransportAddress;
10 import org.elasticsearch.common.unit.TimeValue;
11 import org.elasticsearch.transport.client.PreBuiltTransportClient;
12 import org.springframework.stereotype.Component;
13 
14 import javax.annotation.PostConstruct;
15 import java.net.InetAddress;
16 import java.net.UnknownHostException;
17 
18 /**
19  * @author : huobaopaocai
20  * @date : 2018/2/15 - 10:31
21  * @Description : elasticsearch客户端
22  */
23 @Component("elasticsearchConstant")
24 @Data
25 public class ElasticsearchConstant {
26 
27     public static final Logger LOG = Logger.getLogger(ElasticsearchConstant.class);
28 
29     /**
30      * es服务器地址
31      */
32     private String host = "127.0.0.1";
33 
34     /**
35      * es服务器端口
36      */
37     private Integer port = 9300;
38 
39     /**
40      * es集群名称
41      */
42     private String clusterName = "huobaopaocai-es-cluster";
43 
44     /**
45      * es索引名称
46      */
47     private String esIndex = "ecommerce";
48 
49     /**
50      * es索引下type名称
51      */
52     private String esType = "product";
53 
54     public TransportClient client = null;
55 
56     private SearchRequestBuilder requestBuilder;
57 
58     private Long TIME_OUT = 20L;
59 
60     @PostConstruct
61     public void init() {
62         Settings settings = Settings.builder()
63                 .put("cluster.name", clusterName)
64                 .put("client.transport.sniff", true)
65                 .put("client.transport.ping_timeout", "600s")
66                 .build();
67         try {
68             client = new PreBuiltTransportClient(settings)
69                     .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
70         } catch (UnknownHostException e) {
71             LOG.error(e, e.getCause());
72         }
73     }
74 
75     /**
76      * 初始化SearchRequestBuilder
77      * @return
78      */
79     public SearchRequestBuilder initRequestBuilder() {
80         requestBuilder = client.prepareSearch(esIndex).setTypes(esType);
81         return requestBuilder;
82     }
83 
84     /**
85      * 执行es操作
86      * @param requestBuilder
87      * @return
88      */
89     public SearchResponse execute(SearchRequestBuilder requestBuilder) {
90         SearchResponse searchResponse = requestBuilder.setTimeout(TimeValue.timeValueSeconds(TIME_OUT)).execute().actionGet();
91         return searchResponse;
92     }
93 }

--------------------------------------------------------------------

原文地址:https://www.cnblogs.com/xinhuaxuan/p/8449328.html