完整项目代码地址(https://github.com/fonxian/spring-elasticsearch-example/tree/master/spring-elasticsearch-example)

一、整合过程

引入依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.1.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <dependencies>
  8. <dependency>
  9. <groupId>org.springframework.data</groupId>
  10. <artifactId>spring-data-elasticsearch</artifactId>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-test</artifactId>
  19. <scope>test</scope>
  20. </dependency>
  21. </dependencies>

添加配置


  1. server.port=8085
  2. spring.data.elasticsearch.cluster-nodes = bei1:9300
  3. elasticsearch.cluster.name=coffe-elasticsearch

创建实体类和数据访问类

实体类


  1. @Document(indexName = "book",type = "book")
  2. public class Book {
  3. @Id
  4. private String id;
  5. private String name;
  6. private Long price;
  7. @Version
  8. private Long version;
  9. public Map<Integer, Collection<String>> getBuckets() {
  10. return buckets;
  11. }
  12. public void setBuckets(Map<Integer, Collection<String>> buckets) {
  13. this.buckets = buckets;
  14. }
  15. @Field(type = FieldType.Nested)
  16. private Map<Integer, Collection<String>> buckets = new HashMap();
  17. public Book(){}
  18. public Book(String id, String name,Long version) {
  19. this.id = id;
  20. this.name = name;
  21. this.version = version;
  22. }
  23. public String getId() {
  24. return id;
  25. }
  26. public void setId(String id) {
  27. this.id = id;
  28. }
  29. public String getName() {
  30. return name;
  31. }
  32. public void setName(String name) {
  33. this.name = name;
  34. }
  35. public Long getPrice() {
  36. return price;
  37. }
  38. public void setPrice(Long price) {
  39. this.price = price;
  40. }
  41. public long getVersion() {
  42. return version;
  43. }
  44. public void setVersion(long version) {
  45. this.version = version;
  46. }
  47. }

数据访问类


  1. @Component
  2. public interface BookRepository extends ElasticsearchRepository<Book,String> {
  3. Page<Book> findByNameAndPrice(String name, Long price, Pageable pageable);
  4. Page<Book> findByNameOrPrice(String name, Long price, Pageable pageable);
  5. Page<Book> findByName(String name, Pageable pageable);
  6. }

二、单元测试、测试整合结果

使用单元测试测试使用结果

创建测试类


  1. @SpringBootTest
  2. @RunWith(SpringRunner.class)
  3. public class BookRepositoryTest {
  4. @Autowired
  5. private BookRepository repository;
  6. @Autowired
  7. private ElasticsearchTemplate esTemplate;
  8. }

(1)添加文档

  1. /**
  2. * 插入文档
  3. */
  4. @Test
  5. public void indexBook() {
  6. Book book = new Book();
  7. book.setId("123456");
  8. book.setName("瓦尔登湖");
  9. book.setPrice(20L);
  10. book.setVersion(1L);
  11. repository.save(book);
  12. Book book2 = new Book();
  13. book2.setId("234567");
  14. book2.setName("Java编程思想");
  15. book2.setPrice(88L);
  16. book2.setVersion(1L);
  17. repository.save(book2);
  18. Book book3 = new Book();
  19. book3.setId("8910");
  20. book3.setName("程序员的自我修养");
  21. book3.setPrice(56L);
  22. book3.setVersion(1L);
  23. repository.save(book3);
  24. }

(2)查询所有文档

  1. /**
  2. * 获取所有文档
  3. */
  4. @Test
  5. public void getAll() {
  6. repository.findAll().forEach(book -> {
  7. System.out.println(book.getName());
  8. System.out.println(book.getPrice());
  9. });
  10. }

(3)使用查询条件


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

(4)使用原生方式查询


  1. /**
  2. * 原生方式查询字段
  3. */
  4. @Test
  5. public void queryByName() {
  6. QueryBuilder queryBuilder = new QueryStringQueryBuilder("修养").field("name");
  7. SearchQuery searchQuery = new NativeSearchQueryBuilder()
  8. .withQuery(queryBuilder)
  9. .build();
  10. Page<Book> bookPage = esTemplate.queryForPage(searchQuery, Book.class);
  11. bookPage.getContent().forEach(book -> {
  12. System.out.println(book.getName());
  13. });
  14. }

参考文档

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. Angularjs interceptor

    angularJs 请求过滤 新建一个服务, $HttpProvider 中有一个 interceptore 数组,所谓的拦截器就是一个注册到该数组的工厂,该工厂在app.config() 中注入, ...

  2. 响应式编程知多少 | Rx.NET 了解下

    1. 引言 An API for asynchronous programming with observable streams. ReactiveX is a combination of the ...

  3. Java连接redis

    一.依赖包 jedis-2.1.0.jar   commons-pool-1.6.jar 二.实例 //连接参数public class RedisConfig { public static int ...

  4. asp.net core系列 53 IdentityServer4 (IS4)介绍

    一.概述 在物理层之间相互通信必须保护资源,需要实现身份验证和授权,通常针对同一个用户存储.对于资源安全设计包括二个部分,一个是认证,一个是API访问. 1 认证 认证是指:应用程序需要知道当前用户的 ...

  5. 《HelloGitHub》第 32 期

    公告 新加入了 2 位机器学期的小伙伴负责机器学习专栏.项目的首页增加合作组织一栏,如有开源组织有意合作可以点击联系我. 我们还在路上,不停地前行. <HelloGitHub>第 32 期 ...

  6. java并发编程(2) --Synchronized与Volatile区别

    Synchronized 在多线程并发中synchronized一直是元老级别的角色.利用synchronized来实现同步具体有一下三种表现形式: 对于普通的同步方法,锁是当前实例对象. 对于静态同 ...

  7. 『片段』Win32 模式窗体 消息路由

    需求背景 近来,有个需求: 和一个外部程序对接. 具体是,我这边 主程序用 Process 启动外部程序.外部程序启动后,我这边调用的窗体不允许再进行任何操作. 当外部程序关闭时,外部程序会向我这边的 ...

  8. Python:轻量级 ORM 框架 peewee 用法详解(二)——增删改查

    说明:peewee 中有很多方法是延时执行的,需要调用 execute() 方法使其执行.下文中不再特意说明这个问题,大家看代码. 本文中代码样例所使用的 Person 模型如下: class Per ...

  9. LeetCode重建二叉树系列问题总结

    二叉树天然的递归特性,使得我们可以使用递归算法对二叉树进行遍历和重建.之前已经写过LeetCode二叉树的前序.中序.后序遍历(递归实现),那么本文将进行二叉树的重建,经过对比,会发现二者有着许多相似 ...

  10. 在已有的Asp.net MVC项目中引入Taurus.MVC

    Taurus.MVC是一个优秀的框架,如果要应用到已有的Asp.net MVC项目中,需要修改一下. 1.前提约定: 走Taurus.MVC必须指定后缀.如.api 2.原项目修改如下: web.co ...