前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring学习笔记(二十六)——springboot集成elasticsearch

Spring学习笔记(二十六)——springboot集成elasticsearch

作者头像
不愿意做鱼的小鲸鱼
发布2022-09-26 18:09:46
1.1K0
发布2022-09-26 18:09:46
举报
文章被收录于专栏:web全栈

elasticsearch使用前提

ES 基本数据与MySQL的对应关系: * 索引:相当于MySQL中很多个数据库 * 类型:相当于MySQL中的很多个表 * 文档:相当于MySQL中表中的很多条数据 * 属性:相当于MySQL中每条数据中有很多属性

elasticsearch是什么,elasticsearch怎么用 1. 参考官方文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html 2. 参考之前学习的笔记:Elastic Stack——Elastic Stack简介和Elasticsearch核心详解 3. 参考之前的学习笔记:Elasticsearch 中文分词、全文搜索、分布式集群搭建和java客户端操作

Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客

docker安装elasticsearch

  1. 查找中央镜像中的elasticsearch docker search elasticsearch
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
  1. 拉取镜像 docker pull elasticsearch
  2. 查看镜像 docker images
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
  1. 挂载镜像 elasticsearch是Java写的检索服务,运行会占取2个G的堆内存空间,使用虚拟机可能会内存空间不够,因此可以使用 -e 命令限制限制堆内存的使用空间。 docker run -e ES_JAVA_OPTS="-Xms256m -Xms265m" -d -p 9200:9200 -p 9300:9300 --name ES01 elasticsearch
  2. elasticsearch启动成功
    • docker ps 查看进程情况
    • 访问虚拟机9200端口,可以看到elasticsearch服务
    Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
    Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客

SpringBoot集成elasticsearch

SpringBoot默认使用SpringData ElasticSearch模块进行操作 SpringBoot默认支持两种技术来和ES交互; 1、Jest(默认不生效) 需要导入jest的工具包(io.searchbox.client.JestClient) 2、SpringData ElasticSearch 【ES版本有可能不合适】 版本适配说明:https://github.com/spring-projects/spring-data-elasticsearch

Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客

如果版本不适配: 1)、升级SpringBoot版本 2)、安装对应版本的ES:使用docker安装低版本(elsaticsearch2.4.6)的镜像: 下载镜像:docker pull elasticsearch:2.4.6 运行镜像:docker run -e ES_JAVA_OPTS="-Xms256m -Xms265m" -d -p 9201:9200 -p 9301:9300 --name ES02 5e9d896dc62c

1. 导入坐标依赖pom.xml

代码语言:javascript
复制
<dependencies>
        <!--SpringBoot默认使用SpringData ElasticSearch模块进行操作-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
        <dependency>
            <groupId>io.searchbox</groupId>
            <artifactId>jest</artifactId>
            <version>5.3.3</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>4.1.0</version>
        </dependency>
    </dependencies>

2. application.properties的配置

代码语言:javascript
复制
spring.elasticsearch.jest.uris=http://192.168.147.131:9201

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.168.147.131:9301

注: 9301 是docker映射的 Java 客户端的端口。9201是docker映射的、支持 Restful HTTP 的接口。 更多的配置:

代码语言:javascript
复制
spring.data.elasticsearch.cluster-name Elasticsearch 集群名。(默认值: elasticsearch)
spring.data.elasticsearch.cluster-nodes 集群节点地址列表,用逗号分隔。如果没有指定,就启动一个客户端节点。
spring.data.elasticsearch.propertie 用来配置客户端的额外属性。
spring.data.elasticsearch.repositories.enabled 开启 Elasticsearch 仓库。(默认值:true。)

3. 使用Jest工具测试elasticsearch

  • 建一个文章的实体类 Article.java
代码语言:javascript
复制
public class Article {

    @JestId
    private Integer id;
    private String author;
    private String title;
    private String content;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
  • 编写单元测试
代码语言:javascript
复制
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot03ElasticApplicationTests {

    @Autowired
    JestClient jestClient;
    @Test
    public void contextLoads() {
        //1、给Es中索引(保存)一个文档;
        Article article = new Article();
        article.setId(1);
        article.setTitle("好消息");
        article.setAuthor("zhangsan");
        article.setContent("Hello World");

        //构建一个索引功能
        Index index = new Index.Builder(article).index("hello").type("news").build();

        try {
            //执行
            jestClient.execute(index);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //测试搜索
    @Test
    public void search(){

        //查询表达式(json是elasticsearch的查询表达式)
        String json ="{\n" +
                "    \"query\" : {\n" +
                "        \"match\" : {\n" +
                "            \"content\" : \"hello\"\n" +
                "        }\n" +
                "    }\n" +
                "}";

        //更多操作:https://github.com/searchbox-io/Jest/tree/master/jest
        //构建搜索功能
        Search search = new Search.Builder(json).addIndex("hello").addType("news").build();

        //执行
        try {
            SearchResult result = jestClient.execute(search);
            System.out.println(result.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 运行测试 新增数据后,访问 http://192.168.147.131:9201/hello/news/1,可以查出新增数据。
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客

测试查询操作:

Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客

4. 使用SpringData ElasticSearch Template 工具进行集成测试

  • 建立实体类 Book.java
代码语言:javascript
复制
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "hello",type = "book")
public class Book {
    private Integer id;
    private String bookName;
    private String author;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", bookName='" + bookName + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}
  • 编写一个实体类Repository继承ElasticsearchRepository
代码语言:javascript
复制
import java.util.List;
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
    //参照
    // https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
   public List<Book> findByBookNameLike(String bookName);
}

相当于dao层,具体接口实现和规则可以参考官方文档:https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/ * 编写测试方法

代码语言:javascript
复制
    @Autowired
    BookRepository bookRepository;
    @Test
    public void test02(){
        Book book = new Book();
        book.setId(1);
        book.setBookName("西游记");
        book.setAuthor("吴承恩");
        bookRepository.index(book);
    }
    @Test
    public void  test03(){
        for (Book book : bookRepository.findByBookNameLike("游")) {
            System.out.println(book);
        }
    }
  • 测试结果
  1. 通过http查询结果:
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
  1. 后台打印模糊查询
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客
Spring学习笔记(二十六)——springboot集成elasticsearch-左眼会陪右眼哭の博客

详细的SpringData ElasticSearch Template使用可以参考文档:https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • elasticsearch使用前提
  • docker安装elasticsearch
  • SpringBoot集成elasticsearch
    • 1. 导入坐标依赖pom.xml
      • 2. application.properties的配置
        • 3. 使用Jest工具测试elasticsearch
          • 4. 使用SpringData ElasticSearch Template 工具进行集成测试
          相关产品与服务
          容器服务
          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档