栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Elasticsearch集成Spring Data

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elasticsearch集成Spring Data

文章目录
  • Spring Data 框架集成
    • Spring Data 框架介绍
    • Spring Data Elasticsearch 介绍
    • Spring Data Elasticsearch 版本对比
    • 框架集成
      • 1. 创建 Maven 项目
      • 2. 修改 pom 文件,增加依赖关系
      • 3.增加配置文件
      • 4.SpringBoot 主程序
      • 5. 数据实体类
      • 6. 配置类
      • 7. DAO 数据访问对象
      • 8. 实体类映射操作
      • 9. 索引操作
        • 创建索引
        • 删除索引
      • 10. 文档操作
        • 新增文档
        • 修改文档
        • 根据id查询
        • 查询所有
        • 删除文档
        • 批量新增
        • 分页查询
      • 11. 文档搜索
        • term查询
        • term 查询加分页

Spring Data 框架集成 Spring Data 框架介绍

Spring Data 是一个用于简化数据库、非关系型数据库、索引库访问,并支持云服务的
开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持 map-reduce 框架和云计
算数据服务。 Spring Data 可以极大的简化 JPA(Elasticsearch„)的写法,可以在几乎不用
写实现的情况下,实现对数据的访问和操作。除了 CRUD 外,还包括如分页、排序等一些
常用的功能。

Spring Data 的官网: https://spring.io/projects/spring-data

Spring Data 常用的功能模块如下:

Spring Data Elasticsearch 介绍

Spring Data Elasticsearch 基于 spring data API 简化 Elasticsearch 操作,将原始操作
Elasticsearch 的客户端 API 进行封装 。 Spring Data 为 Elasticsearch 项目提供集成搜索引擎。
Spring Data Elasticsearch POJO 的关键功能区域为中心的模型与 Elastichsearch 交互文档和轻
松地编写一个存储索引库数据访问层。
官方网站: https://spring.io/projects/spring-data-elasticsearch

Spring Data Elasticsearch 版本对比

Spring boot2.3.x 一般可以兼容 Elasticsearch7.x

框架集成 1. 创建 Maven 项目 2. 修改 pom 文件,增加依赖关系

    org.springframework.boot
    spring-boot-starter-parent
    2.3.6.RELEASE
    


    
        org.projectlombok
        lombok
    
    
        org.springframework.boot
        spring-boot-starter-data-elasticsearch
    
    
        org.springframework.boot
        spring-boot-devtools
        runtime
        true
    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    
    
        org.springframework.boot
        spring-boot-test
    
    
        junit
        junit
    
    
        org.springframework
        spring-test
    

3.增加配置文件

在 resources 目录中增加 application.properties 文件

server.port=8080
# es 服务地址
elasticsearch.host=127.0.0.1
# es 服务端口
elasticsearch.port=9200
4.SpringBoot 主程序
@SpringBootApplication
public class EsJestApplication {
    public static void main(String[] args) {
        SpringApplication.run(EsJestApplication.class, args);
    }
}
5. 数据实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;


@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Product {
    private Long id;//商品唯一标识
    private String title;//商品名称
    private String category;//分类名称
    private Double price;//商品价格
    private String images;//图片地址
}
6. 配置类

 ElasticsearchRestTemplate 是 spring-data-elasticsearch 项目中的一个类,和其他 spring 项目中的 template
类似。
 在新版的 spring-data-elasticsearch 中, ElasticsearchRestTemplate 代替了原来的 ElasticsearchTemplate。
 原因是 ElasticsearchTemplate 基于 TransportClient, TransportClient 即将在 8.x 以后的版本中移除。所
以,我们推荐使用 ElasticsearchRestTemplate。
 ElasticsearchRestTemplate 基 于 RestHighLevelClient 客 户 端 的 。 需 要 自 定 义 配 置 类 , 继 承
AbstractElasticsearchConfiguration,并实现 elasticsearchClient()抽象方法,创建 RestHighLevelClient 对
象。

import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;


@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
    private String host ;
    private Integer port ;
    //重写父类方法
    @Override
    public RestHighLevelClient elasticsearchClient() {
        RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
        RestHighLevelClient restHighLevelClient = new
                RestHighLevelClient(builder);
        return restHighLevelClient;
    }
}
7. DAO 数据访问对象
import com.es.springdataes.pojo.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface ProductDao extends ElasticsearchRepository {
}

8. 实体类映射操作
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;


@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@document(indexName = "product", shards = 3, replicas = 1)
public class Product {
    //必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id"
    @Id
    private Long id;//商品唯一标识
    
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String title;//商品名称
    @Field(type = FieldType.Keyword)
    private String category;//分类名称
    @Field(type = FieldType.Double)
    private Double price;//商品价格
    @Field(type = FieldType.Keyword, index = false)
    private String images;//图片地址
}
9. 索引操作
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESIndexTest {
    //注入 ElasticsearchRestTemplate
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

  
}

先查看当前有哪些索引

GET _cat/indices?v

返回响应

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana  9XivAxMzR3mS7PjJmD9SPA   1   1          1            0        4kb            4kb
yellow open   my_index sgoDsJkzSxOXh0dEqz2FEw   1   1          0            0       208b           208b
yellow open   shopping Tcd3un3PSUyeBnqdO-ZAxQ   1   1          1            0      5.2kb          5.2kb
创建索引
//创建索引并增加映射配置
//启动时判断没有product索引,会自动创建
@Test
public void createIndex(){
//创建索引,系统初始化会自动创建索引
System.out.println("创建索引");
}

启动

查看kibana

GET _cat/indices?v

返回响应

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   product  IBQV4_ctSR6rMBdftIKAeQ   3   1          0            0       624b           624b
yellow open   .kibana  9XivAxMzR3mS7PjJmD9SPA   1   1         18            0     41.3kb         41.3kb
yellow open   my_index sgoDsJkzSxOXh0dEqz2FEw   1   1          0            0       208b           208b
yellow open   shopping Tcd3un3PSUyeBnqdO-ZAxQ   1   1          1            0      5.2kb          5.2kb

product索引已经自动创建了

删除索引
@Test
public void deleteIndex(){
    //创建索引,系统初始化会自动创建索引
    boolean flg = elasticsearchRestTemplate.deleteIndex(Product.class);
    System.out.println("删除索引 = " + flg);
}

运行

使用kibana查看

GET _cat/indices?v

返回响应

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana  9XivAxMzR3mS7PjJmD9SPA   1   1         20            1     55.2kb         55.2kb
yellow open   my_index sgoDsJkzSxOXh0dEqz2FEw   1   1          0            0       208b           208b
yellow open   shopping Tcd3un3PSUyeBnqdO-ZAxQ   1   1          1            0      5.2kb          5.2kb

product索引已经被删除。

10. 文档操作
package com.es.esjest.springdataes.test;

import com.es.esjest.springdataes.dao.ProductDao;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESProductDaoTest {
    @Autowired
    private ProductDao productDao;

}
新增文档
@Test
public void save(){
    Product product = new Product();
    product.setId(2L);
    product.setTitle("华为手机");
    product.setCategory("手机");
    product.setPrice(2999.0);
    product.setImages("http://www.dongguo/hw.jpg");
    productDao.save(product);
}

运行

kibana查看

GET product/_doc/2

返回响应

{
  "_index" : "product",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "id" : 2,
    "title" : "华为手机",
    "category" : "手机",
    "price" : 2999.0,
    "images" : "http://www.dongguo/hw.jpg"
  }
}

再创建一个

@Test
public void save(){
    Product product = new Product();
  	product.setId(1L);
    product.setTitle("小米 2 手机");
    product.setCategory("手机");
    product.setPrice(9999.0);
    product.setImages("http://www.dongguo/xm.jpg");
    productDao.save(product);
}
修改文档
@Test
public void update(){
    Product product = new Product();
    product.setId(2L);
    product.setTitle("华为荣耀");
    product.setCategory("手机");
    product.setPrice(4999.0);
    product.setImages("http://www.dongguo/hw.jpg");
    productDao.save(product);
}

运行

根据id查询
@Test
public void findById(){
    Product product = productDao.findById(1L).get();
    System.out.println(product);
}

运行

查询所有
@Test
public void findAll(){
    Iterable products = productDao.findAll();
    for (Product product : products) {
        System.out.println(product);
    }
}

启动

删除文档
@Test
public void delete(){
    Product product = new Product();
    product.setId(1L);
    productDao.delete(product);
}

启动

在kibana中查看,数据已经不存在了

将id为2的也删除掉

@Test
public void delete(){
    Product product = new Product();
    product.setId(2L);
    productDao.delete(product);
}
批量新增
@Test
public void saveAll(){
    List productList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        Product product = new Product();
        product.setId(Long.valueOf(i));
        product.setTitle("["+i+"]小米手机");
        product.setCategory("手机");
        product.setPrice(1999.0+i);
        product.setImages("http://www.dongguo/xm.jpg");
        productList.add(product);
    }
    productDao.saveAll(productList);
}

启动

查看kibana

GET product/_search
{
  "query": {
    "match_all": {}
  }
}

返回响应

{
  "took" : 97,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 5,
          "title" : "[5]小米手机",
          "category" : "手机",
          "price" : 2004.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "7",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 7,
          "title" : "[7]小米手机",
          "category" : "手机",
          "price" : 2006.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "0",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 0,
          "title" : "[0]小米手机",
          "category" : "手机",
          "price" : 1999.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 2,
          "title" : "[2]小米手机",
          "category" : "手机",
          "price" : 2001.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 3,
          "title" : "[3]小米手机",
          "category" : "手机",
          "price" : 2002.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 4,
          "title" : "[4]小米手机",
          "category" : "手机",
          "price" : 2003.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 1,
          "title" : "[1]小米手机",
          "category" : "手机",
          "price" : 2000.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "6",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 6,
          "title" : "[6]小米手机",
          "category" : "手机",
          "price" : 2005.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "8",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 8,
          "title" : "[8]小米手机",
          "category" : "手机",
          "price" : 2007.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      },
      {
        "_index" : "product",
        "_type" : "product",
        "_id" : "9",
        "_score" : 1.0,
        "_source" : {
          "_class" : "com.es.esjest.springdataes.pojo.Product",
          "id" : 9,
          "title" : "[9]小米手机",
          "category" : "手机",
          "price" : 2008.0,
          "images" : "http://www.dongguo/xm.jpg"
        }
      }
    ]
  }
}

也可以执行查询所有

分页查询
@Test
public void findByPageable(){
    //设置排序(排序方式,正序还是倒序,排序的 id)
    Sort sort = Sort.by(Sort.Direction.DESC,"id");
    //当前页,第一页从 0 开始, 1 表示第二页
    int from=0;
    //每页显示多少条
    int size = 5;
    //设置查询分页
    PageRequest pageRequest = PageRequest.of(from, size,sort);
    //分页查询
    Page productPage = productDao.findAll(pageRequest);
    for (Product Product : productPage.getContent()) {
        System.out.println(Product);
    }
}

运行

11. 文档搜索
package com.es.esjest.springdataes.test;
import com.es.esjest.springdataes.dao.ProductDao;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
public class SpringDataESSearchTest {
    @Autowired
    private ProductDao productDao;
    
    
}
term查询
@Test
public void termQuery() {
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
    Iterable products = productDao.search(termQueryBuilder);
    for (Product product : products) {
        System.out.println(product);
    }
}

运行

term 查询加分页
@Test
public void termQueryByPage() {
    int currentPage = 0;
    int pageSize = 5;
    //设置查询分页
    PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
    Iterable products = productDao.search(termQueryBuilder, pageRequest);
    for (Product product : products) {
        System.out.println(product);
    }
}

运行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358322.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号