在应用中经常会有检索的功能——查询数据库中包含关键字的数据,如果采用查数据库(like 关键字)的方式,效率会非常低。为了解决这个问题,我们引入了 ElasticSearch 框架。
ElasticSearch 下载安装,请参考博客:https://blog.csdn.net/yjclsx/article/details/81302041
接下来,我们使用 Spring Data Elasticsearch Repositories 集成 ElasticSearch
步骤1:引入映射
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
步骤2:配置文件进行设置
spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 spring.data.elasticsearch.repositories.enabled=true
步骤3::定义一个实体类
package com.example.demo.domain; import java.io.Serializable; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "blog", type = "article") public class Article implements Serializable{ private static final long serialVersionUID = 1L; private long id; private String title; private String summary; private String content; private int pv; //省略getter、setter方法 }
这里定义了 Article 实例,表示文章。类比关系型数据库的话,Index 相当于表,Document 相当于记录
在 ElasticSearch 6.X 版本中,不建议使用 type,而且在 7.X 版本中将会彻底废弃 type,但我这里用的是 ElasticSearch 5.6.8,所以仍然写了 type。这里,一个 Article 代表一篇文章,同时代表一条索引记录。
步骤4:定义一个接口,并继承 ElasticSearchRepository
package com.example.demo.repository; import com.example.demo.domain.Article; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; @Component //@Repository public interface ArticleRepository extends ElasticsearchRepository<Article, Long> { }
这里的 Repository 相当于 DAO,操作 mysql 还是 ElasticSearch 都是一样的
步骤5:定义 service 接口和实现类
package com.example.demo.service; import com.example.demo.domain.Article; import org.elasticsearch.index.query.QueryBuilder; public interface ArticleService { Article save(Article Article); Iterable<Article> search(QueryBuilder queryBuilder); }
实现类
package com.example.demo.service.impl; import com.example.demo.domain.Article; import com.example.demo.repository.ArticleRepository; import com.example.demo.service.ArticleService; import org.elasticsearch.index.query.QueryBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleRepository articleRepository; @Override public Article save(Article article) { return articleRepository.save(article); } @Override public Iterable<Article> search(QueryBuilder queryBuilder) { return articleRepository.search(queryBuilder); } }
步骤6:写一个测试方法
package com.example.demo.controller; import com.example.demo.domain.Article; import com.example.demo.domain.JsonData; import com.example.demo.repository.ArticleRepository; import com.example.demo.service.ArticleService; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1/article") public class ArticleController { @Autowired private ArticleService articleService; @GetMapping("save") public Object save(long id,String title){ Article article = new Article(); article.setId(id); article.setPv(123); article.setContent("搜索elasticsearch框架整合"); article.setTitle(title); article.setSummary("搜索框架整合"); articleService.save(article); return JsonData.buildSuccess(); } @GetMapping("search") public Object search(String title){ //QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); //搜索全部文档 QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title); Iterable<Article> list = articleService.search(queryBuilder); return JsonData.buildSuccess(list); } }
使用 Postman 进行测试
本文参考:https://www.cnblogs.com/cjsblog/p/9756978.html
原文地址:https://www.cnblogs.com/jwen1994/p/11374154.html
时间: 2024-10-08 19:32:16