Spring Data Elasticsearch提供了ElasticsearchTemplate工具类,实现了POJO与elasticsearch文档之间的映射

elasticsearch本质也是存储数据,它不支持事物,但是它的速度远比数据库快得多,

可以这样来对比elasticsearch和数据库

索引(indices)--------数据库(databases)
类型(type)------------数据表(table)
文档(Document)---------------- 行(row)
字段(Field)-------------------列(Columns )

整合:

  1.在SprinBoot工程中引入jar包

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

  2.配置文件

spring.data.elasticsearch.cluster-name=elasticsearch  //名字必须和elasticsearch.yml里面的cluster.name相同
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.repositories.enabled=true

 

  3.创建实体,并对类和属性进行标注

@Document(indexName = "item",type = "docs", shards = 1, replicas = 0)//标记为文档类型,ndexName:对应索引库名称
type:对应在索引库中的类型,shards:分片数量,默认5,replicas:副本数量,默认1
public class Item {
@Id //主键
private Long id; @Field(type = FieldType.Text, analyzer = "ik_max_word") //标记为成员变量
  FieldType,可以是text、long、short、date、integer等
  text:存储数据时候,会自动分词,并生成索引
  keyword:存储数据时候,不会分词建立索引
  analyzer:分词器名称
private String title; //标题 @Field(type = FieldType.Keyword)
private String category;// 分类 @Field(type = FieldType.Keyword)
private String brand; // 品牌 @Field(type = FieldType.Double)
private Double price; // 价格 @Field(index = false, type = FieldType.Keyword)//index:是否索引
private String images; // 图片地址

  

  4.引入模板ElasticsearchTemplate

   @Autowired
private ElasticsearchTemplate elasticsearchTemplate;

  

  5.创建一个索引

     //添加索引
@Test
public void addIndex() {
elasticsearchTemplate.createIndex(Item.class);
}

 

   6.删除索引

    //删除索引
@Test
public void delete(){
elasticsearchTemplate.deleteIndex("item");
}

  

    7.新增对象

  继承Repository提供的一些子接口,就能具备各种基本的CRUD功能,这里继承ElasticsearchCrudRepository

  首先定义一个对象的接口

public interface ItemRepository extends ElasticsearchCrudRepository<Item,Long> {

}

    然后注入ItemRepository

   @Autowired
private ItemRepository itemRepository;

    新增对象

    //新增一个对象
@Test
public void insert(){
Item item = new Item(2L,"坚果R1","手机","锤子",2500.00,"http://image.baidu.com/13123.jpg");
//Order order = new Order(20180020,"菜单");
itemRepository.save(item);
}

    批量新增

    //批量新增
@Test
public void insertList(){
List<Item> list = new LinkedList<>();
list.add(new Item(9L,"华为p20","手机","华为",3500.00,"http://image.baidu.com/13123.jpg"));
list.add(new Item(10L,"华为p30","手机","华为",5450.00,"http://image.baidu.com/13123.jpg"));
list.add(new Item(11L,"华为p30 pro","手机","华为",6980.00,"http://image.baidu.com/13123.jpg"));
itemRepository.saveAll(list);
}

 

    8.查询

    //根据字段查询所有
@Test
public void queryAll(){
//升序,相应降序为dscending
Iterable<Item> items = this.itemRepository.findAll(Sort.by("price").ascending());
for (Item item : items){
System.out.println(item);
}
}

         9.自定义查询方法

    Spring Data 的另一个强大功能,是根据方法名称自动实现功能,你的方法名叫做:findByTitle,那么它就知道你是根据title查询,然后自动帮你完成,无需写实现类。当然,方法名称要符合一定的约定:

               

   根据手机名查找手机

    //自定义方法,根据Title查询
@Test
public void findByTitle(){
Item item = this.itemRepository.findByTitle("坚果pro");
System.out.println(item);
}

    区间查询

    //根据区间查询
@Test
public void queryByPriceBetween(){
List<Item> list = this.itemRepository.findByPriceBetween(2000.00, 3500.00);
for (Item item : list) {
System.out.println("item = " + item);
}
}

    模糊查询

     //模糊查询
@Test
public void queryLikeTitle(){
List<Item> list = this.itemRepository.findByTitleLike("R2");
for (Item item : list){
System.out.println(item);
}
}

  

    使用自定义方法需要在接口里面申明方法

public interface ItemRepository extends ElasticsearchCrudRepository<Item,Long> {

    Item findByTitle(String title);

    List<Item> findByPriceBetween(double price1, double price2);

    List<Item> findByTitleLike(String title);
}

    10.自定义查询

    //自定义查询,查询数目等
@Test
public void matchQuery(){
// 构建查询条件
NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
// 添加基本分词查询
queryBuilder.withQuery(QueryBuilders.matchQuery("title","坚果"));
//获取结果
Page<Item> items = (Page<Item>) this.itemRepository.findAll();
//条数
long total = items.getTotalElements();
System.out.println("total = "+total);
for (Item item : items){
System.out.println(item);
}
} 关键的是NativeSearchQueryBuilder这个类

    分页查询

    //分页查询
@Test
public void queryByPage(){
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("category","手机"));
int page = 0;
int size = 2;
nativeSearchQueryBuilder.withPageable(PageRequest.of(page,size));
Page<Item> items = (Page<Item>) this.itemRepository.findAll();
long total = items.getTotalElements();
int totalPage = items.getTotalPages();
int nowPage = items.getNumber();
int pageSize = items.getSize();
System.out.println("总条数 = "+total);
System.out.println("总页数 = "+totalPage);
System.out.println("当前页 = "+nowPage);
System.out.println("每页大小 = "+pageSize);
for (Item item : items){
System.out.println(item);
}
}

  

   还有很多,就不意义列举

  在elasticsearch-head上查看数据

关于安装elasticsearch-head,参考:https://www.cnblogs.com/xuwenjin/p/8792919.html,

  Spring Data Elasticsearch文档:https://docs.spring.io/spring-data/elasticsearch/docs/3.1.10.RELEASE/reference/html/  

SpringBoot整合Spring Data Elasticsearch的更多相关文章

  1. springboot整合spring Data JPA

    今天敲代码,一连串的错误,我也是服气~果然,我们不是在出bug,就是在找bug的路上…… 今天完成的是springboot整合spring data JPA ,出了一连串的错,真是头大 java.sq ...

  2. SprignBoot整合Spring Data Elasticsearch

    一.原生java整合elasticsearch的API地址 https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.2/java ...

  3. springboot集成spring data ElasticSearch

    ES支持SpringBoot使用类似于Spring Data Jpa的方式查询,使得查询更加方便. 1.依赖引入 compile “org.springframework.boot:spring-bo ...

  4. 3.4_springboot2.x整合spring Data Elasticsearch

    Spring Data Elasticsearch 是spring data对elasticsearch进行的封装. 这里有两种方式操作elasticsearch: 1.使用Elasticsearch ...

  5. springboot整合spring data jpa 动态查询

    Spring Data JPA虽然大大的简化了持久层的开发,但是在实际开发中,很多地方都需要高级动态查询,在实现动态查询时我们需要用到Criteria API,主要是以下三个: 1.Criteria ...

  6. SpringBoot整合Spring Data Solr

    此文不讲solr相关,只讲整合,内容清单如下 1. maven依赖坐标 2. application.properties配置 3. Java Config配置 1. maven坐标 <depe ...

  7. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

  8. spring-boot (三) spring data jpa

    学习文章来自:http://www.ityouknow.com/spring-boot.html spring data jpa介绍 首先了解JPA是什么? JPA(Java Persistence ...

  9. 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分

    Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...

随机推荐

  1. shell备份,重命名,删除目录下面的文件

    因为经常会用到shell脚本,所以经常会写一些,但是我从来没有系统的学习过shell脚本,遇到问题,就去看手册,或者google一下,到了一定的程度才发现自己的基础真的好差.下面在系统学习shell时 ...

  2. Modbus 协议-Ascii,RTU

    Modbus 协议之 Ascii 下载地址:http://download.csdn.net/detail/woxpp/5043249 1.提供Modbus Ascii 相关发送与接收代码 2.提供M ...

  3. C# - List.Sort()自定义排序方法

    本文通过示例介绍了C#中典型容器List.Sort()的自定义排序方法,进而引出了C#中自定义排序的核心接口及方法 项目地址:自定义Sort方法 - SouthBegonia's Github Lis ...

  4. 《MySQL实战45讲》学习笔记2——MySQL的日志系统

    一.日志类型 逻辑日志:存储了逻辑SQL修改语句 物理日志:存储了数据被修改的值 二.binlog 1.定义 binlog 是 MySQL 的逻辑日志,也叫二进制日志.归档日志,由 MySQL Ser ...

  5. 整理:C#中Expression表达式的妙用

    原文:整理:C#中Expression表达式的妙用 一.目的:通过示例了解C#中Expression表达式的作用,通过表达式和反射可以写出很优雅的代码和架构,也可以完成一些看似不可能完成的任务 二.示 ...

  6. 总结:WPF中模板需要绑定父级别的ViewModel该如何处理

    原文:总结:WPF中模板需要绑定父级别的ViewModel该如何处理 <ListBox ItemsSource="{Binding ClassCollection}"> ...

  7. 史上最全HashMap红黑树解析

    HashMap红黑树解析 红黑树介绍 TreeNode结构 树化的过程 红黑树的左旋和右旋 TreeNode的左旋和右旋 红黑树的插入 TreeNode的插入 红黑树的删除 TreeNode的删除节点 ...

  8. C#利用控件mscomm32.ocx读取串口datalogic扫描枪数据

    1).开发环境VS12,语言C# 2).扫描枪品牌:datalogic 4470 3).通讯协议:串口 1.首先,第一步创建一个新工程,windows窗体应用程序,命名为TestScanner,如下: ...

  9. 分布式图片服务器FastDFS

    1. 什么是FastDFS FastDFS 是用 c 语言编写的一款开源的分布式文件系统.FastDFS 为互联网量身定制,充分考虑了冗余备份.负载均衡.线性扩容等机制,并注重高可用.高性能等指标,使 ...

  10. 使用Nginx 对Laravel 进行负载

    项目环境php7.2, nginx , Laravel,开发的微信公众号应用 .目前访问量的上升,单台服务器不能满足需求,于是用nginx做了负载.以下是一种可行性方案,目前正在使用. session ...