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( ...
随机推荐
- LCA 各种神奇的LCA优化方法
LCA(Least Common Ancestors) 树上问题的一种. 朴素lca很简单啦,我就不多说了,时间复杂度n^2 1.倍增LCA 时间复杂度 nlongn+klogn 其实是一种基于朴素l ...
- 【爆料】-《西澳大学毕业证书》UWA一模一样原件
☞西澳大学毕业证书[微/Q:2544033233◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归&a ...
- Go 实现 自动检索 API 错误码代码行 并 打印成文档,例 markDown 形式等
作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...
- 计算机17-3,4作业A
A货车过隧道问题 Description 输入若干组数据,每组数据中有三个整数分别表示某条公路沿途所经过的三个隧道的最大高度,数之间用单个空格分隔.输入高度单位是厘米,范围在0到762之间.现有一台高 ...
- Windows环境下springboot集成redis的安装与使用
一,redis安装 首先我们需要下载Windows版本的redis压缩包地址如下: https://github.com/MicrosoftArchive/redis/releases 连接打开后如下 ...
- len(x) 击败 x.len(),从内置函数看 Python 的设计思想
内置函数是 Python 的一大特色,用极简的语法实现很多常用的操作. 它们预先定义在内置命名空间中,开箱即用,所见即所得.Python 被公认是一种新手友好型的语言,这种说法能够成立,内置函数在其中 ...
- Mybatis增删改查,Demo整合
第一步:MyBatis的Jar包引入mybatis-3.2.7.jarmysql-connector-java-5.1.8.jar MyBatis的pom.xml依赖 <dependencies ...
- Http通讯Util
目录 HttpUtil类 HttpUtil类 import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...
- JQuery --- 第二期 (jQuery属性操作)
个人学习笔记 1.JQuery的内容选择器 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- openlayers4 入门开发系列之小区信号扇形图篇
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...