SpringBoot - 整合ElasticSearch检索

Elasticsearch 是一个分布式、可扩展、实时的搜索与数据分析引擎。 它能从项目一开始就赋予你的数据以搜索、分析和探索的能力,可用于实现全文搜索和实时数据统计。

SpringBoot默认支持两种技术来和ES交互;
1、Jest(默认不生效)
需要导入jest的工具包才生效(io.searchbox.client.JestClient)
2、SpringData ElasticSearch【默认,但是ES版本有可能不合适】

直接上干货:

一 、创建项目 选择Spring Web和ElasticeSearch组件

在这里插入图片描述

我们首先使用jest来与ES交互:
1、http://localhost:9200/ 查看ES版本号

在这里插入图片描述

2、然后在maven库找到对应的包 https://mvnrepository.com/artifact/io.searchbox/jest

在这里插入图片描述

3、将默认springdata ElasticSearch注掉,添加jest路径

在这里插入图片描述

4、配置数据源

在这里插入图片描述

启动服务没有问题,已连接好
在这里插入图片描述

测试一下:

1、创建一个实体

在这里插入图片描述

2、编写测试添加索引和内容

在这里插入图片描述

@Autowired
JestClient jestClient;

@Test
public void contextLoads() {
    //1、给ES中索引一个文档
    Article article = new Article();
    article.setId(1);
    article.setTitle("好消息");
    article.setAuthor("aTeng");
    article.setContent("Hello World");

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

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

编写搜索查询:json表达式官方指南能找到
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_with_query_dsl.html
在这里插入图片描述
在这里插入图片描述

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

    //查找内容里含hello的数据
    String json = "{\n" +
            "    \"query\" : {\n" +
            "        \"match\" : {\n" +
            "            \"content\" : \"hello\"\n" +
            "        }\n" +
            "    }\n" +
            "}";

    //构建搜索功能 index:索引 type:类型
    Search build = new Search.Builder(json).addIndex("xianyu").addType("news").build();

    //执行
    try {
        SearchResult result = jestClient.execute(build);
        System.out.println(result.getJsonString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

搜索成功!
在这里插入图片描述


使用默认SpringData操作ElasticSearch
1、使用默认POM:

在这里插入图片描述

2、编辑数据源

在这里插入图片描述

name为:
在这里插入图片描述
nodes端口改为9300,因为不适用9200

3、重启服务,如果报错的话,可能是版本不合适

解决方法: 1)、升级SpringBoot版本
2)、安装对应版本的ES版本,适配说明:https://github.com/spring-projects/spring-data-elasticsearch
在这里插入图片描述

1、创建实体添加注解

@Document(indexName = "xianyu1",type = "book")

在这里插入图片描述

2、编写一个 ElasticsearchRepository接口 继承 ElasticsearchRepository
在这里插入图片描述

3、直接测试调用

@Test
public void test02(){
    Book book = new Book();
    book.setId(1);
    book.setBookName("颠沛流离");
    book.setAuthor("浩叔");
    bookRespository.index(book);
}

访问 http://localhost:9200/xianyu1/book/_search 查询xianyu1索引下 book类型下内容含1的数据
在这里插入图片描述
http://localhost:9200/xianyu1/book/_search 可以查询xianyu1索引下 book类型下所有数据
在这里插入图片描述

我们也可以在BookRespository内自定义查询方式:
在这里插入图片描述
官方有示例:https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
在这里插入图片描述

测试
@Test
    public void test02(){
//        Book book = new Book();
//        book.setId(1);
//        book.setBookName("颠沛流离");
//        book.setAuthor("浩叔");
//        bookRespository.index(book);

        List<Book> books = bookRespository.findByBookNameLike("离");
        System.out.println(books);
    }

查询成功

写在最后…其实用习惯了,2个都差不多,不过我感觉默认的更方便一点 - - (主要是代码量少哈哈哈

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot整合Elasticsearch可以使用Spring Data Elasticsearch,在pom.xml文件添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 在application.properties配置文件添加以下配置: ``` spring.data.elasticsearch.repositories.enabled=true spring.data.elasticsearch.cluster-name=my-application spring.data.elasticsearch.cluster-nodes=localhost:9300 ``` 其,cluster-name为Elasticsearch集群名称,cluster-nodesElasticsearch集群节点地址。 接下来,定义一个实体类,并使用注解标注该实体类对应的索引和类型: ```java @Document(indexName = "article", type = "news") public class Article { @Id private Long id; @Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word") private String title; @Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word") private String content; // getter/setter } ``` 在定义完实体类后,可以使用ElasticsearchRepository接口定义数据访问层的方法: ```java public interface ArticleRepository extends ElasticsearchRepository<Article, Long> { List<Article> findByTitle(String title); } ``` 在服务层调用ArticleRepository的方法即可进行数据的增删改查操作: ```java @Service public class ArticleService { @Autowired private ArticleRepository articleRepository; public void save(Article article) { articleRepository.save(article); } public void delete(Long id) { articleRepository.deleteById(id); } public Article getById(Long id) { Optional<Article> optional = articleRepository.findById(id); return optional.isPresent() ? optional.get() : null; } public List<Article> getByTitle(String title) { return articleRepository.findByTitle(title); } } ``` 以上就是Spring Boot整合Elasticsearch的基本流程。需要注意的是,在使用Spring Data Elasticsearch时,需要根据实际情况配置相应的注解和属性,以保证数据的正确存储和检索

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值