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

最新文章

  1. jQuery为开发插件提拱了两个方法:jQuery.fn.extend(); jQuery.extend();
  2. [日常训练]常州集训day7
  3. jquery选择器之内容选择器
  4. SQL Server中的20个系统变量
  5. 文成小盆友python-num11-(2) python操作Memcache Redis
  6. linux增大交换分区
  7. 【OpenCV】OpenCV2.4.6 与Visiual Studio 2008,Python2.7.5配置和图像载入显示
  8. EntityFramework Core饥饿加载忽略导航属性问题
  9. Servlet--ServletException类,UnavailableException类
  10. 使用open-falcon监控Nginx
  11. Linux shell 脚本(三)
  12. Python面试笔记一
  13. Ubuntu+IntelliJ IDEA+Android 配置NDK环境+openCV
  14. jquery判断点击事件是否指定区域
  15. [leetcode]224. Basic Calculator
  16. echarts 调整图表 位置 的方法
  17. python之路--内置模块02
  18. Revit中如何给不同构件着色
  19. 白鹭引擎 - 显示对象与 HelloWord ( 绘制了一个红蓝相间的 2 x 2 格子 )
  20. C# Task注意事项

热门文章

  1. 硬木地板 JDFZ1667
  2. Python3字符串替换replace(),translate(),re.sub()
  3. CentOS7 安装Redis 单机版
  4. PHP全栈学习笔记12
  5. DataFrameNaFunctions无fill方法
  6. Unity 用ml-agents机器学习造个游戏AI吧(2)(入门DEMO)
  7. Android版数据结构与算法(五):LinkedHashMap核心源码彻底分析
  8. spring boot 文件上传大小限制
  9. openlayers4 入门开发系列之风场图篇
  10. Flink源码分析 - 源码构建