当前位置:   article > 正文

Elasticsearch集成Spring Data_springdataes集成

springdataes集成

Spring Data 框架集成

Spring Data 框架介绍

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

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

image-20211030160827968

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

image-20211030160856949

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

image-20211030160940866

Spring Data Elasticsearch 版本对比

image-20211030161002658

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

框架集成

1. 创建 Maven 项目

2. 修改 pom 文件,增加依赖关系

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.6.RELEASE</version>
    <relativePath/>
</parent>
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
</dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

3.增加配置文件

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

server.port=8080
# es 服务地址
elasticsearch.host=127.0.0.1
# es 服务端口
elasticsearch.port=9200
  • 1
  • 2
  • 3
  • 4
  • 5

4.SpringBoot 主程序

@SpringBootApplication
public class EsJestApplication {
    public static void main(String[] args) {
        SpringApplication.run(EsJestApplication.class, args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

5. 数据实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
 * @author Dongguo
 * @date 2021/10/30 0030-16:53
 * @description:
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Product {
    private Long id;//商品唯一标识
    private String title;//商品名称
    private String category;//分类名称
    private Double price;//商品价格
    private String images;//图片地址
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

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;

/**
 * @author Dongguo
 * @date 2021/10/30 0030-16:58
 * @description:
 */
@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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

7. DAO 数据访问对象

import com.es.springdataes.pojo.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;

/**
 * @author Dongguo
 * @date 2021/10/30 0030-17:27
 * @description:
 */
@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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;

/**
 * @author Dongguo
 * @date 2021/10/30 0030-16:53
 * @description:
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document(indexName = "product", shards = 3, replicas = 1)
public class Product {
    //必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id"
    @Id
    private Long id;//商品唯一标识
    /**
     * type : 字段数据类型
     * analyzer : 分词器类型
     * index : 是否索引(默认:true)
     * Keyword : 短语,不进行分词
     */
    @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;//图片地址
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

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;

/**
 * @author Dongguo
 * @date 2021/10/30 0030-17:31
 * @description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESIndexTest {
    //注入 ElasticsearchRestTemplate
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

  
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

先查看当前有哪些索引

GET _cat/indices?v
  • 1

返回响应

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
  • 1
  • 2
  • 3
  • 4
创建索引
//创建索引并增加映射配置
//启动时判断没有product索引,会自动创建
@Test
public void createIndex(){
//创建索引,系统初始化会自动创建索引
System.out.println("创建索引");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

启动

image-20211030180002874

查看kibana

GET _cat/indices?v
  • 1

返回响应

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
  • 1
  • 2
  • 3
  • 4
  • 5

product索引已经自动创建了

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

运行

image-20211030180353825

使用kibana查看

GET _cat/indices?v
  • 1

返回响应

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

  • 1
  • 2
  • 3
  • 4
  • 5

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;

/**
 * @author Dongguo
 * @date 2021/10/30 0030-18:07
 * @description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESProductDaoTest {
    @Autowired
    private ProductDao productDao;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
新增文档
/**
 * 新增
 */
@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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

运行

image-20211030181359907

kibana查看

GET product/_doc/2
  • 1

返回响应

{
  "_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"
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

再创建一个

/**
 * 新增
 */
@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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
修改文档
/**
 * 修改
 */
@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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

运行

image-20211030183745615

根据id查询
/**
 * 根据 id 查询
 */
@Test
public void findById(){
    Product product = productDao.findById(1L).get();
    System.out.println(product);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

运行

image-20211030184115292

查询所有
/**
 * 查询所有
 */
@Test
public void findAll(){
    Iterable<Product> products = productDao.findAll();
    for (Product product : products) {
        System.out.println(product);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

启动

image-20211030204424598

删除文档
/**
 * 删除
 */
@Test
public void delete(){
    Product product = new Product();
    product.setId(1L);
    productDao.delete(product);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

启动

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

image-20211030205015362

将id为2的也删除掉

@Test
public void delete(){
    Product product = new Product();
    product.setId(2L);
    productDao.delete(product);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
批量新增
/**
 * 批量新增
 */
@Test
public void saveAll(){
    List<Product> 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);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

启动

查看kibana

GET product/_search
{
  "query": {
    "match_all": {}
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

返回响应

{
  "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"
        }
      }
    ]
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160

也可以执行查询所有

image-20211030212513580

分页查询
/**
 * 分页查询
 */
@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<Product> productPage = productDao.findAll(pageRequest);
    for (Product Product : productPage.getContent()) {
        System.out.println(Product);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

运行

image-20211030212914471

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;
/**
 * @author Dongguo
 * @date 2021/10/30 0030-21:36
 * @description:
 */
@RunWith(SpringRunner.class)
public class SpringDataESSearchTest {
    @Autowired
    private ProductDao productDao;
    
    
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
term查询
/**
 * term 查询
 * search(termQueryBuilder) 调用搜索方法,参数查询构建器对象
 */
@Test
public void termQuery() {
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
    Iterable<Product> products = productDao.search(termQueryBuilder);
    for (Product product : products) {
        System.out.println(product);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

运行

image-20211030223334257

term 查询加分页
/**
 * term 查询加分页  
 */
@Test
public void termQueryByPage() {
    int currentPage = 0;
    int pageSize = 5;
    //设置查询分页
    PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
    TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
    Iterable<Product> products = productDao.search(termQueryBuilder, pageRequest);
    for (Product product : products) {
        System.out.println(product);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

运行

image-20211030223805350

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/200636
推荐阅读
相关标签
  

闽ICP备14008679号