From 07d36e090017628afdc8e58d903132aa989a804c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Sat, 13 May 2023 11:15:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=BC=80=E5=90=AF=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.yml | 3 + src/main/resources/logback-spring.xml | 47 ++++++++ .../TestDocument_01_get.java | 2 +- .../TestDocument_02_get.java | 2 +- .../TestDocument_03_add.java | 2 +- .../TestDocument_04_add.java | 2 +- .../TestDocument_05_add.java | 2 +- .../TestDocument_06_add.java | 2 +- .../TestDocument_07_add.java | 2 +- .../TestDocument_08_update.java | 2 +- .../TestDocument_09_delete.java | 2 +- .../TestDocument_10_bulk.java | 2 +- .../java/es_02_index/TestIndex_01_Create.java | 100 ++++++++++++++++++ .../java/es_02_index/TestIndex_02_Create.java | 91 ++++++++++++++++ .../java/es_02_index/TestIndex_03_Create.java | 99 +++++++++++++++++ .../java/es_02_index/TestIndex_04_Async.java | 94 ++++++++++++++++ .../java/es_02_index/TestIndex_05_delete.java | 42 ++++++++ .../java/es_02_index/TestIndex_06_delete.java | 47 ++++++++ .../java/es_02_index/TestIndex_07_exists.java | 38 +++++++ .../java/es_02_index/TestIndex_08_close.java | 37 +++++++ .../java/es_02_index/TestIndex_09_open.java | 37 +++++++ 21 files changed, 645 insertions(+), 10 deletions(-) create mode 100644 src/main/resources/logback-spring.xml rename src/test/java/{es_01 => es_01_document}/TestDocument_01_get.java (98%) rename src/test/java/{es_01 => es_01_document}/TestDocument_02_get.java (98%) rename src/test/java/{es_01 => es_01_document}/TestDocument_03_add.java (99%) rename src/test/java/{es_01 => es_01_document}/TestDocument_04_add.java (99%) rename src/test/java/{es_01 => es_01_document}/TestDocument_05_add.java (99%) rename src/test/java/{es_01 => es_01_document}/TestDocument_06_add.java (98%) rename src/test/java/{es_01 => es_01_document}/TestDocument_07_add.java (99%) rename src/test/java/{es_01 => es_01_document}/TestDocument_08_update.java (98%) rename src/test/java/{es_01 => es_01_document}/TestDocument_09_delete.java (98%) rename src/test/java/{es_01 => es_01_document}/TestDocument_10_bulk.java (99%) create mode 100644 src/test/java/es_02_index/TestIndex_01_Create.java create mode 100644 src/test/java/es_02_index/TestIndex_02_Create.java create mode 100644 src/test/java/es_02_index/TestIndex_03_Create.java create mode 100644 src/test/java/es_02_index/TestIndex_04_Async.java create mode 100644 src/test/java/es_02_index/TestIndex_05_delete.java create mode 100644 src/test/java/es_02_index/TestIndex_06_delete.java create mode 100644 src/test/java/es_02_index/TestIndex_07_exists.java create mode 100644 src/test/java/es_02_index/TestIndex_08_close.java create mode 100644 src/test/java/es_02_index/TestIndex_09_open.java diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f731245..4c8948d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,3 +1,6 @@ +server: + port: 40100 + spring: application: name: search-service diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..f3bbecb --- /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}/%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/es_01/TestDocument_01_get.java b/src/test/java/es_01_document/TestDocument_01_get.java similarity index 98% rename from src/test/java/es_01/TestDocument_01_get.java rename to src/test/java/es_01_document/TestDocument_01_get.java index a8e4608..54a938e 100644 --- a/src/test/java/es_01/TestDocument_01_get.java +++ b/src/test/java/es_01_document/TestDocument_01_get.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_01/TestDocument_02_get.java b/src/test/java/es_01_document/TestDocument_02_get.java similarity index 98% rename from src/test/java/es_01/TestDocument_02_get.java rename to src/test/java/es_01_document/TestDocument_02_get.java index 071b75b..f76d066 100644 --- a/src/test/java/es_01/TestDocument_02_get.java +++ b/src/test/java/es_01_document/TestDocument_02_get.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_01/TestDocument_03_add.java b/src/test/java/es_01_document/TestDocument_03_add.java similarity index 99% rename from src/test/java/es_01/TestDocument_03_add.java rename to src/test/java/es_01_document/TestDocument_03_add.java index 95050fd..4c2e4b2 100644 --- a/src/test/java/es_01/TestDocument_03_add.java +++ b/src/test/java/es_01_document/TestDocument_03_add.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_01/TestDocument_04_add.java b/src/test/java/es_01_document/TestDocument_04_add.java similarity index 99% rename from src/test/java/es_01/TestDocument_04_add.java rename to src/test/java/es_01_document/TestDocument_04_add.java index 1df9d68..30eeef5 100644 --- a/src/test/java/es_01/TestDocument_04_add.java +++ b/src/test/java/es_01_document/TestDocument_04_add.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_01/TestDocument_05_add.java b/src/test/java/es_01_document/TestDocument_05_add.java similarity index 99% rename from src/test/java/es_01/TestDocument_05_add.java rename to src/test/java/es_01_document/TestDocument_05_add.java index 58491a3..8b06200 100644 --- a/src/test/java/es_01/TestDocument_05_add.java +++ b/src/test/java/es_01_document/TestDocument_05_add.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_01/TestDocument_06_add.java b/src/test/java/es_01_document/TestDocument_06_add.java similarity index 98% rename from src/test/java/es_01/TestDocument_06_add.java rename to src/test/java/es_01_document/TestDocument_06_add.java index b8d9793..839f6ca 100644 --- a/src/test/java/es_01/TestDocument_06_add.java +++ b/src/test/java/es_01_document/TestDocument_06_add.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_01/TestDocument_07_add.java b/src/test/java/es_01_document/TestDocument_07_add.java similarity index 99% rename from src/test/java/es_01/TestDocument_07_add.java rename to src/test/java/es_01_document/TestDocument_07_add.java index 6a9c0db..5022f28 100644 --- a/src/test/java/es_01/TestDocument_07_add.java +++ b/src/test/java/es_01_document/TestDocument_07_add.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_01/TestDocument_08_update.java b/src/test/java/es_01_document/TestDocument_08_update.java similarity index 98% rename from src/test/java/es_01/TestDocument_08_update.java rename to src/test/java/es_01_document/TestDocument_08_update.java index 2f6c00e..8f0527a 100644 --- a/src/test/java/es_01/TestDocument_08_update.java +++ b/src/test/java/es_01_document/TestDocument_08_update.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_01/TestDocument_09_delete.java b/src/test/java/es_01_document/TestDocument_09_delete.java similarity index 98% rename from src/test/java/es_01/TestDocument_09_delete.java rename to src/test/java/es_01_document/TestDocument_09_delete.java index 55ed222..1204b7e 100644 --- a/src/test/java/es_01/TestDocument_09_delete.java +++ b/src/test/java/es_01_document/TestDocument_09_delete.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_01/TestDocument_10_bulk.java b/src/test/java/es_01_document/TestDocument_10_bulk.java similarity index 99% rename from src/test/java/es_01/TestDocument_10_bulk.java rename to src/test/java/es_01_document/TestDocument_10_bulk.java index 1c57e44..10d5940 100644 --- a/src/test/java/es_01/TestDocument_10_bulk.java +++ b/src/test/java/es_01_document/TestDocument_10_bulk.java @@ -1,4 +1,4 @@ -package es_01; +package es_01_document; import com.kwan.shuyu.SearchApplication; import lombok.extern.slf4j.Slf4j; diff --git a/src/test/java/es_02_index/TestIndex_01_Create.java b/src/test/java/es_02_index/TestIndex_01_Create.java new file mode 100644 index 0000000..8179d98 --- /dev/null +++ b/src/test/java/es_02_index/TestIndex_01_Create.java @@ -0,0 +1,100 @@ +package es_02_index; + +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.client.IndicesClient; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; +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; + +/** + * 创建索引 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/13 11:06 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class TestIndex_01_Create { + @Autowired + private RestHighLevelClient client; + + //创建索引 + @Test + public void testCreateIndex() throws IOException { +// PUT /my_index +// { +// "settings": { +// "number_of_shards": 1, +// "number_of_replicas": 1 +// }, +// "mappings": { +// "properties": { +// "field1":{ +// "type": "text" +// }, +// "field2":{ +// "type": "text" +// } +// } +// }, +// "aliases": { +// "default_index": {} +// } +// } + //创建索引对象 + CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book"); + //设置参数 + createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0")); + //指定映射1 + createIndexRequest.mapping(" {\n" + + " \t\"properties\": {\n" + + " \"name\":{\n" + + " \"type\":\"keyword\"\n" + + " },\n" + + " \"description\": {\n" + + " \"type\": \"text\"\n" + + " },\n" + + " \"price\":{\n" + + " \"type\":\"long\"\n" + + " },\n" + + " \"pic\":{\n" + + " \"type\":\"text\",\n" + + " \"index\":false\n" + + " }\n" + + " \t}\n" + + "}", XContentType.JSON); + //设置别名 + createIndexRequest.alias(new Alias("itheima_index_new")); + // 额外参数 + //设置超时时间 + createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2)); + //设置主节点超时时间 + createIndexRequest.setMasterTimeout(TimeValue.timeValueMinutes(1)); + //在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示 + createIndexRequest.waitForActiveShards(ActiveShardCount.from(2)); + createIndexRequest.waitForActiveShards(ActiveShardCount.DEFAULT); + //操作索引的客户端 + IndicesClient indices = client.indices(); + //执行创建索引库 + CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT); + //得到响应 + boolean acknowledged = createIndexResponse.isAcknowledged(); + //得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本 + boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged(); + System.out.println("acknowledged:" + acknowledged); + System.out.println("shardsAcknowledged:" + shardsAcknowledged); + } +} \ No newline at end of file diff --git a/src/test/java/es_02_index/TestIndex_02_Create.java b/src/test/java/es_02_index/TestIndex_02_Create.java new file mode 100644 index 0000000..8e70f9c --- /dev/null +++ b/src/test/java/es_02_index/TestIndex_02_Create.java @@ -0,0 +1,91 @@ +package es_02_index; + +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.client.IndicesClient; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; +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; + +/** + * 创建索引 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/13 11:06 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class TestIndex_02_Create { + @Autowired + private RestHighLevelClient client; + + //创建索引 + @Test + public void testCreateIndex() throws IOException { +// PUT /my_index +// { +// "settings": { +// "number_of_shards": 1, +// "number_of_replicas": 1 +// }, +// "mappings": { +// "properties": { +// "field1":{ +// "type": "text" +// }, +// "field2":{ +// "type": "text" +// } +// } +// }, +// "aliases": { +// "default_index": {} +// } +// } + //创建索引对象 + CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book"); + //设置参数 + createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0")); + //指定映射 + Map message = new HashMap<>(); + message.put("type", "text"); + Map properties = new HashMap<>(); + properties.put("message", message); + Map mapping = new HashMap<>(); + mapping.put("properties", properties); + createIndexRequest.mapping(mapping); + //设置别名 + createIndexRequest.alias(new Alias("itheima_index_new")); + // 额外参数 + //设置超时时间 + createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2)); + //设置主节点超时时间 + createIndexRequest.setMasterTimeout(TimeValue.timeValueMinutes(1)); + //在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示 + createIndexRequest.waitForActiveShards(ActiveShardCount.from(2)); + createIndexRequest.waitForActiveShards(ActiveShardCount.DEFAULT); + //操作索引的客户端 + IndicesClient indices = client.indices(); + //执行创建索引库 + CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT); + //得到响应(全部) + boolean acknowledged = createIndexResponse.isAcknowledged(); + //得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本 + boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged(); + System.out.println("acknowledged:" + acknowledged); + System.out.println("shardsAcknowledged:" + shardsAcknowledged); + } +} \ No newline at end of file diff --git a/src/test/java/es_02_index/TestIndex_03_Create.java b/src/test/java/es_02_index/TestIndex_03_Create.java new file mode 100644 index 0000000..9940966 --- /dev/null +++ b/src/test/java/es_02_index/TestIndex_03_Create.java @@ -0,0 +1,99 @@ +package es_02_index; + +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.client.IndicesClient; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.elasticsearch.common.xcontent.XContentFactory; +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; + +/** + * 创建索引 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/13 11:06 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class TestIndex_03_Create { + @Autowired + private RestHighLevelClient client; + + //创建索引 + @Test + public void testCreateIndex() throws IOException { +// PUT /my_index +// { +// "settings": { +// "number_of_shards": 1, +// "number_of_replicas": 1 +// }, +// "mappings": { +// "properties": { +// "field1":{ +// "type": "text" +// }, +// "field2":{ +// "type": "text" +// } +// } +// }, +// "aliases": { +// "default_index": {} +// } +// } + //创建索引对象 + CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book"); + //设置参数 + createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0")); + //指定映射 + XContentBuilder builder = XContentFactory.jsonBuilder(); + builder.startObject(); + { + builder.startObject("properties"); + { + builder.startObject("message"); + { + builder.field("type", "text"); + } + builder.endObject(); + } + builder.endObject(); + } + builder.endObject(); + createIndexRequest.mapping(builder); + //设置别名 + createIndexRequest.alias(new Alias("itheima_index_new")); + // 额外参数 + //设置超时时间 + createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2)); + //设置主节点超时时间 + createIndexRequest.setMasterTimeout(TimeValue.timeValueMinutes(1)); + //在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示 + createIndexRequest.waitForActiveShards(ActiveShardCount.from(2)); + createIndexRequest.waitForActiveShards(ActiveShardCount.DEFAULT); + //操作索引的客户端 + IndicesClient indices = client.indices(); + //执行创建索引库 + CreateIndexResponse createIndexResponse = indices.create(createIndexRequest, RequestOptions.DEFAULT); + //得到响应(全部) + boolean acknowledged = createIndexResponse.isAcknowledged(); + //得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本 + boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged(); + System.out.println("acknowledged:" + acknowledged); + System.out.println("shardsAcknowledged:" + shardsAcknowledged); + } +} \ No newline at end of file diff --git a/src/test/java/es_02_index/TestIndex_04_Async.java b/src/test/java/es_02_index/TestIndex_04_Async.java new file mode 100644 index 0000000..0f2acdd --- /dev/null +++ b/src/test/java/es_02_index/TestIndex_04_Async.java @@ -0,0 +1,94 @@ +package es_02_index; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.alias.Alias; +import org.elasticsearch.action.support.ActiveShardCount; +import org.elasticsearch.client.IndicesClient; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; +import org.elasticsearch.client.indices.CreateIndexResponse; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.unit.TimeValue; +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; + +/** + * 异步创建索引 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/13 11:10 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class TestIndex_04_Async { + @Autowired + private RestHighLevelClient client; + + //创建索引异步方式 + @Test + public void testCreateIndexAsync() { + //创建索引对象 + CreateIndexRequest createIndexRequest = new CreateIndexRequest("itheima_book"); + //设置参数 + createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0")); + //指定映射1 + createIndexRequest.mapping(" {\n" + + " \t\"properties\": {\n" + + " \"name\":{\n" + + " \"type\":\"keyword\"\n" + + " },\n" + + " \"description\": {\n" + + " \"type\": \"text\"\n" + + " },\n" + + " \"price\":{\n" + + " \"type\":\"long\"\n" + + " },\n" + + " \"pic\":{\n" + + " \"type\":\"text\",\n" + + " \"index\":false\n" + + " }\n" + + " \t}\n" + + "}", XContentType.JSON); + //设置别名 + createIndexRequest.alias(new Alias("itheima_index_new")); + // 额外参数 + //设置超时时间 + createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2)); + //设置主节点超时时间 + createIndexRequest.setMasterTimeout(TimeValue.timeValueMinutes(1)); + //在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示 + createIndexRequest.waitForActiveShards(ActiveShardCount.from(2)); + createIndexRequest.waitForActiveShards(ActiveShardCount.DEFAULT); + //操作索引的客户端 + IndicesClient indices = client.indices(); + //执行创建索引库 + ActionListener listener = new ActionListener() { + @Override + public void onResponse(CreateIndexResponse createIndexResponse) { + //得到响应(全部) + boolean acknowledged = createIndexResponse.isAcknowledged(); + //得到响应 指示是否在超时前为索引中的每个分片启动了所需数量的碎片副本 + boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged(); + System.out.println("acknowledged:" + acknowledged); + System.out.println("shardsAcknowledged:" + shardsAcknowledged); + } + + @Override + public void onFailure(Exception e) { + e.printStackTrace(); + } + }; + client.indices().createAsync(createIndexRequest, RequestOptions.DEFAULT, listener); + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/test/java/es_02_index/TestIndex_05_delete.java b/src/test/java/es_02_index/TestIndex_05_delete.java new file mode 100644 index 0000000..80a03db --- /dev/null +++ b/src/test/java/es_02_index/TestIndex_05_delete.java @@ -0,0 +1,42 @@ +package es_02_index; + + +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +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; + + +/** + * 删除索引 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/13 11:11 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class TestIndex_05_delete { + + @Autowired + private RestHighLevelClient client; + + @Test + public void testDeleteIndex() throws IOException { + //创建删除索引请求 + DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("itheima_book"); + // 执行 + AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT); + //得到相应 + boolean acknowledged = delete.isAcknowledged(); + System.out.println("acknowledged:" + acknowledged); + + } +} \ No newline at end of file diff --git a/src/test/java/es_02_index/TestIndex_06_delete.java b/src/test/java/es_02_index/TestIndex_06_delete.java new file mode 100644 index 0000000..7119deb --- /dev/null +++ b/src/test/java/es_02_index/TestIndex_06_delete.java @@ -0,0 +1,47 @@ +package es_02_index; + +import org.elasticsearch.action.ActionListener; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.client.RestHighLevelClient; +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; + +/** + * 删除索引异步操作 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/13 11:13 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class TestIndex_06_delete { + @Autowired + private RestHighLevelClient client; + + @Test + public void testDeleteIndexAsync() throws IOException { + //创建删除索引请求 + DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("itheima_book"); + // 执行 + ActionListener listener = new ActionListener() { + @Override + public void onResponse(AcknowledgedResponse acknowledgedResponse) { + //得到相应 + boolean acknowledged = acknowledgedResponse.isAcknowledged(); + System.out.println("acknowledged:" + acknowledged); + } + + @Override + public void onFailure(Exception e) { + e.printStackTrace(); + } + }; + } +} \ No newline at end of file diff --git a/src/test/java/es_02_index/TestIndex_07_exists.java b/src/test/java/es_02_index/TestIndex_07_exists.java new file mode 100644 index 0000000..d660343 --- /dev/null +++ b/src/test/java/es_02_index/TestIndex_07_exists.java @@ -0,0 +1,38 @@ +package es_02_index; + +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.GetIndexRequest; +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; + +/** + * 索引是否存在 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/13 11:14 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class TestIndex_07_exists { + + @Autowired + private RestHighLevelClient client; + + @Test + public void testExistIndex() throws IOException { + GetIndexRequest request = new GetIndexRequest("itheima_book"); + //参数 + request.local(false);//从主节点返回本地索引信息状态 + request.humanReadable(true);//以适合人类的格式返回 + request.includeDefaults(false);//是否返回每个索引的所有默认配置 + boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); + System.out.println("exists:" + exists); + } +} \ No newline at end of file diff --git a/src/test/java/es_02_index/TestIndex_08_close.java b/src/test/java/es_02_index/TestIndex_08_close.java new file mode 100644 index 0000000..d8f6381 --- /dev/null +++ b/src/test/java/es_02_index/TestIndex_08_close.java @@ -0,0 +1,37 @@ +package es_02_index; + +import org.elasticsearch.action.admin.indices.close.CloseIndexRequest; +import org.elasticsearch.action.support.master.AcknowledgedResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +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; + +/** + * 关闭索引 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/13 11:14 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class TestIndex_08_close { + + @Autowired + private RestHighLevelClient client; + + //关闭索引 + @Test + public void testCloseIndex() throws IOException { + CloseIndexRequest request = new CloseIndexRequest("itheima_book"); + AcknowledgedResponse close = client.indices().close(request, RequestOptions.DEFAULT); + boolean acknowledged = close.isAcknowledged(); + System.out.println("acknowledged:" + acknowledged); + } +} \ No newline at end of file diff --git a/src/test/java/es_02_index/TestIndex_09_open.java b/src/test/java/es_02_index/TestIndex_09_open.java new file mode 100644 index 0000000..f55dacc --- /dev/null +++ b/src/test/java/es_02_index/TestIndex_09_open.java @@ -0,0 +1,37 @@ +package es_02_index; + +import org.elasticsearch.action.admin.indices.open.OpenIndexRequest; +import org.elasticsearch.action.admin.indices.open.OpenIndexResponse; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +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; + +/** + * 开启索引 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/5/13 11:14 + */ +@SpringBootTest +@RunWith(SpringRunner.class) +public class TestIndex_09_open { + + @Autowired + private RestHighLevelClient client; + + //开启索引 + @Test + public void testOpenIndex() throws IOException { + OpenIndexRequest request = new OpenIndexRequest("itheima_book"); + OpenIndexResponse open = client.indices().open(request, RequestOptions.DEFAULT); + boolean acknowledged = open.isAcknowledged(); + System.out.println("acknowledged:" + acknowledged); + } +} \ No newline at end of file -- GitLab