注意對 transport client不瞭解先閱讀官方文檔:html
transport client(傳送門)java
pom.xmlweb
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.jiatp</groupId> <artifactId>springboot-03-rest</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-03-rest</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.3.2</version> </dependency> <!-- Java Low Level REST Client --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>6.3.2</version> </dependency> <!-- Java High Level REST Client --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.3.2</version> </dependency> <!-- json轉換 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
ElasticsearchConfig.java
package com.jiatp.springboot.config; import org.apache.http.HttpHost; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.RequestConfig.Builder; import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.beans.factory.annotation.Autowire; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.io.IOException; @Configuration public class ElasticsearchConfig { @Value("${elasticsearch.host}") private String host; @Value("${elasticsearch.port}") private int port; @Value("${elasticsearch.schema}") private String schema; @Value("${elasticsearch.connectTimeOut}") private int connectTimeOut; @Value("${elasticsearch.socketTimeOut}") private int socketTimeOut; @Value("${elasticsearch.connectionRequestTimeOut}") private int connectionRequestTimeOut; @Value("${elasticsearch.maxConnectNum}") private int maxConnectNum; @Value("${elasticsearch.maxConnectPerRoute}") private int maxConnectPerRoute; private HttpHost httpHost; private boolean uniqueConnectTimeConfig = true; private boolean uniqueConnectNumConfig = true; private RestClientBuilder builder; private RestHighLevelClient client; /** * 返回一個RestHighLevelClient * * @return */ @Bean(autowire = Autowire.BY_NAME, name = "restHighLevelClient") public RestHighLevelClient client() { httpHost= new HttpHost(host, port, schema); builder = RestClient.builder(httpHost); if (uniqueConnectTimeConfig) { setConnectTimeOutConfig(); } if (uniqueConnectNumConfig) { setMutiConnectConfig(); } client = new RestHighLevelClient(builder); return client; } /** * 異步httpclient的鏈接延時配置 */ public void setConnectTimeOutConfig() { builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() { @Override public Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) { requestConfigBuilder.setConnectTimeout(connectTimeOut); requestConfigBuilder.setSocketTimeout(socketTimeOut); requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut); return requestConfigBuilder; } }); } /** * 異步httpclient的鏈接數配置 */ public void setMutiConnectConfig() { builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { httpClientBuilder.setMaxConnTotal(maxConnectNum); httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute); return httpClientBuilder; } }); } /** * 關閉鏈接 */ public void close() { if (client != null) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } }
application.ymlspring
elasticsearch: host: 192.168.x.x port: 9200 schema: http connectTimeOut: 1000 socketTimeOut: 30000 connectionRequestTimeOut: 500 maxConnectNum: 100 maxConnectPerRoute: 100
@RunWith(SpringRunner.class) @SpringBootTest public class Springboot03RestApplicationTests { @Qualifier(value = "restHighLevelClient") @Autowired RestHighLevelClient restHighLevelClient; String indexName="student"; String esType="msg"; @Test public void contextLoads() throws IOException{ RestClient restClient = RestClient.builder( new HttpHost("192.168.56.101", 9200, "http")).build(); //(1) 執行一個基本的方法,驗證es集羣是否搭建成功 Response response = restClient.performRequest("GET", "/", Collections.singletonMap("pretty", "true")); System.out.println(EntityUtils.toString(response.getEntity())); }
當現實create時則代表沒問題。apache
其它測試:json
@RunWith(SpringRunner.class) @SpringBootTest public class Springboot03RestApplicationTests { @Qualifier(value = "restHighLevelClient") @Autowired RestHighLevelClient restHighLevelClient; String indexName="student"; String esType="msg"; @Test public void contextLoads() throws IOException{ RestClient restClient = RestClient.builder( new HttpHost("192.168.56.101", 9200, "http")).build(); //(1) 執行一個基本的方法,驗證es集羣是否搭建成功 Response response = restClient.performRequest("GET", "/", Collections.singletonMap("pretty", "true")); System.out.println(EntityUtils.toString(response.getEntity())); } //建立索引 @Test public void createIndex(){ //index名必須全小寫,不然報錯 String index ="book"; CreateIndexRequest request = new CreateIndexRequest(index); try { CreateIndexResponse indexResponse = restHighLevelClient.indices().create(request); if (indexResponse.isAcknowledged()) { System.out.println("建立索引成功"); } else { System.out.println("建立索引失敗"); } } catch (IOException e) { e.printStackTrace(); } } //檢查索引 @Test public void findIndex()throws Exception{ try { Response response = restHighLevelClient.getLowLevelClient().performRequest("HEAD", "book"); boolean exist = response.getStatusLine().getReasonPhrase().equals("OK"); System.out.println(exist); } catch (IOException e) { e.printStackTrace(); } } //插入數據 @Test public void addData(){ JSONObject jsonObject = new JSONObject(); jsonObject.put("id", 3); jsonObject.put("age", 26); jsonObject.put("name", "wangwu"); jsonObject.put("date", new Date()); IndexRequest indexRequest = new IndexRequest(indexName, esType, "2").source(jsonObject); try { IndexResponse indexResponse = restHighLevelClient.index(indexRequest); System.out.println(indexResponse.getId()); } catch (Exception e) { e.printStackTrace(); } } /* * 使用XContentBuilder添加數據 * */ @Test public void addData1() throws Exception{ XContentBuilder builder = jsonBuilder(); builder.startObject(); { builder.field("user", "jiatp"); builder.timeField("postDate", new Date()); builder.field("message", "trying out Elasticsearch"); } builder.endObject(); IndexRequest indexRequest = new IndexRequest("twitter", "t_doc", "3") .source(builder).routing("my_route");//能夠添加指定路由 IndexResponse response = restHighLevelClient.index(indexRequest); System.out.println(response.status().name()); } /* * 使用Object key-pairs對象鍵 * */ @Test public void addData2() throws Exception{ IndexRequest indexRequest = new IndexRequest("twitter", "t_doc", "3") .source("user", "kimchy", "postDate", new Date(), "message", "trying out Elasticsearch"); IndexResponse response = restHighLevelClient.index(indexRequest); System.out.println(response.status().name()); } //異步方式 @Test public void testAddAsync() throws InterruptedException { ActionListener listener = new ActionListener<IndexResponse>() { @Override public void onResponse(IndexResponse indexResponse) { System.out.println("Async:" + indexResponse.status().name()); if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { // Todo } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) { // Todo } // 處理成功分片小於總分片的狀況 ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo(); if (shardInfo.getTotal() != shardInfo.getSuccessful()) { // Todo } } @Override public void onFailure(Exception e) { System.out.println("AsyncFailure:" + e.getMessage()); e.printStackTrace(); } }; IndexRequest indexRequest = new IndexRequest("twitter", "t_doc", "4") .source("user", "luxi", "postDate", new Date(), "message", "trying out Elasticsearch"); restHighLevelClient.indexAsync(indexRequest, listener); // 異步方式 Thread.sleep(2000); } /* * 查詢 * * */ // 指定routing的數據,查詢也要指定 @Test public void searchRoute()throws Exception{ GetRequest request = new GetRequest("twitter", "t_doc", "3").routing("my_route"); // 指定routing的數據,查詢也要指定 GetResponse response = restHighLevelClient.get(request); System.out.println(response.getSourceAsString()); } //查詢-額外參數 異步獲取 @Test public void getOneOp() throws IOException, InterruptedException { ActionListener<GetResponse> listener = new ActionListener<GetResponse>() { @Override public void onResponse(GetResponse documentFields) { System.out.println(documentFields.getSourceAsString()); } @Override public void onFailure(Exception e) { System.out.println("Error:" + e.getMessage()); e.printStackTrace(); } }; GetRequest request = new GetRequest("twitter", "t_doc", "2"); String[] includes = new String[]{"message", "*Date"}; // 包含的字段 String[] excludes = Strings.EMPTY_ARRAY; // 排除的字段 FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes); request.fetchSourceContext(fetchSourceContext); restHighLevelClient.getAsync(request,listener); Thread.sleep(2000); } //查詢全部 @Test public void searchAll(){ HttpEntity entity = new NStringEntity( "{ \"query\": { \"match_all\": {}}}", ContentType.APPLICATION_JSON); String endPoint = "/" + indexName + "/" + esType + "/_search"; try { Response response = restHighLevelClient.getLowLevelClient() .performRequest("POST", endPoint, Collections.<String, String>emptyMap(), entity); System.out.println(EntityUtils.toString(response.getEntity())); } catch(IOException e) { e.printStackTrace(); } } //條件查詢 姓名:李四 @Test public void test(){ try { String endPoint = "/" + indexName + "/" + esType + "/_search"; IndexRequest indexRequest = new IndexRequest(); XContentBuilder builder; try { builder = JsonXContent.contentBuilder() .startObject() .startObject("query") .startObject("match") .field("name.keyword", "lisi") .endObject() .endObject() .endObject(); indexRequest.source(builder); } catch (IOException e) { e.printStackTrace(); } String source = indexRequest.source().utf8ToString(); System.out.println(source); HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON); Response response = restHighLevelClient.getLowLevelClient().performRequest("POST", endPoint, Collections.<String, String>emptyMap(), entity); System.out.println(EntityUtils.toString(response.getEntity())); } catch (IOException e) { e.printStackTrace(); } } //條件查詢 叫kimchy的 @Test public void testSearch(){ try { SearchRequest searchRequest = new SearchRequest("twitter"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy")); sourceBuilder.from(0); sourceBuilder.size(5); searchRequest.source(sourceBuilder); SearchResponse response = restHighLevelClient.search(searchRequest); System.out.println("Hits:" + response.getHits().totalHits); response.getHits().forEach(e -> { System.out.println(e.getSourceAsString()); }); } catch(IOException e) { e.printStackTrace(); } } /** * * 查詢名字等於 lisi * 而且年齡在20和40之間 */ @Test public void serarchFuhe(){ try { String endPoint = "/" + indexName + "/" + esType + "/_search"; IndexRequest indexRequest = new IndexRequest(); XContentBuilder builder; try { builder = JsonXContent.contentBuilder() .startObject() .startObject("query") .startObject("bool") .startObject("must") .startObject("match") .field("name.keyword", "lisi") .endObject() .endObject() .startObject("filter") .startObject("range") .startObject("age") .field("gte", "20") .field("lte", "40") .endObject() .endObject() .endObject() .endObject() .endObject() .endObject(); indexRequest.source(builder); } catch (IOException e) { e.printStackTrace(); } String source = indexRequest.source().utf8ToString(); System.out.println(source); HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON); Response response = restHighLevelClient.getLowLevelClient().performRequest("POST", endPoint, Collections.<String, String>emptyMap(), entity); System.out.println(EntityUtils.toString(response.getEntity())); } catch (IOException e) { e.printStackTrace(); } } /** * 存在即更新【輸出:OK】 * OK * {"C":"Carambola","A":"Apple","B":"Banana"} * 不存在則建立【輸出:CREATED】 * CREATED * {"C":"Carambola"} * 開啓scriptedUpsert【在文檔不存在狀況下輸出:CREATED】 * {"A" : "Apple","B" : "Banana","C" : "Carambola"} */ @Test public void testUpdate() throws IOException { UpdateRequest request = new UpdateRequest("twitter", "t_doc", "7") .script(new Script(ScriptType.INLINE,"painless", "ctx._source.A='Apple';ctx._source.B='Banana'",Collections.EMPTY_MAP)) // 若是文檔不存在,使用upsert方法定義一些內容,這些內容將做爲新文檔插入 .upsert(jsonBuilder() .startObject() .field("C","Carambola") .endObject()); request.timeout(TimeValue.timeValueSeconds(2)); // 2秒超時 //request.scriptedUpsert(true); // 不管文檔是否存在,腳本都必須運行 UpdateResponse update = restHighLevelClient.update(request); System.out.println(update.status().name()); } //刪除 @Test public void delete(){ String endPoint = "/" + indexName + "/" + esType + "/_delete_by_query"; /** * 刪除條件 */ IndexRequest indexRequest = new IndexRequest(); XContentBuilder builder; try { builder = JsonXContent.contentBuilder() .startObject() .startObject("query") .startObject("term") //name中包含deleteText .field("name.keyword", "wangwu") .endObject() .endObject() .endObject(); indexRequest.source(builder); } catch (IOException e) { e.printStackTrace(); } String source = indexRequest.source().utf8ToString(); HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON); try { Response response = restHighLevelClient.getLowLevelClient().performRequest("POST", endPoint, Collections.<String, String>emptyMap(), entity); System.out.println(EntityUtils.toString(response.getEntity())); } catch (IOException e) { e.printStackTrace(); } } }
可看api進行測試,https://blog.csdn.net/jatpen/article/details/102631110api
或者查看官方文檔:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.4/java-rest-high-supported-apis.htmlspringboot