From dc74f121c53d9aca6e3391913cd5861fdae94ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Wed, 10 May 2023 12:42:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 37 +++ .../com/kwan/shuyu/SearchApplication.java | 12 + .../shuyu/config/ElasticsearchConfig.java | 33 +++ src/main/resources/application.yml | 6 + src/main/resources/logback-spring.xml | 47 +++ src/test/java/TestDemo.java | 32 +++ src/test/java/TestDocument.java | 272 ++++++++++++++++++ 7 files changed, 439 insertions(+) create mode 100644 src/main/java/com/kwan/shuyu/SearchApplication.java create mode 100644 src/main/java/com/kwan/shuyu/config/ElasticsearchConfig.java create mode 100644 src/main/resources/application.yml create mode 100644 src/main/resources/logback-spring.xml create mode 100644 src/test/java/TestDemo.java create mode 100644 src/test/java/TestDocument.java diff --git a/pom.xml b/pom.xml index 16cf573..40db940 100644 --- a/pom.xml +++ b/pom.xml @@ -13,4 +13,41 @@ 8 + + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + 7.3.0 + + + org.elasticsearch + elasticsearch + + + + + org.elasticsearch + elasticsearch + 7.3.0 + + + + org.springframework.boot + spring-boot-starter + 2.0.6.RELEASE + + + org.springframework.boot + spring-boot-starter-test + test + 2.0.6.RELEASE + + + org.projectlombok + lombok + 1.16.10 + + + \ No newline at end of file diff --git a/src/main/java/com/kwan/shuyu/SearchApplication.java b/src/main/java/com/kwan/shuyu/SearchApplication.java new file mode 100644 index 0000000..deed20f --- /dev/null +++ b/src/main/java/com/kwan/shuyu/SearchApplication.java @@ -0,0 +1,12 @@ +package com.kwan.shuyu; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + + +@SpringBootApplication +public class SearchApplication { + public static void main(String[] args) { + SpringApplication.run(SearchApplication.class,args); + } +} diff --git a/src/main/java/com/kwan/shuyu/config/ElasticsearchConfig.java b/src/main/java/com/kwan/shuyu/config/ElasticsearchConfig.java new file mode 100644 index 0000000..12c4bbd --- /dev/null +++ b/src/main/java/com/kwan/shuyu/config/ElasticsearchConfig.java @@ -0,0 +1,33 @@ +package com.kwan.shuyu.config; + +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * es的配置类 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/10 12:37 + */ +@Configuration +public class ElasticsearchConfig { + + @Value("${kwan.elasticsearch.hostlist}") + private String hostlist; + + @Bean(destroyMethod = "close") + public RestHighLevelClient restHighLevelClient() { + String[] split = hostlist.split(","); + HttpHost[] httpHostsArray = new HttpHost[split.length]; + for (int i = 0; i < split.length; i++) { + String item = split[i]; + httpHostsArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http"); + } + return new RestHighLevelClient(RestClient.builder(httpHostsArray)); + } +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..f731245 --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + application: + name: search-service +kwan: + elasticsearch: + hostlist: 47.119.160.231:9200 #多个节点用逗号分隔 \ No newline at end of file diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..bf5bcf4 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + utf8 + + + + + + + + ${LOG_HOME}/xc.%d{yyyy-MM-dd}.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + 0 + + 512 + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/TestDemo.java b/src/test/java/TestDemo.java new file mode 100644 index 0000000..ad9d2f8 --- /dev/null +++ b/src/test/java/TestDemo.java @@ -0,0 +1,32 @@ +import org.apache.http.HttpHost; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestHighLevelClient; + +import java.io.IOException; + +/** + * 普通项目查询es,获取数据 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/10 11:50 + */ +public class TestDemo { + public static void main(String[] args) throws IOException { + //1获取连接客户端 + RestHighLevelClient client = new RestHighLevelClient(RestClient.builder( + new HttpHost("47.119.160.231", 9200, "http") + )); + //2构建请求 + GetRequest getRequest = new GetRequest("book", "1"); + //3执行 + GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); + //4获取结果 + System.out.println(getResponse.getId()); + System.out.println(getResponse.getVersion()); + System.out.println(getResponse.getSourceAsString()); + } +} \ No newline at end of file diff --git a/src/test/java/TestDocument.java b/src/test/java/TestDocument.java new file mode 100644 index 0000000..297ee96 --- /dev/null +++ b/src/test/java/TestDocument.java @@ -0,0 +1,272 @@ +import com.kwan.shuyu.SearchApplication; +import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.action.DocWriteResponse; +import org.elasticsearch.action.bulk.BulkItemResponse; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.bulk.BulkResponse; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.support.replication.ReplicationResponse; +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.action.update.UpdateResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.common.xcontent.XContentType; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + + +@Slf4j +@SpringBootTest(classes = SearchApplication.class) +@RunWith(SpringRunner.class) +public class TestDocument { + @Autowired + RestHighLevelClient client; + + @Test + public void testGet() throws IOException { +// 1构建请求 + GetRequest getRequest = new GetRequest("test_post", "1"); +// ===========可选参数========== +// String[] includes=new String[]{"user","message"}; +// String[] excludes= Strings.EMPTY_ARRAY; +// FetchSourceContext fetchSourceContext=new FetchSourceContext(true, includes, excludes); +// getRequest.fetchSourceContext(fetchSourceContext); +// String[] includes=Strings.EMPTY_ARRAY; +// String[] excludes= new String[]{"user","message"}; +// FetchSourceContext fetchSourceContext=new FetchSourceContext(true, includes, excludes); +// getRequest.fetchSourceContext(fetchSourceContext); +// getRequest.routing("routing"); +// 2执行 +// 同步查询 + GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); + //异步查询 +// ActionListener listener=new ActionListener() { +// //成功时 +// public void onResponse(GetResponse getResponse) { +// System.out.println(getResponse.getId()); +//// System.out.println(getResponse.getVersion()); +//// System.out.println(getResponse.getSourceAsString()); +// } +// //失败时 +// public void onFailure(Exception e) { +// e.printStackTrace(); +// } +// }; +// +// client.getAsync(getRequest, RequestOptions.DEFAULT, listener); +// +// try { +// Thread.sleep(5000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// 3获取结果 + if (getResponse.isExists()) { + System.out.println(getResponse.getId()); + System.out.println(getResponse.getVersion()); + System.out.println(getResponse.getSourceAsString());//以string获取数据 + System.out.println(getResponse.getSourceAsBytes());////以Bytes获取数据 + System.out.println(getResponse.getSourceAsMap());//以Map获取数据 + } else { + log.info("数据不存在"); + } + } + + @Test + public void testAdd() throws IOException { +// PUT test_post/_doc/2 +// { +// "user":"tomas", +// "postDate":"2019-07-18", +// "message":"trying out es1" +// } +// 1构建请求 + IndexRequest request = new IndexRequest("test_post"); + request.id("5"); +// ======================构建文档数据======================= + //方法1 + String jsonString = "{\n" + + " \"user\":\"tomas\",\n" + + " \"postDate\":\"2019-07-18\",\n" + + " \"message\":\"trying out es1\"\n" + + "}"; + request.source(jsonString, XContentType.JSON); + //方法2 +// Map jsonMap = new HashMap(); +// jsonMap.put("user", "tomas"); +// jsonMap.put("postDate", "2019-07-18"); +// jsonMap.put("message", "trying out es1"); +// request.source(jsonMap); + //方法3 +// XContentBuilder builder = XContentFactory.jsonBuilder(); +// builder.startObject(); +// { +// builder.field("user", "tomas"); +// builder.field("message", "trying out es1"); +// builder.timeField("postDate", "2019-07-18"); +// } +// builder.endObject(); +// request.source(builder); + //方法4 +// request.source("user", "tomas", +// "message", "trying out es1", +// "postDate", "2019-07-18"); +// =====可选参数== + //设置超时时间 +// request.timeout("1s"); +// request.timeout(TimeValue.timeValueSeconds(1)); + //手动维护版本号 +// request.version(4); +// request.versionType(VersionType.EXTERNAL); +// 2执行 + //同步 + IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); +// 异步 +// ActionListener listener=new ActionListener() { +// //成功时 +// public void onResponse(IndexResponse indexResponse) { +// System.out.println(indexResponse.getIndex()); +// System.out.println(indexResponse.getId()); +// System.out.println(indexResponse.getResult()); +// } +// //失败时 +// public void onFailure(Exception e) { +// +// } +// }; +// +// client.indexAsync(request, RequestOptions.DEFAULT, listener); +// try { +// Thread.sleep(5000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// 3获取结果 + System.out.println(indexResponse.getIndex()); + System.out.println(indexResponse.getId()); + System.out.println(indexResponse.getResult()); + if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { + DocWriteResponse.Result result = indexResponse.getResult(); + System.out.println("CREATE" + result); + } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) { + DocWriteResponse.Result result = indexResponse.getResult(); + System.out.println("UPDATED" + result); + } else { + } +// "_shards" : { +// "total" : 2, +// "successful" : 1, +// "failed" : 0 +// } + ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo(); + if (shardInfo.getTotal() != shardInfo.getSuccessful()) { + System.out.println("处理成功的分片数少于总分片!"); + } + if (shardInfo.getFailed() > 0) { + for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) { + String reason = failure.reason();//每一个错误的原因 + System.out.println(reason); + } + } + } + + @Test + public void testUpdate() throws IOException { +// POST test_post/_doc/3/_update +// { +// "doc":{ +// "user":"tomas Lee" +// } +// } +// 1创建请求 + UpdateRequest request = new UpdateRequest("test_post", "3"); + Map jsonMap = new HashMap<>(); + jsonMap.put("user", "tomas Lee"); + request.doc(jsonMap); +// =========可选参数======== + request.timeout("1s"); + request.retryOnConflict(3);//重试次数 +// 2执行 + //同步 + UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT); + //异步 +// 3获取结果 + updateResponse.getId(); + updateResponse.getIndex(); + //判断结果 + if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) { + DocWriteResponse.Result result = updateResponse.getResult(); + System.out.println("CREATED:" + result); + } else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) { + DocWriteResponse.Result result = updateResponse.getResult(); + System.out.println("UPDATED:" + result); + } else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) { + DocWriteResponse.Result result = updateResponse.getResult(); + System.out.println("DELETED:" + result); + } else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) { + //没有操作 + DocWriteResponse.Result result = updateResponse.getResult(); + System.out.println("NOOP:" + result); + } + } + + @Test + public void testDelete() throws IOException { +// 1创建请求 + DeleteRequest request = new DeleteRequest("test_post", "3"); +//可选参数 +// 2执行 + DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT); +// 3获取结果 + deleteResponse.getId(); + deleteResponse.getIndex(); + DocWriteResponse.Result result = deleteResponse.getResult(); + System.out.println(result); + } + + @Test + public void testBulk() throws IOException { + // 1创建请求 + BulkRequest request = new BulkRequest(); +// request.add(new IndexRequest("post").id("1").source(XContentType.JSON,"field","1" )); +// request.add(new IndexRequest("post").id("2").source(XContentType.JSON,"field","2" )); + request.add(new UpdateRequest("post", "1").doc(XContentType.JSON, "field", "3")); + request.add(new DeleteRequest("post").id("2")); + // 2执行 + BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT); + // 3获取结果 + for (BulkItemResponse itemResponse : bulkResponse) { + DocWriteResponse response = itemResponse.getResponse(); + switch (itemResponse.getOpType()) { + case INDEX: + IndexResponse indexResponse = (IndexResponse) response; + System.out.println("INDEX:" + indexResponse.getResult()); + break; + case CREATE: + IndexResponse createResponse = (IndexResponse) response; + System.out.println("CREATE:" + createResponse.getResult()); + break; + case UPDATE: + UpdateResponse updateResponse = (UpdateResponse) response; + System.out.println("UPDATE:" + updateResponse.getResult()); + break; + case DELETE: + DeleteResponse deleteResponse = (DeleteResponse) response; + System.out.println("DELETE:" + deleteResponse.getResult()); + break; + } + } + } +} \ No newline at end of file -- GitLab