• 记录帖 ES的RestApi使用


    索引库操作

    1. import org.apache.http.HttpHost;
    2. import org.elasticsearch.client.RequestOptions;
    3. import org.elasticsearch.client.RestClient;
    4. import org.elasticsearch.client.RestHighLevelClient;
    5. import org.elasticsearch.client.indices.CreateIndexRequest;
    6. import org.elasticsearch.client.indices.GetIndexRequest;
    7. import org.elasticsearch.common.xcontent.XContentType;
    8. import org.junit.jupiter.api.AfterEach;
    9. import org.junit.jupiter.api.BeforeEach;
    10. import org.junit.jupiter.api.Test;
    11. import java.io.IOException;
    12. public class ElassticTest {
    13. private RestHighLevelClient client;
    14. //创建与虚拟机的es的连接
    15. @BeforeEach
    16. void setUp() {
    17. this.client = new RestHighLevelClient(RestClient.builder(
    18. HttpHost.create("http://192.168.48.129:9200")
    19. ));
    20. }
    21. @Test
    22. void testConnect() {
    23. System.out.println(client);
    24. }
    25. @AfterEach
    26. void tearDown() throws IOException {
    27. this.client.close();
    28. }
    29. //创建索引库
    30. @Test
    31. void testCreateIndex() throws IOException {
    32. // 1.创建Request对象
    33. CreateIndexRequest request = new CreateIndexRequest("items");
    34. // 2.准备请求参数
    35. request.source(MAPPING_TEMPLATE, XContentType.JSON);
    36. // 3.发送请求
    37. client.indices().create(request, RequestOptions.DEFAULT);
    38. }
    39. //查看索引库是否存在
    40. @Test
    41. void testExistIndex() throws IOException {
    42. //创建request
    43. GetIndexRequest request = new GetIndexRequest("items");
    44. //发送请求
    45. boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    46. System.out.println(exists);
    47. }
    48. static final String MAPPING_TEMPLATE = "{\n" +
    49. " \"mappings\": {\n" +
    50. " \"properties\": {\n" +
    51. " \"id\": {\n" +
    52. " \"type\": \"keyword\"\n" +
    53. " },\n" +
    54. " \"name\":{\n" +
    55. " \"type\": \"text\",\n" +
    56. " \"analyzer\": \"ik_max_word\"\n" +
    57. " },\n" +
    58. " \"price\":{\n" +
    59. " \"type\": \"integer\"\n" +
    60. " },\n" +
    61. " \"stock\":{\n" +
    62. " \"type\": \"integer\"\n" +
    63. " },\n" +
    64. " \"image\":{\n" +
    65. " \"type\": \"keyword\",\n" +
    66. " \"index\": false\n" +
    67. " },\n" +
    68. " \"category\":{\n" +
    69. " \"type\": \"keyword\"\n" +
    70. " },\n" +
    71. " \"brand\":{\n" +
    72. " \"type\": \"keyword\"\n" +
    73. " },\n" +
    74. " \"sold\":{\n" +
    75. " \"type\": \"integer\"\n" +
    76. " },\n" +
    77. " \"commentCount\":{\n" +
    78. " \"type\": \"integer\"\n" +
    79. " },\n" +
    80. " \"isAD\":{\n" +
    81. " \"type\": \"boolean\"\n" +
    82. " },\n" +
    83. " \"updateTime\":{\n" +
    84. " \"type\": \"date\"\n" +
    85. " }\n" +
    86. " }\n" +
    87. " }\n" +
    88. "}";
    89. }

    文件操作

    1. import cn.hutool.core.bean.BeanUtil;
    2. import cn.hutool.json.JSONUtil;
    3. import com.hmall.item.domain.po.Item;
    4. import com.hmall.item.domain.po.ItemDoc;
    5. import com.hmall.item.service.IItemService;
    6. import org.apache.http.HttpHost;
    7. import org.elasticsearch.action.delete.DeleteRequest;
    8. import org.elasticsearch.action.get.GetRequest;
    9. import org.elasticsearch.action.get.GetResponse;
    10. import org.elasticsearch.action.index.IndexRequest;
    11. import org.elasticsearch.action.update.UpdateRequest;
    12. import org.elasticsearch.client.RequestOptions;
    13. import org.elasticsearch.client.RestClient;
    14. import org.elasticsearch.client.RestHighLevelClient;
    15. import org.elasticsearch.common.xcontent.XContentType;
    16. import org.junit.jupiter.api.AfterEach;
    17. import org.junit.jupiter.api.BeforeEach;
    18. import org.junit.jupiter.api.Test;
    19. import org.springframework.beans.factory.annotation.Autowired;
    20. import org.springframework.boot.test.context.SpringBootTest;
    21. import java.io.IOException;
    22. @SpringBootTest(properties = "spring.profiles.active=local")
    23. public class ElassticDocumentTest {
    24. private RestHighLevelClient client;
    25. @Autowired
    26. private IItemService itemService;
    27. @BeforeEach
    28. void setUp() {
    29. this.client = new RestHighLevelClient(RestClient.builder(
    30. HttpHost.create("http://192.168.48.129:9200")
    31. ));
    32. }
    33. @AfterEach
    34. void tearDown() throws IOException {
    35. this.client.close();
    36. }
    37. @Test
    38. void testAddDocument() throws IOException {
    39. // 1.根据id查询商品数据
    40. Item item = itemService.getById(100002644680L);
    41. // 2.转换为文档类型
    42. ItemDoc itemDoc = BeanUtil.copyProperties(item, ItemDoc.class);
    43. // 3.将ItemDTO转json
    44. String doc = JSONUtil.toJsonStr(itemDoc);
    45. // 1.准备Request对象
    46. IndexRequest request = new IndexRequest("items").id(itemDoc.getId());
    47. // 2.准备Json文档
    48. request.source(doc, XContentType.JSON);
    49. // 3.发送请求
    50. client.index(request, RequestOptions.DEFAULT);
    51. }
    52. @Test
    53. void testGetDocumentById() throws IOException {
    54. // 1.准备Request对象
    55. GetRequest request = new GetRequest("items").id("100002644680");
    56. // 2.发送请求
    57. GetResponse response = client.get(request, RequestOptions.DEFAULT);
    58. // 3.获取响应结果中的source
    59. String json = response.getSourceAsString();
    60. ItemDoc itemDoc = JSONUtil.toBean(json, ItemDoc.class);
    61. System.out.println("itemDoc= " + itemDoc);
    62. }
    63. @Test
    64. void testDeleteDocument() throws IOException {
    65. // 1.准备Request,两个参数,第一个是索引库名,第二个是文档id
    66. DeleteRequest request = new DeleteRequest("item", "100002644680");
    67. // 2.发送请求
    68. client.delete(request, RequestOptions.DEFAULT);
    69. }
    70. @Test
    71. void testUpdateDocument() throws IOException {
    72. // 1.准备Request
    73. UpdateRequest request = new UpdateRequest("items", "100002644680");
    74. // 2.准备请求参数
    75. request.doc(
    76. "price", 58800,
    77. "commentCount", 1
    78. );
    79. // 3.发送请求
    80. client.update(request, RequestOptions.DEFAULT);
    81. }
    82. //批处理
    83. @Test
    84. void testLoadItemDocs() throws IOException {
    85. // 分页查询商品数据
    86. int pageNo = 1;
    87. int size = 1000;
    88. while (true) {
    89. Page<Item> page = itemService.lambdaQuery().eq(Item::getStatus, 1).page(new Page<Item>(pageNo, size));
    90. // 非空校验
    91. List<Item> items = page.getRecords();
    92. if (CollUtils.isEmpty(items)) {
    93. return;
    94. }
    95. log.info("加载第{}页数据,共{}条", pageNo, items.size());
    96. // 1.创建Request
    97. BulkRequest request = new BulkRequest("items");
    98. // 2.准备参数,添加多个新增的Request
    99. for (Item item : items) {
    100. // 2.1.转换为文档类型ItemDTO
    101. ItemDoc itemDoc = BeanUtil.copyProperties(item, ItemDoc.class);
    102. // 2.2.创建新增文档的Request对象
    103. request.add(new IndexRequest()
    104. .id(itemDoc.getId())
    105. .source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON));
    106. }
    107. // 3.发送请求
    108. client.bulk(request, RequestOptions.DEFAULT);
    109. // 翻页
    110. pageNo++;
    111. }
    112. }
    113. }

  • 相关阅读:
    Redis系列11:内存淘汰策略
    线程池ThreadPoolExecutor、ThreadLocal
    Linux中的yum和vim
    IOS面试题object-c 1-10
    Java基于springboot+vue的图书馆网上图书借阅系统 nodejs前后端分离
    明明肚子就是不饿,就是总想着吃点东西,这是为什么?原因有3个
    Rhino Linux:滚动发布但也很稳定的 Ubuntu
    js 闭包:概念-用途-弊端
    MSC 307(88) (2010 FTPC Code) Part 5低播焰测试
    图像处理学习笔记-01-绪论
  • 原文地址:https://blog.csdn.net/qq_65252348/article/details/139278055