当前位置:   article > 正文

【SpringCloud——Elasticsearch(上)】_es 和mysql

es 和mysql

一、什么是Elasticsearch

        elasticsearch是一款非常强大的开源搜索引擎,可以帮助我们从海量数据中快速找到需要的内容。

 二、倒排索引

1、正向索引

2、倒排索引

 3、总结

 三、ES和MySQL的区别

 

四、操作索引库

1、基于Kibana(WEB界面)

以下操作均根据以上数据库表进行:

 ①、创建索引库

  1. #创建索引库
  2. PUT /music
  3. {
  4. "mappings": {
  5. "properties": {
  6. "title":{
  7. "type": "text",
  8. "analyzer": "ik_smart"
  9. },
  10. "singer":{
  11. "type": "object",
  12. "properties": {
  13. "firstname":{
  14. "type":"keyword"
  15. },
  16. "lastname":{
  17. "type":"keyword"
  18. }
  19. }
  20. },
  21. "time":{
  22. "type": "date",
  23. "index": false
  24. },
  25. "url":{
  26. "type": "keyword",
  27. "index": false
  28. },
  29. "userid":{
  30. "type": "text",
  31. "index": false
  32. }
  33. }
  34. }
  35. }

②、查看索引库

  1. #获取索引库
  2. GET /music

③、修改索引库

索引库和mapping一旦创建则无法修改,但是可以添加新的字段。

  1. #为索引库添加字段(仅支持新增字段,不支持修改字段)
  2. PUT /music/_mapping
  3. {
  4. "properties":{
  5. "id":{
  6. "type":"integer",
  7. "index":false
  8. }
  9. }
  10. }

④、删除索引库

  1. #删除索引库
  2. DELETE /music

2、基于RestClient(代码)

1.Java项目导入es的RestHighLevelClient依赖:

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

2.因为SpringBoot默认的ES版本为7.6.2,所以我们需要覆盖默认的ES版本:

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <elasticsearch.version>7.12.1</elasticsearch.version>
  4. </properties>

3.初始化RestHighLevelClient(单元测试中执行)

  1. private RestHighLevelClient client;
  2. @BeforeEach
  3. void setClient(){
  4. this.client = new RestHighLevelClient(RestClient.builder(
  5. HttpHost.create("http://XXXX:9200")
  6. ));
  7. }
  8. @AfterEach
  9. void tearDown() throws Exception{
  10. this.client.close();
  11. }
  1. CREATE TABLE `tb_hotel` (
  2.   `id` bigint(20NOT NULL COMMENT '酒店id',
  3.   `name` varchar(255NOT NULL COMMENT '酒店名称;例:7天酒店',
  4.   `address` varchar(255NOT NULL COMMENT '酒店地址;例:航头路',
  5.   `price` int(10NOT NULL COMMENT '酒店价格;例:329',
  6.   `score` int(2NOT NULL COMMENT '酒店评分;例:45,就是4.5分',
  7.   `brand` varchar(32NOT NULL COMMENT '酒店品牌;例:如家',
  8.   `city` varchar(32NOT NULL COMMENT '所在城市;例:上海',
  9.   `star_name` varchar(16DEFAULT NULL COMMENT '酒店星级,从低到高分别是:1星到5星,1钻到5钻',
  10.   `business` varchar(255DEFAULT NULL COMMENT '商圈;例:虹桥',
  11.   `latitude` varchar(32NOT NULL COMMENT '纬度;例:31.2497',
  12.   `longitude` varchar(32NOT NULL COMMENT '经度;例:120.3925',
  13.   `pic` varchar(255DEFAULT NULL COMMENT '酒店图片;例:/img/1.jpg',
  14.   PRIMARY KEY (`id`)
  15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

 以下操作均根据以上数据库表进行:

①、编写mapping映射

  1. PUT /hotel
  2. {
  3. "mappings": {
  4. "properties": {
  5. "id":{
  6. "type": "keyword"
  7. },
  8. "name":{
  9. "type": "text",
  10. "analyzer": "ik_max_word",
  11. "copy_to": "all"
  12. },
  13. "address":{
  14. "type": "keyword",
  15. "index": false
  16. },
  17. "price":{
  18. "type": "integer"
  19. },
  20. "score":{
  21. "type": "integer"
  22. },
  23. "brand":{
  24. "type": "keyword",
  25. "copy_to": "all"
  26. },
  27. "city":{
  28. "type": "keyword"
  29. },
  30. "starName":{
  31. "type": "keyword"
  32. },
  33. "bussiness":{
  34. "type": "keyword",
  35. "copy_to": "all"
  36. },
  37. "location":{
  38. "type": "geo_point"
  39. },
  40. "pic":{
  41. "type": "keyword",
  42. "index": false
  43. },
  44. "all":{
  45. "type": "text",
  46. "analyzer": "ik_max_word"
  47. }
  48. }
  49. }
  50. }

 copy_to属性的作用:支持多条件搜索查询

 ②、创建索引库

  1. //索引库创建
  2. @Test
  3. void createHotelIndex() throws IOException {
  4. //1.创建Request对象
  5. CreateIndexRequest request = new CreateIndexRequest("hotel");
  6. //2.准备请求的参数,DSL语句
  7. //MAPPING_TEMPLATE 就是我们编写的mapping映射中的DSL
  8. request.source(MAPPING_TEMPLATE, XContentType.JSON);
  9. //3.发送请求
  10. client.indices().create(request, RequestOptions.DEFAULT);
  11. }

②、删除索引库

  1. //删除索引库
  2. @Test
  3. void deleteHotelIndex() throws IOException {
  4. DeleteIndexRequest request = new DeleteIndexRequest("hotel");
  5. client.indices().delete(request,RequestOptions.DEFAULT);
  6. }

③、查看索引库是否存在

  1. //判断索引库是否存在
  2. @Test
  3. void testExistHotelIndex() throws IOException {
  4. GetIndexRequest request = new GetIndexRequest("hotel");
  5. boolean ret = client.indices().exists(request,RequestOptions.DEFAULT);
  6. System.err.println(ret);
  7. }

五、操作文档

1、基于Kibana(WEB界面)

※参考四当中基于Kibana生成的索引库进行以下文档操作:

①、插入文档

  1. #插入文档(新增数据)
  2. POST /music/_doc/1
  3. {
  4. "id":1,
  5. "title":"爱在西元前",
  6. "singer":{
  7. "firstname":"周",
  8. "lastname":"杰伦"
  9. },
  10. "time":"2022-01-01T12:00:00.000Z",
  11. "url":"/music/get?path=茜茜 - 爱在西元前",
  12. "userid":"1966998940"
  13. }

②、获取文档

  1. #查询文档
  2. GET /music/_doc/1

③、修改文档

方式一:全量修改,会删除旧文档,添加新文档(如果旧文档不存在则创建

  1. PUT /music/_doc/1
  2. {
  3. "id":1,
  4. "title":"爱在西元前",
  5. "singer":{
  6. "firstname":"周杰伦",
  7. "lastname":"Jay"
  8. },
  9. "time":"2022-01-01T12:00:00.000Z",
  10. "url":"/music/get?path=茜茜 - 爱在西元前",
  11. "userid":"1966998940"
  12. }

方式二:局部修改,只修改指定字段

  1. POST /music/_update/1
  2. {
  3. "doc": {
  4. "title":"爱不在西元前"
  5. }
  6. }

④、删除文档

  1. #删除文档
  2. DELETE /music/_doc/1

2、基于RestClient(代码)

※参考四当中基于RestClient生成的索引库及其数据库数据进行以下文档操作:

①、新增数据库数据到索引库

  1. //添加数据到索引库
  2. @Test
  3. void addDataToHotelIndex() throws IOException {
  4. //根据id查询酒店数据
  5. Hotel hotel = hotelService.getById(61083L);
  6. //转换为Doc文档类型
  7. HotelDoc doc = new HotelDoc(hotel);
  8. //1.准备request对象
  9. IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
  10. //2.准备Json文档
  11. request.source(JSON.toJSONString(doc), XContentType.JSON);
  12. //3.发送请求
  13. client.index(request, RequestOptions.DEFAULT);
  14. }

 HotelDoc实体类:

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

②、根据ID查询索引库数据

  1. //根据id查询酒店数据
  2. @Test
  3. void queryDataFromHotelIndex() throws IOException {
  4. //1.创建request对象
  5. GetRequest request = new GetRequest("hotel","61083");
  6. //2.发送请求,获取结果
  7. GetResponse response = client.get(request,RequestOptions.DEFAULT);
  8. //3.解析响应结果
  9. String json = response.getSourceAsString();
  10. HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);
  11. System.out.println(hotelDoc);
  12. }

③、删除酒店数据

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

④、修改酒店数据

  1. //修改文档数据
  2. @Test
  3. void updateDocument() throws IOException {
  4. //1.准备Request
  5. UpdateRequest request = new UpdateRequest("hotel","61083");
  6. //2.准备请求参数
  7. request.doc(
  8. "brand","皇冠假日酒店",
  9. "starName","四星"
  10. );
  11. //3.发送请求
  12. client.update(request,RequestOptions.DEFAULT);
  13. }

⑤、批量导入数据库数据到索引库

  1. //批量新增文档数据
  2. @Test
  3. void testBulkRequest() throws IOException {
  4. //1.创建Request
  5. BulkRequest request = new BulkRequest();
  6. //2.准备Json文档
  7. //批量查询酒店数据
  8. List<Hotel> list = hotelService.list();
  9. for (int i = 0; i < list.size(); i++) {
  10. HotelDoc hotelDoc = new HotelDoc(list.get(i));
  11. request.add(new IndexRequest("hotel").
  12. id(hotelDoc.getId().toString()).
  13. source(JSON.toJSONString(hotelDoc),XContentType.JSON));
  14. }
  15. //3.发送请求
  16. client.bulk(request,RequestOptions.DEFAULT);
  17. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/642210
推荐阅读
相关标签
  

闽ICP备14008679号