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
(完)
如果觉得我的文章对你有帮助,请随意打赏。

- 版权声明:本文由 梁桂钊 发表于 梁桂钊的博客
- 转载声明:自由转载-非商用-非衍生-保持署名(创意共享3.0许可证),非商业转载请注明作者及出处,商业转载请联系作者本人。
- 文章标题:Spring Boot 揭秘与实战(二) 数据存储篇 - ElasticSearch
- 文章链接:http://blog.720ui.com/2016/springboot_02_data_elasticsearch/
Spring Boot 揭秘与实战(二) 数据存储篇 - ElasticSearch的更多相关文章
- Spring Boot 揭秘与实战(二) 数据存储篇 - 声明式事务管理
文章目录 1. 声明式事务 2. Spring Boot默认集成事务 3. 实战演练4. 源代码 3.1. 实体对象 3.2. DAO 相关 3.3. Service 相关 3.4. 测试,测试 本文 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - MongoDB
文章目录 1. 环境依赖 2. 数据源 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 3. 使用mongoTemplate操作4. 总结 3.1. 实体对象 3 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - Redis
文章目录 1. 环境依赖 2. 数据源 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 3. 使用 redisTemplate 操作4. 总结 3.1. 工具类 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - JPA整合
文章目录 1. 环境依赖 2. 数据源 3. 脚本初始化 4. JPA 整合方案一 通过继承 JpaRepository 接口 4.1. 实体对象 4.2. DAO相关 4.3. Service相关 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - MyBatis整合
文章目录 1. 环境依赖 2. 数据源3. 脚本初始化 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 4. MyBatis整合5. 总结 4.1. 方案一 通过 ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - 数据访问与多数据源配置
文章目录 1. 环境依赖 2. 数据源 3. 单元测试 4. 源代码 在某些场景下,我们可能会在一个应用中需要依赖和访问多个数据源,例如针对于 MySQL 的分库场景.因此,我们需要配置多个数据源. ...
- Spring Boot 揭秘与实战(二) 数据存储篇 - MySQL
文章目录 1. 环境依赖 2. 数据源3. 脚本初始化 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 4. 使用JdbcTemplate操作5. 总结 4.1. ...
- Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门
文章目录 1. 声明式缓存 2. Spring Boot默认集成CacheManager 3. 默认的 ConcurrenMapCacheManager 4. 实战演练5. 扩展阅读 4.1. Mav ...
- Spring Boot 揭秘与实战(二) 数据缓存篇 - Redis Cache
文章目录 1. Redis Cache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 Redis Cache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存 ...
随机推荐
- 转-【exp/imp】将US7ASCII字符集的dmp文件导入到ZHS16GBK字符集的数据库中
原帖地址:http://blog.csdn.net/lihuarongaini/article/details/71512116 1.2 前言部分 1.2.1 导读和注意事项 各位技术爱好者,看完 ...
- 搭建RESTful API 之 实现WSGI服务的URL映射
javarestfull 搭建参考 http://blog.csdn.net/hejias/article/details/47424511 问题引出:对于一个稍具规模的网站来说,实现的功能不可能通过 ...
- 3G 4G 5G中的网络安全问题——文献汇总
Modeling and Analysis of RRC-Based Signalling Storms in 3G Networks 还是使用状态机模型来做恶意UE识别 https://san.ee ...
- 转:Java工程师成神之路~(2018修订版)
转: http://www.hollischuang.com/archives/489 阿里大牛珍藏架构资料,点击链接免费获取 针对本文,博主最近在写<成神之路系列文章> ,分章分节介绍所 ...
- MySQL批量修改字符集
统一将字符字符集变成utf8_general_ci,已测试. DROP PROCEDURE IF EXISTS `chanageCharSet`; CREATE PROCEDURE `chanageC ...
- wpf自定义控件中使用自定义事件
wpf自定义控件中使用自定义事件 1 创建自定义控件及自定义事件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...
- linux150条命令
●线上查询及帮助命令(2 个)man help ●文件和目录操作命令(13 个) ls tree pwd mkdir rmdir cd touch cp mv rm ln find rename ●查 ...
- HTML5 ②
html代码中的各种标签和特殊符号: 1.<p>段落标签 默认会在段落前后都有空行</p>: 2.<hr/> 水平线标签 <br/>换行标签 ...
- Oracle exists 和not exists 用法详解
有两个简单例子,以说明 “exists”和“in”的效率问题 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; ...
- Saiku部分函数解析(八)
Saiku函数解析 1. now() : 获取当前日期 直接使用即可 2. IIF(logic_exp, string, string): IIF判断,logic_exp是逻辑表达式,结果为t ...