diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index f7312457a7e6fcad3b9a40634904e84039aa1779..4c8948d4a341debddd0845a3fc12affe816bd553 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 0000000000000000000000000000000000000000..f3bbecbefc49cee675306491a55209b08759f2ca --- /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 a8e4608a031e950e5e268f333f729392affcb2ca..54a938e6118f577d7f2f6239e29e9d737a6ddcbd 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 071b75b58345d78af548d5b301e8fa42636d3315..f76d0667968dd348bb0772c3f8fec4be2b7b8828 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 95050fdcdc7c3539e96851482b7e36d29cf95a77..4c2e4b24ac7cea4b4b40907b119293143bf51bfd 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 1df9d68b20a2d5c351d262037b91ae70d6813dd3..30eeef5a88ee1575a70cfdfd04b8498ab1b64779 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 58491a368f3ed49d8764b7cb0cf1f6612e8bf420..8b06200a99f780d9d0cb17294c2f070eb53b16de 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 b8d9793f64ae58d4b83a14a6571d62bf7bf2ea5e..839f6caed04faa5b681c3819edc5aaa3c6427af2 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 6a9c0dbe95e6cb3fc825a5110d769cde694af2e2..5022f28d25256e7de3bdc84cf54b76557381c8c3 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 2f6c00e9dfd0d9e31a4c58076ea788c750dc15c9..8f0527a8805f5246ffd2b7d6209c0042ed72ba2a 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 55ed222cce8dcfaef95a13d6dedfa9b450f98a60..1204b7e78ab36a42be535402917cda9a46b6b8bf 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 1c57e4427a10ec520e363a59fde780db6d69d158..10d59405e19f8772bb99aede48c7120282394f1e 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 0000000000000000000000000000000000000000..8179d98ed166521756cd7db1b13a0b27113f4b21 --- /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 0000000000000000000000000000000000000000..8e70f9c2348e24beae72438baf9c67aed1c8e079 --- /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 0000000000000000000000000000000000000000..994096605e7a6fd818c875e0ae6b26b018b08df7 --- /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 0000000000000000000000000000000000000000..0f2acddeacd810b59215232131713b7b0b892af1 --- /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 0000000000000000000000000000000000000000..80a03db15ea2dfb4ffe20a78f9016fb9bf3d3dd6 --- /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 0000000000000000000000000000000000000000..7119debaf6d56e25a34a3964fdfaf136593cebc9 --- /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 0000000000000000000000000000000000000000..d660343ac58847786f335200dcd8a6023a83e546 --- /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 0000000000000000000000000000000000000000..d8f63819959154fa30c677353cb2774022a357ff --- /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 0000000000000000000000000000000000000000..f55dacc20a521f97d238618d9c756caae896a0d3 --- /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