SpringBoot 2.x (12):整合Elasticsearch
Elasticsearch:一个优秀的搜索引擎框架
搜索方面最基本的是SQL的like语句
进一步的有Lucene框架
后来有企业级的Solr框架
而Elasticsearch框架尤其适合于数据量特别大的
Elasticsearch底层也是由Lucene实现的
应用:Github、维基百科、StackOverflow
Elasticsearch部署:
纯Java开发,因此必备JDK
采用5.6版本而不是最新版,因为SpringBoot可能不支持
推荐部署到Linux服务器,但是这里为了方便我直接部署在本地Windows系统
下载地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.8.zip
下载好进入BIN目录,运行elasticsearch.bat运行
如果报错,通常情况是机器配置不够或者以错误地root权限启动了
Win10高配机器不存在这种问题
http://127.0.0.1:9200/:进入主页
http://localhost:9200/_cat/health?v:查看集群状态
localhost:9200/_cat/indices?v:查看索引列表
参考官网,创建索引:
查看我创建的索引:
加入数据:
查看我加入的数据:
SpringBoot进行整合
依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
查看依赖后发现,SpringBoot2.1.4采用的elasticsearch是6.4.3版本
为了确保不出问题,我重新下载6.4.3版本,事后我验证了,就以下的这些基本操作,es的版本可以不影响
新建实体类Article:
搜索的对象就是文章
package org.dreamtech.esdemo.domain; import java.io.Serializable; import org.springframework.data.elasticsearch.annotations.Document; /** * 文章对象 * * @author Xu Yiqing * */ @Document(indexName = "blog", type = "article") public class Article implements Serializable { private static final long serialVersionUID = 8210249797764830332L; private long id; private String title; private String summary; private String content; private int pv; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public int getPv() { return pv; } public void setPv(int pv) { this.pv = pv; } }
对ES进行配置:
spring.data.elasticsearch.cluster-name=elasticsearch spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 spring.data.elasticsearch.repositories.enabled=true
对ES进行操作默认的接口:
package org.dreamtech.esdemo.repository; import org.dreamtech.esdemo.domain.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Component; @Component public interface ArticleRepository extends ElasticsearchRepository<Article, Long> { }
Controller层测试代码:
package org.dreamtech.esdemo.controller; import org.dreamtech.esdemo.domain.Article; import org.dreamtech.esdemo.repository.ArticleRepository; 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.RestController; @RestController public class ArticleController { @Autowired private ArticleRepository articleRepository; @GetMapping("/save") public Object save(long id, String title) { Article article = new Article(); article.setId(id); article.setPv(123); article.setContent("Springboot整合Elasticsearch"); article.setTitle(title); article.setSummary("搜索框架整合"); articleRepository.save(article); return "save"; } @GetMapping("/search") public Object search(String title) { QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", title); Iterable<Article> list = articleRepository.search(queryBuilder); return list; } }
实际测试:
用这种方式保存多个文章后就可以进行搜索了: