SpringBoot + Elasticsearch 6.2.4
阿新 • • 發佈:2019-02-16
一、下載 Elasticsearch
https://www.elastic.co/downloads/elasticsearch
二、修改Gradle
1 compile "org.elasticsearch:elasticsearch:6.2.3" 2 compile 'org.elasticsearch.client:elasticsearch-rest-high-level-client:6.2.3'
三、EsConfig
1 import java.io.IOException; 2 3 import org.apache.http.HttpHost; 4 import org.apache.http.client.config.RequestConfig.Builder;5 import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; 6 import org.elasticsearch.client.RestClient; 7 import org.elasticsearch.client.RestClientBuilder; 8 import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback; 9 import org.elasticsearch.client.RestClientBuilder.RequestConfigCallback;10 import org.elasticsearch.client.RestHighLevelClient; 11 import org.springframework.context.annotation.Bean; 12 import org.springframework.context.annotation.Configuration; 13 14 @Configuration 15 public class EsConfig { 16 17 private String host = "127.0.0.1"; 18 private int port = 9200; 19 privateString schema = "http"; 20 private int connectTimeOut = 1000; 21 private int socketTimeOut = 30000; 22 private int connectionRequestTimeOut = 500; 23 24 private int maxConnectNum = 100; 25 private int maxConnectPerRoute = 100; 26 27 private HttpHost httpHost = new HttpHost(host, port, schema); 28 private boolean uniqueConnectTimeConfig = true; 29 private boolean uniqueConnectNumConfig = true; 30 private RestClientBuilder builder; 31 private RestHighLevelClient client; 32 33 @Bean 34 public RestHighLevelClient client() { 35 builder = RestClient.builder(httpHost); 36 if (uniqueConnectTimeConfig) { 37 setConnectTimeOutConfig(); 38 } 39 if (uniqueConnectNumConfig) { 40 setMutiConnectConfig(); 41 } 42 client=new RestHighLevelClient(builder); 43 return client; 44 } 45 46 // 主要關於非同步httpclient的連線延時配置 47 public void setConnectTimeOutConfig() { 48 builder.setRequestConfigCallback(new RequestConfigCallback() { 49 50 @Override 51 public Builder customizeRequestConfig(Builder requestConfigBuilder) { 52 requestConfigBuilder.setConnectTimeout(connectTimeOut); 53 requestConfigBuilder.setSocketTimeout(socketTimeOut); 54 requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut); 55 return requestConfigBuilder; 56 } 57 }); 58 } 59 60 // 主要關於非同步httpclient的連線數配置 61 public void setMutiConnectConfig() { 62 builder.setHttpClientConfigCallback(new HttpClientConfigCallback() { 63 64 @Override 65 public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { 66 httpClientBuilder.setMaxConnTotal(maxConnectNum); 67 httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute); 68 return httpClientBuilder; 69 } 70 }); 71 } 72 73 public void close() { 74 if (client != null) { 75 try { 76 client.close(); 77 } catch (IOException e) { 78 e.printStackTrace(); 79 } 80 } 81 } 82 }
四、呼叫
1 @Autowired 2 private RestHighLevelClient client;
1.建立index
1 CreateIndexRequest request = new CreateIndexRequest("indexname");//index名必須全小寫,否則報錯 2 try { 3 client.indices().create(request); 4 } catch (IOException e) { 5 e.printStackTrace(); 6 }
2.新增資料
1 IndexRequest indexRequest = new IndexRequest("indexname", "typename", "id");//id為自定義 2 ObjectMapper mapper = new ObjectMapper(); 3 try { 4 indexRequest.source(mapper.writeValueAsString(object), XContentType.JSON); 5 client.index(indexRequest); 6 } catch (Exception e) { 7 e.printStackTrace(); 8 }
3.刪除資料
1 DeleteRequest deleteRequest = new DeleteRequest("indexname", "typename", id); 2 DeleteResponse response = null; 3 try { 4 response = client.delete(deleteRequest); 5 } catch (IOException e) { 6 e.printStackTrace(); 7 }
4.批量插入資料
1 BulkRequest bulkRequest = new BulkRequest(); 2 IndexRequest indexRequest = null; 3 ObjectMapper mapper = new ObjectMapper(); 4 try { 5 for (Object o : objects) { 6 indexRequest = new IndexRequest(index, type, p.getId()); 7 indexRequest.source(mapper.writeValueAsString(o), XContentType.JSON); 8 bulkRequest.add(indexRequest); 9 } 10 client.bulk(bulkRequest); 11 } catch (Exception e) { 12 e.printStackTrace(); 13 }
5.批量刪除資料
1 BulkRequest bulkRequest = new BulkRequest(); 2 DeleteRequest deleteRequest = null; 3 try { 4 for (String id : ids) { 5 deleteRequest = new DeleteRequest(index, type, id); 6 bulkRequest.add(deleteRequest); 7 } 8 client.bulk(bulkRequest); 9 } catch (Exception e) { 10 e.printStackTrace(); 11 }