当前位置:   article > 正文

Spring Data Elasticsearch--ElasticsearchRestTemplate--使用/教程/实例_elasticsearchresttemplate.delete

elasticsearchresttemplate.delete

原文网址:Spring Data Elasticsearch--ElasticsearchRestTemplate--使用/教程/实例_IT利刃出鞘的博客-CSDN博客

技术星球

欢迎来到IT技术星球,网站是:learn.skyofit.com(或者百度直接搜:自学精灵)。内容有:Java真实面试题Java设计模式实战、Shiro项目实战、Idea和Navicat的“魔法”教程、SpringBoot进阶、架构与微服务设计、高并发实战、Java入门实战、网站防御技术、JavaWeb入门项目等。网站的定位:超高的质量、超高的真实性、超高的实用性。欢迎加入~

简介

说明

本文用实例来介绍如何使用Spring Data Elasticsearch的ElasticsearchRestTemplate来操作ES。包括:索引的增删等、文档的增删改查、文档的动态查询(或者说:多条件查询、复杂查询)。

动态查询的含义:查询条件有多个时,某个查询条件可能有也可能没有,这时就需要手动判断,如果为空则不拼装搜索条件。

本文用博客的业务进行示例。索引名:blog,里边有标题、内容、作者、创建时间等。

官网

Elasticsearch Operations

为什么用ElasticsearchRestTemplate

在Spring Data Elasticsearch4.0之后,ElasticsearchRepository里边大部分搜索方法都已经被标记为废弃,注释中让我们用两种方法来操作:

  1. 在方法上使用@Query自定义DSL。
  2. 使用 ElasticsearchOperations来操作。

使用@Query自定义DSL不支持动态查询,其用法见:Spring Data Elasticsearch--使用/教程/实例_IT利刃出鞘的博客-CSDN博客

ElasticsearchOperations支持动态查询,ElasticsearchRestTemplate是ElasticsearchOperations的实现类。

公共代码

依赖及配置

依赖

主要是spring-boot-starter-data-elasticsearch这个依赖:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  4. </dependency>

配置

  1. spring:
  2. elasticsearch:
  3. rest:
  4. # 如果是集群,用逗号隔开
  5. uris: http://127.0.0.1:9200
  6. # username: xxx
  7. # password: yyy
  8. # connection-timeout: 1
  9. # read-timeout: 30
  10. # 上边是客户端High Level REST Client的配置,推荐使用。
  11. # 下边是reactive客户端的配置,非官方,不推荐。
  12. # data:
  13. # elasticsearch:
  14. # client:
  15. # reactive:
  16. # endpoints: 127.0.0.1:9200
  17. # username: xxx
  18. # password: yyy
  19. # connection-timeout: 1
  20. # socket-timeout: 30
  21. # use-ssl: false

整个pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.12.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo_spring-data-elasticsearch</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo_spring-data-elasticsearch</name>
  15. <description>demo_spring-data-elasticsearch</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. <optional>true</optional>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38. <dependency>
  39. <groupId>com.github.xiaoymin</groupId>
  40. <artifactId>knife4j-spring-boot-starter</artifactId>
  41. <version>3.0.3</version>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <plugins>
  46. <plugin>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-maven-plugin</artifactId>
  49. <configuration>
  50. <excludes>
  51. <exclude>
  52. <groupId>org.projectlombok</groupId>
  53. <artifactId>lombok</artifactId>
  54. </exclude>
  55. </excludes>
  56. </configuration>
  57. </plugin>
  58. </plugins>
  59. </build>
  60. </project>

索引结构及数据

索引结构

http://localhost:9200/
PUT blog

  1. {
  2. "mappings": {
  3. "properties": {
  4. "id":{
  5. "type":"long"
  6. },
  7. "title": {
  8. "type": "text"
  9. },
  10. "content": {
  11. "type": "text"
  12. },
  13. "author":{
  14. "type": "text",
  15. "fields": {
  16. "keyword": {
  17. "type": "keyword"
  18. }
  19. }
  20. },
  21. "category":{
  22. "type": "keyword"
  23. },
  24. "createTime": {
  25. "type": "date",
  26. "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
  27. },
  28. "updateTime": {
  29. "type": "date",
  30. "format":"yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd'T'HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss||epoch_millis"
  31. },
  32. "status":{
  33. "type":"integer"
  34. },
  35. "serialNum": {
  36. "type": "keyword"
  37. }
  38. }
  39. }
  40. }

数据

  • 每个文档必须独占一行,不能换行。
  • 此命令要放到postman中去执行,如果用head执行会失败

http://localhost:9200/
POST _bulk

  1. {"index":{"_index":"blog","_id":1}}
  2. {"blogId":1,"title":"Spring Data ElasticSearch学习教程1","content":"这是批量添加的文档1","author":"Iron Man","category":"ElasticSearch","status":1,"serialNum":"1","createTime":"2021-10-10 11:52:01.249","updateTime":null}
  3. {"index":{"_index":"blog","_id":2}}
  4. {"blogId":2,"title":"Spring Data ElasticSearch学习教程2","content":"这是批量添加的文档2","author":"Iron Man","category":"ElasticSearch","status":1,"serialNum":"2","createTime":"2021-10-10 11:52:02.249","updateTime":null}
  5. {"index":{"_index":"blog","_id":3}}
  6. {"blogId":3,"title":"Spring Data ElasticSearch学习教程3","content":"这是批量添加的文档3","author":"Captain America","category":"ElasticSearch","status":1,"serialNum":"3","createTime":"2021-10-10 11:52:03.249","updateTime":null}
  7. {"index":{"_index":"blog","_id":4}}
  8. {"blogId":4,"title":"Spring Data ElasticSearch学习教程4","content":"这是批量添加的文档4","author":"Captain America","category":"ElasticSearch","status":1,"serialNum":"4","createTime":"2021-10-10 11:52:04.249","updateTime":null}
  9. {"index":{"_index":"blog","_id":5}}
  10. {"blogId":5,"title":"Spring Data ElasticSearch学习教程5","content":"这是批量添加的文档5","author":"Spider Man","category":"ElasticSearch","status":1,"serialNum":"5","createTime":"2021-10-10 11:52:05.249","updateTime":null}
  11. {"index":{"_index":"blog","_id":6}}
  12. {"blogId":6,"title":"Java学习教程6","content":"这是批量添加的文档6","author":"Spider Man","category":"ElasticSearch","status":1,"serialNum":"6","createTime":"2021-10-10 11:52:06.249","updateTime":null}
  13. {"index":{"_index":"blog","_id":7}}
  14. {"blogId":7,"title":"Java学习教程7","content":"这是批量添加的文档7","author":"Iron Man","category":"ElasticSearch","status":1,"serialNum":"7","createTime":"2021-10-10 11:52:07.249","updateTime":null}
  15. {"index":{"_index":"blog","_id":8}}
  16. {"blogId":8,"title":"Java学习教程8","content":"这是批量添加的文档8","author":"Iron Man","category":"ElasticSearch","status":1,"serialNum":"8","createTime":"2021-10-10 11:52:08.249","updateTime":null}
  17. {"index":{"_index":"blog","_id":9}}
  18. {"blogId":9,"title":"Java学习教程9","content":"这是批量添加的文档9","author":"Captain America","category":"ElasticSearch","status":1,"serialNum":"9","createTime":"2021-10-10 11:52:09.249","updateTime":null}
  19. {"index":{"_index":"blog","_id":10}}
  20. {"blogId":10,"title":"Java学习教程10","content":"这是批量添加的文档10","author":"God Of Thunder","category":"ElasticSearch","status":1,"serialNum":"10","createTime":"2021-10-10 11:52:10.249","updateTime":null}

执行后的结果

实体类

  1. package com.example.demo.entity;
  2. import com.fasterxml.jackson.annotation.JsonFormat;
  3. import lombok.Data;
  4. import org.springframework.data.annotation.Id;
  5. import org.springframework.data.elasticsearch.annotations.DateFormat;
  6. import org.springframework.data.elasticsearch.annotations.Document;
  7. import org.springframework.data.elasticsearch.annotations.Field;
  8. import org.springframework.data.elasticsearch.annotations.FieldType;
  9. import java.util.Date;
  10. @Data
  11. @Document(indexName = "blog", shards = 1, replicas = 1)
  12. public class Blog {
  13. //此项作为id,不会写到_source里边。
  14. @Id
  15. private Long blogId;
  16. @Field(type = FieldType.Text)
  17. private String title;
  18. @Field(type = FieldType.Text)
  19. private String content;
  20. @Field(type = FieldType.Text)
  21. private String author;
  22. //博客所属分类。
  23. @Field(type = FieldType.Keyword)
  24. private String category;
  25. //0: 未发布(草稿) 1:已发布 2:已删除
  26. @Field(type = FieldType.Integer)
  27. private int status;
  28. //序列号,用于给外部展示的id
  29. @Field(type = FieldType.Keyword)
  30. private String serialNum;
  31. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
  32. @Field(type= FieldType.Date, format= DateFormat.custom, pattern="yyyy-MM-dd HH:mm:ss.SSS")
  33. private Date createTime;
  34. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
  35. @Field(type=FieldType.Date, format=DateFormat.custom, pattern="yyyy-MM-dd HH:mm:ss.SSS")
  36. private Date updateTime;
  37. }

索引操作(IndexOperations)

本处只是展示索引的操作方法,项目中不会这样创建索引,而是手写DSL去创建。

  1. package com.example.demo.controller;
  2. import com.example.demo.entity.Blog;
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiOperation;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RestController;
  10. /**
  11. * 本处只是展示索引的操作方法,项目中不会这样创建索引,而是手写DSL去创建。
  12. */
  13. @Api(tags = "索引操作(用不到)")
  14. @RestController
  15. @RequestMapping("index")
  16. public class IndexController {
  17. @Autowired
  18. private ElasticsearchRestTemplate elasticsearchRestTemplate;
  19. @ApiOperation("创建索引")
  20. @PostMapping("createIndex")
  21. public String createIndex() {
  22. // 创建索引,会根据Blog类的@Document注解信息来创建
  23. elasticsearchRestTemplate.createIndex(Blog.class);
  24. // 配置映射,会根据Item类中的id、Field等字段来自动完成映射
  25. elasticsearchRestTemplate.putMapping(Blog.class);
  26. return "success";
  27. }
  28. @ApiOperation("创建索引")
  29. @PostMapping("deleteIndex")
  30. public String deleteIndex() {
  31. // 删除索引,会根据Blog类的@Document注解信息来删除
  32. elasticsearchRestTemplate.deleteIndex(Blog.class);
  33. return "success";
  34. }
  35. }

文档操作(DocumentOperations)

        本处介绍常用的一些方法,这些方法能够满足开发中所有需求:

  1. 添加单个文档
  2. 添加多个文档
  3. 修改单个文档数据
  4. 修改单个文档部分数据
  5. 修改多个文档部分数据
  6. 查看单个文档
  7. 删除单个文档(根据id)
  8. 删除单个文档(根据条件)
  9. 删除所有文档。

上边是文章的部分内容,为便于维护,全文已转移至此网址:Spring Data Elasticsearch-ElasticsearchRestTemplate-使用实例 - 自学精灵

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

闽ICP备14008679号