SpringBoot轻松整合ElasticSearch
完整项目代码地址(https://github.com/fonxian/spring-elasticsearch-example/tree/master/spring-elasticsearch-example)
一、整合过程
引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
添加配置
server.port=8085
spring.data.elasticsearch.cluster-nodes = bei1:9300
elasticsearch.cluster.name=coffe-elasticsearch
创建实体类和数据访问类
实体类
@Document(indexName = "book",type = "book")
public class Book {
@Id
private String id;
private String name;
private Long price;
@Version
private Long version;
public Map<Integer, Collection<String>> getBuckets() {
return buckets;
}
public void setBuckets(Map<Integer, Collection<String>> buckets) {
this.buckets = buckets;
}
@Field(type = FieldType.Nested)
private Map<Integer, Collection<String>> buckets = new HashMap();
public Book(){}
public Book(String id, String name,Long version) {
this.id = id;
this.name = name;
this.version = version;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getPrice() {
return price;
}
public void setPrice(Long price) {
this.price = price;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}
数据访问类
@Component
public interface BookRepository extends ElasticsearchRepository<Book,String> {
Page<Book> findByNameAndPrice(String name, Long price, Pageable pageable);
Page<Book> findByNameOrPrice(String name, Long price, Pageable pageable);
Page<Book> findByName(String name, Pageable pageable);
}
二、单元测试、测试整合结果
使用单元测试测试使用结果
创建测试类
@SpringBootTest
@RunWith(SpringRunner.class)
public class BookRepositoryTest {
@Autowired
private BookRepository repository;
@Autowired
private ElasticsearchTemplate esTemplate;
}
(1)添加文档
/**
* 插入文档
*/
@Test
public void indexBook() {
Book book = new Book();
book.setId("123456");
book.setName("瓦尔登湖");
book.setPrice(20L);
book.setVersion(1L);
repository.save(book);
Book book2 = new Book();
book2.setId("234567");
book2.setName("Java编程思想");
book2.setPrice(88L);
book2.setVersion(1L);
repository.save(book2);
Book book3 = new Book();
book3.setId("8910");
book3.setName("程序员的自我修养");
book3.setPrice(56L);
book3.setVersion(1L);
repository.save(book3);
}
(2)查询所有文档
/**
* 获取所有文档
*/
@Test
public void getAll() {
repository.findAll().forEach(book -> {
System.out.println(book.getName());
System.out.println(book.getPrice());
});
}

(3)使用查询条件
/**
* 使用查询条件
*/
@Test
public void queryByNameOrPrice() {
Page<Book> books = repository.findByNameOrPrice("瓦尔登湖", 56L, Pageable.unpaged());
books.forEach(book -> {
System.out.println(book.getName());
System.out.println(book.getPrice());
});
}

(4)使用原生方式查询
/**
* 原生方式查询字段
*/
@Test
public void queryByName() {
QueryBuilder queryBuilder = new QueryStringQueryBuilder("修养").field("name");
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.build();
Page<Book> bookPage = esTemplate.queryForPage(searchQuery, Book.class);
bookPage.getContent().forEach(book -> {
System.out.println(book.getName());
});
}

参考文档
SpringBoot轻松整合ElasticSearch的更多相关文章
- SpringBoot进阶教程(七十三)整合elasticsearch
Elasticsearch 是一个分布式.高扩展.高实时的搜索与数据分析引擎.它能很方便的使大量数据具有搜索.分析和探索的能力.充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- ElasticSearch(2)---SpringBoot整合ElasticSearch
SpringBoot整合ElasticSearch 一.基于spring-boot-starter-data-elasticsearch整合 开发环境:springboot版本:2.0.1,elast ...
- SpringBoot整合elasticsearch
在这一篇文章开始之前,你需要先安装一个ElasticSearch,如果你是mac或者linux可以参考https://www.jianshu.com/p/e47b451375ea,如果是windows ...
- java框架之SpringBoot(13)-检索及整合Elasticsearch
ElasticSearch介绍 简介 我们的应用经常需要使用检索功能,开源的 Elasticsearch 是目前全文搜索引擎的首选.它可以快速的存储.搜索和分析海量数据.SpringBoot 通过整合 ...
- SpringBoot 2.x 整合ElasticSearch的demo
SpringBoot 2.x 整合ElasticSearch的demo 1.配置文件application.yml信息 # Tomcat server: tomcat: uri-encoding: U ...
- springboot整合elasticsearch入门例子
springboot整合elasticsearch入门例子 https://blog.csdn.net/tianyaleixiaowu/article/details/72833940 Elastic ...
- SpringBoot 2.x (12):整合Elasticsearch
Elasticsearch:一个优秀的搜索引擎框架 搜索方面最基本的是SQL的like语句 进一步的有Lucene框架 后来有企业级的Solr框架 而Elasticsearch框架尤其适合于数据量特别 ...
- Springboot整合elasticsearch以及接口开发
Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...
随机推荐
- Scala 枚举介绍及深入应用
本文详细地总结了Scala枚举的几种实现方式,对我们更好地进行函数式编程有很好地指导和帮助. Scala 枚举示例和特性 枚举(Enumerations)是一种语言特性,对于建模有限的实体集来说特别有 ...
- Django_cookie_session
登录时候后台打印request.COOKIE 1.login页面正确登录的话,后台页面可以获取到浏览器携带的cookie的. 2.第一行的sessionid其实就是cookie值 3.session的 ...
- ES 19 - Elasticsearch的检索语法(_search API的使用)
目录 1 Search API的基本用法 1.1 查询所有数据 1.2 响应信息说明 1.3 timeout超时机制 1.4 查询多索引和多类型中的数据 2 URI Search的用法 2.1 GET ...
- RSA签名和验证数据
private const string PubKey = "BgIAAACkAABSU0ExAAQAAAEAAQAxg/L6l3AyA+Zd7Hm7ESCcS4CcgY8PvwE2arRv ...
- maven构建失败。
maven项目编译报“Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1” 解决方案:setting.x ...
- mqtt服务器apollo的搭建和测试工具paho的使用
(1)前言 MQTT协议是IBM开发的一个即时通讯协议; 基于发布/订阅的消息协议,近些年来被广泛应用于能源.电力.....等硬件性能低下的远程设备,此外国内很多企业使用MQTT作为android手机 ...
- [翻译 EF Core in Action 1.8] MyFirstEfCoreApp应用程序设置
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- 第6章 令牌撤销端点(Token Revocation Endpoint) - IdentityModel 中文文档(v1.0.0)
OAuth 2.0令牌撤销的客户端库是作为扩展方法提供的HttpClient. 以下代码撤消撤销端点处的访问令牌令牌: var client = new HttpClient(); var resul ...
- vue 中使用promise
init1(){return new Promise((resolve, reject) => { let data={ dateStr:this.time }; api.get('url', ...
- Css3 笔记 动画 和定位属性
transform 变形属性属性:translate 平移,rotate 旋转, scale 缩放,skew 倾斜 ◆ translate :指定对象的2D平移第一个参数对应X轴,第二参数对应Y轴:如 ...