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. Spring JPA事务

    目录 1. 概述 促进阅读: 2. 配置不带XML的事务 3. 使用XML配置事务 4. @Transactional 注解 5. 潜在的陷阱 5.1. 事务和代理 5.2. 更改隔离级别 5.3. ...

  2. 第九节:ASP.NET Core 中多环境的使用

    一. 环境相关配置 1. 说明 ASP.NET Core 在应用启动时读取环境变量 ASPNETCORE_ENVIRONMENT, ASPNETCORE_ENVIRONMENT 可设置为任意值,但框架 ...

  3. SpringBoot扩展点之一:SpringApplicationRunListener

    三种监听器的关系 ApplicationListener.SpringApplicationRunListeners.SpringApplicationRunListener的关系: SpringAp ...

  4. ElasticSearch中碰到的C10K问题

    Elasticsearch基于Netty解决C10K问题背后的原理是JAVA NIO中的IO多路复用机制,涉及到三大"组件":SelectableChannel.Selector. ...

  5. Go语言系列教程(十二)之函数完结篇

    Hello,各位小伙伴大家好,我是小栈君.上一期我们讲到了关于函数的有参.无参.匿名函数,本期我们分享一下关于go语言函数类型.匿名函数和闭包的概念和实战.闲话不多说,立马开始分享. 在Go语言中,函 ...

  6. laravel中hasOne、HasMany、belongsTo、belongsToMany的ORM方法

    在laravel5.4框架中,使用ORM关联方法,一对一,一对多 一对一关系,代码: user表为主表,需要向下找关联表的字段用hasOne video表为关联表,需要向上找关联表的字段用belong ...

  7. 详解JS与Jquery获得的对象的区别与联系

    世上无难事只怕有心人,敲代码也一样只要你用心去搞懂一件事,即使一个小小的用法对你以后也会有很大的作用: 项目虽然赶得紧但是有些问题百度找完答案解决之后,也要自己梳理一遍做到心领神会!!!今天就直接来上 ...

  8. 爬虫requests库 之爬虫贴吧

    首先要观察爬虫的URL规律,爬取一个贴吧所有页的数据,观察点击下一页时URL是如何变化的. 思路: 定义一个类,初始化方法什么都不用管 定义一个run方法,用来实现主要逻辑 3 class Tieba ...

  9. css q标签和quotes属性的使用

    当元素是q的时候,可以无需使用::after或者::before选择器,直接就可以为q元素的内容添加标记元素. <!DOCTYPE html> <html lang="en ...

  10. AR-运行自动开票主程序报错

    问题: 在AR运行自动开票主程序时出现如下错误提示: 错误日志: raagsp()+ 当前的系统时间为 12-09-2014 07:23:58 raagsp()- 当前的系统时间为 12-09-201 ...