完整项目代码地址(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());
}); }

参考文档

spring-data-elasticsearch

SpringBoot轻松整合ElasticSearch的更多相关文章

  1. SpringBoot进阶教程(七十三)整合elasticsearch

    Elasticsearch 是一个分布式.高扩展.高实时的搜索与数据分析引擎.它能很方便的使大量数据具有搜索.分析和探索的能力.充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更 ...

  2. SpringBoot整合ElasticSearch实现多版本的兼容

    前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...

  3. ElasticSearch(2)---SpringBoot整合ElasticSearch

    SpringBoot整合ElasticSearch 一.基于spring-boot-starter-data-elasticsearch整合 开发环境:springboot版本:2.0.1,elast ...

  4. SpringBoot整合elasticsearch

    在这一篇文章开始之前,你需要先安装一个ElasticSearch,如果你是mac或者linux可以参考https://www.jianshu.com/p/e47b451375ea,如果是windows ...

  5. java框架之SpringBoot(13)-检索及整合Elasticsearch

    ElasticSearch介绍 简介 我们的应用经常需要使用检索功能,开源的 Elasticsearch 是目前全文搜索引擎的首选.它可以快速的存储.搜索和分析海量数据.SpringBoot 通过整合 ...

  6. SpringBoot 2.x 整合ElasticSearch的demo

    SpringBoot 2.x 整合ElasticSearch的demo 1.配置文件application.yml信息 # Tomcat server: tomcat: uri-encoding: U ...

  7. springboot整合elasticsearch入门例子

    springboot整合elasticsearch入门例子 https://blog.csdn.net/tianyaleixiaowu/article/details/72833940 Elastic ...

  8. SpringBoot 2.x (12):整合Elasticsearch

    Elasticsearch:一个优秀的搜索引擎框架 搜索方面最基本的是SQL的like语句 进一步的有Lucene框架 后来有企业级的Solr框架 而Elasticsearch框架尤其适合于数据量特别 ...

  9. Springboot整合elasticsearch以及接口开发

    Springboot整合elasticsearch以及接口开发 搭建elasticsearch集群 搭建过程略(我这里用的是elasticsearch5.5.2版本) 写入测试数据 新建索引book( ...

随机推荐

  1. eShopOnContainers 知多少[8]:Ordering microservice

    1. 引言 Ordering microservice(订单微服务)就是处理订单的了,它与前面讲到的几个微服务相比要复杂的多.主要涉及以下业务逻辑: 订单的创建.取消.支付.发货 库存的扣减 2. 架 ...

  2. com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server 报错问题

    com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known serve ...

  3. [深度应用]·实战掌握Dlib人脸识别开发教程

    [深度应用]·实战掌握Dlib人脸识别开发教程 个人网站--> http://www.yansongsong.cn/ 项目GitHub地址--> https://github.com/xi ...

  4. 补习系列(16)-springboot mongodb 数据库应用技巧

    目录 一.关于 MongoDB 二.Spring-Data-Mongo 三.整合 MongoDB CRUD A. 引入框架 B. 数据库配置 C. 数据模型 D. 数据操作 E. 自定义操作 四.高级 ...

  5. Smobiler Service是什么?(Smobiler——.NET移动开发平台)

    在得知Smobiler即将推出新产品SmobilerService之后,许多人第一个疑问便是——Smobiler Service是什么? Smobiler的开发者对这个exe窗口一定不陌生,有时候因为 ...

  6. Java中三目运算符不为人知的坑

    一.思考题 以下代码可能有什么错误?为什么? import java.util.HashMap; import java.util.Map; public class Test { public st ...

  7. javascript语言精粹-笔记

    walkDOM function walkTheDOM(node, func) { func(node); node = node.firstChild; while (node) { walkThe ...

  8. MySQL数据库Inception工具学习与测试 笔记

    MySQL语句的审核,在业界都已经基本被认同了,实际上也是对MySQL语句写法的统一化,标准化,而之前的人工审核,针对标准这个问题其实是很吃力的,标准越多,DBA越累,开发也越累. 那么在这个都追求自 ...

  9. Visual Studio Code-批量在文末添加文本字段

    小技巧一例,在vs code或notepad++文末批量添加文本字段信息,便于数据信息的完整,具体操作如下: Visual Studio Code批量添加"@azureyun.com&quo ...

  10. 解决 mac 10.14.4 无法 sublime text 3207 安装 Package Control,以及安装第三方包报错 `Package Control There are no packages available for installation`

    下载最新的 sublime text 3207,无法安装 Package Control. 根据官方提示,手动安装 Package Control. 手动安装 Package Control 后,无法 ...