Spring Boot 揭秘与实战 数据存储篇 - ElasticSearch
Posted long77
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 揭秘与实战 数据存储篇 - ElasticSearch相关的知识,希望对你有一定的参考价值。
文章目录
本文讲解Spring Boot基础下,如何使用 ElasticSearch,实现全文搜索。
版本须知
spring data elasticSearch 的版本与Spring boot、Elasticsearch版本需要匹配。
Spring Boot Version (x) | Spring Data Elasticsearch Version (y) | Elasticsearch Version (z) |
---|---|---|
x <= 1.3.5 | y <= 1.3.4 | z <= 1.7.2 |
x >= 1.4.x | 2.0.0 <=y <5.0.0 | 2.0.0 <= z < 5.0.0 |
环境依赖
修改 POM 文件,添加 spring-boot-starter-data-elasticsearch 依赖。
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
- </dependency>
数据源
方案一 使用 Spring Boot 默认配置
在 src/main/resources/application.properties 中配置数据源信息。
- spring.data.elasticsearch.properties.host = 127.0.0.1
- spring.data.elasticsearch.properties.port = 9300
通过 Java Config 创建ElasticSearchConfig。
- @Configuration
- @EnableElasticsearchRepositories("com.lianggzone.springboot.action.data.elasticsearch")
- public class ElasticSearchConfig {}
方案二 手动创建
通过 Java Config 创建ElasticSearchConfig。
- @Configuration
- @EnableElasticsearchRepositories("com.lianggzone.springboot.action.data.elasticsearch")
- public class ElasticsearchConfig2 {
- private String hostname = "127.0.0.1";
- private int port = 9300;
- @Bean
- public ElasticsearchOperations elasticsearchTemplate() {
- return new ElasticsearchTemplate(client());
- }
- @Bean
- public Client client() {
- TransportClient client = new TransportClient();
- TransportAddress address = new InetSocketTransportAddress(hostname, port);
- client.addTransportAddress(address);
- return client;
- }
- }
业务操作
实体对象
- @Document(indexName = "springbootdb", type = "news")
- public class News {
- @Id
- private String id;
- private String title;
- private String content;
- @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMdd‘T‘HHmmss.SSS‘Z‘")
- @Field(type = FieldType.Date, format = DateFormat.basic_date_time, index = FieldIndex.not_analyzed)
- @CreatedDate
- private Date createdDateTime;
- // GET和SET方法
- }
DAO相关
- public interface NewsRepository extends ElasticsearchRepository<News, String> {
- public List<News> findByTitle(String title);
- }
Service相关
我们来定义实现类,Service层调用Dao层的方法,这个是典型的套路。
- @Service
- public class NewsService {
- @Autowired
- private NewsRepository newsRepository;
- public Iterable<News> findAll(){
- return newsRepository.findAll();
- }
- public Iterable<News> search(QueryBuilder query){
- return newsRepository.search(query);
- }
- public List <News> findByTitle(String title) {
- return this.newsRepository.findByTitle(title);
- }
- public void deleteAll(String id){
- this.newsRepository.delete(id);
- }
- public void init(){
- for (int i = 0; i < 100; i++) {
- News news = new News();
- news.setId(i+"");
- news.setTitle(i + ".梁桂钊单元测试用例");
- news.setContent("梁桂钊单元测试用例"+i+"xxxxx");
- news.setCreatedDateTime(new Date());
- this.newsRepository.save(news);
- }
- }
- }
Controller相关
为了展现效果,我们先定义一组简单的 RESTful API 接口进行测试。
- @RestController
- @RequestMapping(value="/data/elasticsearch/news")
- public class NewsController {
- @Autowired
- private NewsService newsService;
- /**
- * 初始化
- * @param request
- */
- @RequestMapping(value = "/init", method = RequestMethod.POST)
- public void init(HttpServletRequest request) {
- this.newsService.init();
- }
- /**
- * findAll
- * @param request
- * @return
- */
- @RequestMapping(value = "/", method = RequestMethod.GET)
- public Map<String, Object> findList(HttpServletRequest request) {
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("items", this.newsService.findAll());
- return params;
- }
- /**
- * find
- * @param request
- * @return
- */
- @RequestMapping(value = "/{title}", method = RequestMethod.GET)
- public Map<String, Object> search(@PathVariable String title) {
- // 构建查询条件
- QueryBuilder queryBuilder = QueryBuilders.queryString(title);
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("items", this.newsService.search(queryBuilder));
- return params;
- }
- }
总结
上面这个简单的案例,让我们看到了 Spring Boot 整合 ElasticSearch 流程如此简单。
源代码
相关示例完整代码: springboot-action
(完)
以上是关于Spring Boot 揭秘与实战 数据存储篇 - ElasticSearch的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 揭秘与实战 数据存储篇 - Redis
Spring Boot 揭秘与实战 数据存储篇 - ElasticSearch
Spring Boot 揭秘与实战 数据存储篇 - 声明式事务管理
Spring Boot 揭秘与实战 数据缓存篇 - 快速入门