• springboot初试elasticsearch


    引入依赖

     elasticsearch的依赖版本与你elasticsearch要一致

    1. <dependency>
    2. <groupId>org.elasticsearch.client</groupId>
    3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
    4. </dependency>

    索引库的操作 

    创建索引库

    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.common.xcontent.XContentType;
    7. import org.junit.jupiter.api.AfterEach;
    8. import org.junit.jupiter.api.BeforeEach;
    9. import org.junit.jupiter.api.Test;
    10. import java.io.IOException;
    11. public class HotelIndexTest {
    12. // 创建索引语句
    13. public static final String MAPPING_TEMPLATE = "{\n" +
    14. " \"mappings\": {\n" +
    15. " \"properties\": {\n" +
    16. " \"id\": {\n" +
    17. " \"type\": \"keyword\"\n" +
    18. " },\n" +
    19. " \"name\":{\n" +
    20. " \"type\": \"text\",\n" +
    21. " \"analyzer\": \"ik_max_word\",\n" +
    22. " \"copy_to\": \"all\"\n" +
    23. " },\n" +
    24. " \"address\":{\n" +
    25. " \"type\": \"keyword\",\n" +
    26. " \"index\": false\n" +
    27. " },\n" +
    28. " \"price\":{\n" +
    29. " \"type\": \"integer\"\n" +
    30. " },\n" +
    31. " \"score\":{\n" +
    32. " \"type\": \"integer\"\n" +
    33. " },\n" +
    34. " \"brand\":{\n" +
    35. " \"type\": \"keyword\",\n" +
    36. " \"copy_to\": \"all\"\n" +
    37. " },\n" +
    38. " \"city\":{\n" +
    39. " \"type\": \"keyword\",\n" +
    40. " \"copy_to\": \"all\"\n" +
    41. " },\n" +
    42. " \"starName\":{\n" +
    43. " \"type\": \"keyword\"\n" +
    44. " },\n" +
    45. " \"business\":{\n" +
    46. " \"type\": \"keyword\"\n" +
    47. " },\n" +
    48. " \"location\":{\n" +
    49. " \"type\": \"geo_point\"\n" +
    50. " },\n" +
    51. " \"pic\":{\n" +
    52. " \"type\": \"keyword\",\n" +
    53. " \"index\": false\n" +
    54. " },\n" +
    55. " \"all\":{\n" +
    56. " \"type\": \"text\",\n" +
    57. " \"analyzer\": \"ik_max_word\"\n" +
    58. " }\n" +
    59. " }\n" +
    60. " }\n" +
    61. "}";
    62. private RestHighLevelClient client;
    63. // 创建索引库
    64. @Test
    65. void createHotelIndex() throws IOException {
    66. // 1.创建Request对象
    67. CreateIndexRequest request = new CreateIndexRequest("hotel");
    68. // 2.准备请求的参数:DSL语句
    69. request.source(MAPPING_TEMPLATE, XContentType.JSON);
    70. // 3.发送请求
    71. client.indices().create(request, RequestOptions.DEFAULT);
    72. }
    73. // 初始化连接
    74. @BeforeEach
    75. void setUp() {
    76. this.client = new RestHighLevelClient(RestClient.builder(
    77. HttpHost.create("http://43.139.59.28:9200")
    78. ));
    79. }
    80. // 关闭连接
    81. @AfterEach
    82. void tearDown() throws IOException {
    83. this.client.close();
    84. }
    85. }

    查看索引库

    删除索引库

    1. @Test
    2. void testDeleteHotelIndex() throws IOException {
    3. // 1.创建Request对象
    4. DeleteIndexRequest request = new DeleteIndexRequest("hotel");
    5. // 2.发送请求
    6. client.indices().delete(request, RequestOptions.DEFAULT);
    7. }

     查看索引库

    文档的操作 

    数据库实体类

    数据库查询后的结果是一个Hotel类型的对象  

    1. import com.baomidou.mybatisplus.annotation.IdType;
    2. import com.baomidou.mybatisplus.annotation.TableId;
    3. import com.baomidou.mybatisplus.annotation.TableName;
    4. import lombok.Data;
    5. @Data
    6. @TableName("tb_hotel")
    7. public class Hotel {
    8. @TableId(type = IdType.INPUT)
    9. private Long id;
    10. private String name;
    11. private String address;
    12. private Integer price;
    13. private Integer score;
    14. private String brand;
    15. private String city;
    16. private String starName;
    17. private String business;
    18. private String longitude;
    19. private String latitude;
    20. private String pic;
    21. }

     文档实体类

    索引库查询后的结果是一个HotelDoc类型的对象,与Hotel不同在于横纵坐标的处理.

    1. import lombok.Data;
    2. import lombok.NoArgsConstructor;
    3. @Data
    4. @NoArgsConstructor
    5. public class HotelDoc {
    6. private Long id;
    7. private String name;
    8. private String address;
    9. private Integer price;
    10. private Integer score;
    11. private String brand;
    12. private String city;
    13. private String starName;
    14. private String business;
    15. private String location;
    16. private String pic;
    17. public HotelDoc(Hotel hotel) {
    18. this.id = hotel.getId();
    19. this.name = hotel.getName();
    20. this.address = hotel.getAddress();
    21. this.price = hotel.getPrice();
    22. this.score = hotel.getScore();
    23. this.brand = hotel.getBrand();
    24. this.city = hotel.getCity();
    25. this.starName = hotel.getStarName();
    26. this.business = hotel.getBusiness();
    27. // 合并横纵坐标
    28. this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
    29. this.pic = hotel.getPic();
    30. }
    31. }

    新增文档

    1. @Resource
    2. private HotelService hotelService;
    3. @Test
    4. void testAddDocument() throws IOException {
    5. // 1.根据id查询酒店数据
    6. Hotel hotel = hotelService.getById(61083L);
    7. // 2.转换为文档类型
    8. HotelDoc hotelDoc = new HotelDoc(hotel);
    9. // 3.将HotelDoc转json
    10. String json = JSON.toJSONString(hotelDoc);
    11. // 1.准备Request对象
    12. IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
    13. // 2.准备Json文档
    14. request.source(json, XContentType.JSON);
    15. // 3.发送请求
    16. client.index(request, RequestOptions.DEFAULT);
    17. }

    查询文档

    1. @Test
    2. void testGetDocumentById() throws IOException {
    3. // 1.准备Request
    4. GetRequest request = new GetRequest("hotel", "61083");
    5. // 2.发送请求,得到响应
    6. GetResponse response = client.get(request, RequestOptions.DEFAULT);
    7. // 3.解析响应结果
    8. String json = response.getSourceAsString();
    9. HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
    10. System.out.println(hotelDoc);
    11. }

     结果

    修改文档

    修改我们讲过两种方式:

    • 全量修改:本质是先根据id删除,再新增

    • 增量修改:修改文档中的指定字段值  

    RestClient的API中,全量修改与新增的API完全一致,这里不再赘述,我们主要关注增量修改  

    1. @Test
    2. void testUpdateDocument() throws IOException {
    3. // 1.准备Request
    4. UpdateRequest request = new UpdateRequest("hotel", "61083");
    5. // 2.准备请求参数
    6. request.doc(
    7. "price", "952",
    8. "starName", "四钻"
    9. );
    10. // 3.发送请求
    11. client.update(request, RequestOptions.DEFAULT);
    12. }

    查看文档

    删除文档

    1. @Test
    2. void testDeleteDocument() throws IOException {
    3. // 1.准备Request
    4. DeleteRequest request = new DeleteRequest("hotel", "61083");
    5. // 2.发送请求
    6. client.delete(request, RequestOptions.DEFAULT);
    7. }

     查询61083文档

    批量导入文档

    利用BulkRequest批量将数据库数据导入到索引库中

    1. @Test
    2. void testBulkRequest() throws IOException {
    3. // 批量查询酒店数据
    4. List hotels = hotelService.list();
    5. // 1.创建Request
    6. BulkRequest request = new BulkRequest();
    7. // 2.准备参数,添加多个新增的Request
    8. for (Hotel hotel : hotels) {
    9. // 2.1.转换为文档类型HotelDoc
    10. HotelDoc hotelDoc = new HotelDoc(hotel);
    11. // 2.2.创建新增文档的Request对象
    12. request.add(new IndexRequest("hotel")
    13. .id(hotelDoc.getId().toString())
    14. .source(JSON.toJSONString(hotelDoc), XContentType.JSON));
    15. }
    16. // 3.发送请求
    17. client.bulk(request, RequestOptions.DEFAULT);
    18. }

    查看所有文档

    1. @Test
    2. void testMatchAll() throws IOException {
    3. // 1.准备Request
    4. SearchRequest request = new SearchRequest("hotel");
    5. // 2.准备DSL
    6. request.source()
    7. .query(QueryBuilders.matchAllQuery());
    8. // 3.发送请求
    9. SearchResponse response = client.search(request, RequestOptions.DEFAULT);
    10. // 4.解析响应
    11. System.out.println(response);
    12. }

    结果

  • 相关阅读:
    Python学习基础笔记七十五——Python调用其他程序
    使用 Echarts 插件完成中国地图
    GGTalk 开源即时通讯系统源码剖析之:服务端全局缓存
    Linux基础概要
    一个高性能类型安全的.NET枚举实用开源库
    MediaCodec源码分析 configure流程
    【Iterator模式】C++设计模式——迭代器
    eclipse导入maven项目
    Android Bitmap 缩放
    【Mysql】有关数据表中存在重复数据问题解决方案(已实现mysql类型的sql)
  • 原文地址:https://blog.csdn.net/qq_63431773/article/details/132654189