002-es5.4.3结合spring-data-elasticsearch3.0.0.0使用
2018-03-03 10:41
489 查看
一、使用前设置
1、elasticsearch开启
cmd下,进入安装目录cd D:\developToool\elasticsearch-5.4.3 elasticsearch # 或者后台运行 elasticsearch -d
校验安装成功:http://127.0.0.1:9200/
查看集群健康:http://127.0.0.1:9200/_cat/health?v
查看集群节点:http://127.0.0.1:9200/_cat/nodes?v
查看索引信息:http://127.0.0.1:9200/_cat/indices?v
2、elasticsearch-header
cd D:\developToool\elasticsearch-head npm run start
校验:http://127.0.0.1:9100/
二、spring对接
由于服务器端版本使用5.4.3。故客户端使用此版本环境配置
ES版本:5.4.3
spring-data-elasticsearch:3.0.0.RELEASE
spring:5.0.1.RELEASE
2.1、maven配置
package com.trace.service.impl; import org.elasticsearch.client.Client; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.trace.domain.Article; import com.trace.service.ArticleService; /** * 文档的service测试类 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class ArticleServiceTest { /** 注入service */ @Autowired private ArticleService articleService; /** 注入客户端对象 基于原生API */ @Autowired private Client client; /** 注入es服务器模板 */ @Autowired private ElasticsearchTemplate elasticsearchTemplate; /** * 通过 ElasticsearchTemplate 创建索引和添加映射 */ @Test public void createIndex() { elasticsearchTemplate.createIndex(Article.class); elasticsearchTemplate.putMapping(Article.class); } /** * 添加方法测试 */ @Test public void testSave() { Article article = new Article(); article.setId(1001); article.setTitle("Spring Data Elasticsearch 1.3.1 昨天发布"); article.setContent( "DATAES-171 - 添加失效查询关键字支持 DATAES-194 - 测试可以清理 data 目录 DATAES-179 - 支持 Attachment 字段类型 DATAES-94 - " + "更新到最新版本的 elasticsearch 1.7.3 驱动器"); articleService.save(article); } /** * 根据索引删除的方法测试 */ @Test public void testDelete() { Article article = new Article(); article.setId(1001); articleService.delete(article); } /** * 根据索引查询的方法测试 */ @Test public void testFindOne() { System.out.println(articleService.findOne(1001)); } /** * 添加100条测试数据的方法 */ @Test public void testSaveBatch() { for (int i = 1; i <= 100; i++) { Article article = new Article(); article.setId(i); article.setTitle(i + "Spring Data Elasticsearch 1.3.1 昨天发布"); article.setContent( i + "DATAES-171 - 添加失效查询关键字支持 DATAES-194 - 测试可以清理 data 目录 DATAES-179 - 支持 Attachment 字段类型 DATAES-94 -" + " 更新到最新版本的 elasticsearch 1.7.3 驱动器"); articleService.save(article); } } /** * 排序分页查询的方法测试 */ @Test public void testSortAndPaging() { Iterable<Article> articles = articleService.findAll(); for (Article article : articles) { System.out.println(article); } Pageable pageable = new PageRequest(0, 10); Page<Article> pageData = articleService.findAll(pageable); for (Article article : pageData.getContent()) { System.out.println(article); } } /** * 条件查询的方法测试 */ @Test public void testConditionQuery() { // 查询 标题中含有 “昨天” // List<Article> articles = articleService.findByTitle("昨天"); // for (Article article : articles) { // System.out.println(article); // } // 查询 标题中含有 “昨天” 1-10条 Pageable pageable = new PageRequest(0, 10); Page<Article> pageData = articleService.findByTitle("昨天", pageable); System.out.println("总记录数:" + pageData.getTotalElements()); for (Article article : pageData.getContent()) { System.out.println(article); } } }
View Code
参考地址:https://www.cnblogs.com/dijia478/p/7839110.html
相关文章推荐
- 005-spring-data-elasticsearch 3.0.0.0使用【三】-spring-data之Spring数据扩展
- 006-spring-data-elasticsearch 3.0.0.0使用【四】-spring-data之Elasticsearch Repositories
- 003-spring-data-elasticsearch 3.0.0.0使用【一】-spring-data之概述、核心概念、查询方法、定义Repository接口
- 004-spring-data-elasticsearch 3.0.0.0使用【二】-spring-data之定义方法、创建repository实例、从聚合根发布事件
- ELK第七篇:spring-boot-starter-data-elasticsearch使用
- elasticsearch结合spring springmvc jest 使用做成web架构
- 一篇总结的很好的Spring data jpa 文章,里面包含多种查询方式,可以结合api使用
- SpringBoot项目通过 spring data elasticsearch使用elasticsearch
- spring-boot 结合spring-data-redis使用redis的消息队列
- elasticsearch结合spring springmvc jest 使用做成web架构
- Spring Data JPA(3)--JpaRepository和JpaSpecificationExecutor的结合使用
- spring-data-elasticsearch 初级使用
- spring-data-elasticsearch使用笔记
- elasticsearch结合spring springmvc jest 使用做成WEB架构
- spring-data-elasticsearch和shield插件结合
- 使用 Spring data redis 结合 Spring cache 缓存数据配置
- 使用 Spring、Elasticsearch 及 Logstash 构建企业级数据搜索和分析平台
- 001-spring结合quartz使用
- Spring data JPA 中IN的使用
- springdata jpa使用Example快速实现动态查询