当前位置:   article > 正文

ES的安装和RestClient的操作_es restclient

es restclient

目录

初识elasticsearch

什么是elasticsearch

elasticsearch的发展

Lucene的优缺点

elasticsearch的优势

倒排索引 

es与mysql的概念对比

文档

索引

概念对比

架构

安装es

安装kibana

安装ik分词器 

分词器

安装ik分词器

ik分词器的拓展和停用词典

操作索引库

mapping属性

创建索引库

查询、删除、修改索引库

文档操作

新增查询删除文档

修改文档

方法一:全量修改

方法二:增量修改

RestClient的操作

什么是RestClient

hotel数据结构分析

索引库操作

初始化JavaRestClient

创建索引库

删除索引库

判断索引库是否存在

文档操作

初始化JavaRestClient

添加文档

查询文档

修改文档

删除文档

批量导入文档


什么是elasticsearch
1、一个开源的分布式搜索引擎,可以用来实现搜索、日志统计、分析、系统监控等功能
什么是elastic stack (ELK) ?
2、是以elasticsearch为核心的技术栈,包括beats、Logstash、kibana、elasticsearch
什么是Lucene
3、是Apache的开源搜索引擎类库,提供了搜索引擎的核心API


什么是文档和词条?

1、每一条数据就是一个文档
2、对文档中的内容分词,得到的词语就是词条
什么是正向索引?
1、基于文档id创建索引。查询词条时必须先找到文档,而后判断是否包含词条
什么是倒排索引
1、对文档内容分词,对词条创建索引,并记录词条所在文档的信息。查询时先根据词条查询到文档id,而后获取到文档


文档:一条数据就是一个文档,es中是Json格式
字段:Json文档中的字段
索引:同类型文档的集合
映射:索引中文档的约束,比如字段名称、类型
elasticsearch与数据库的关系:
1、数据库负责事务类型操作
2、elasticsearch负责海量数据的搜索、分析、计算


分词器的作用是什么?
1、创建倒排索引时对文档分词
2、用户搜索时,对输入的内容分词
IK分词器有几种模式?
1、ik_smart:智能切分,粗粒度

2、ik_max_word:最细切分,细粒度
IK分词器如何拓展词条?如何停用词条?
1、利用config目录的lkAnalyzer.cfg.xml文件添加拓展词典和停用词典

2、在词典中添加拓展词条或者停用词条


mapping常见属性有哪些?

1、type:数据类型
2、index:是否索引

3、analyzer:分词器

4、properties:子字段

type常见的有哪些?

1、字符串:text、keyword
2、数字:long、integer、short、 byte、double、float

3、布尔:boolean
4、日期:date
5、对象:object


索引库操作有哪些?
1、创建索引库:PUT/索引库名
2、查询索引库:GET/索引库名
3、删除索引库:DELETE/索引库名
4、添加字段:PUT/索引库名/_mapping


文档操作有哪些?
1、创建文档:POST/索引库名/_doc/文档id { json文档}
2、查询文档:GET/索引库名/_doc/文档id
3、删除文档:DELETE/索引库名/_doc/文档id
4、修改文档:
全量修改:PUT/索引库名/_doc/文档id { json文档}
增量修改:POST/索引库名/_update/文档id { "doc":{字段}}


索引库操作的基本步骤:
1、初始化RestHighLevelClient
2、创建XxxIndexRequest。XXX是CREATE、Get、Delete
3、准备DSL ( CREATE时需要)
4、发送请求。调用RestHighLevelClient#tindices().xxx()方法,xxx是create、exists、delete


文档操作的基本步骤:
1、初始化RestHighLevelClient
2、创建XxxRequest。XXX是Index、Get、Update、Delete
3、准备参数((Index和Update时需要)
4、发送请求。调用RestHighLevelClient#t.xxx()方法,xxx是index、get、update、delete
5、解析结果(Get时需要)

初识elasticsearch

什么是elasticsearch

elasticsearch是一款非常强大的开源搜索引擎,可以帮助我们从海量数据中快速找到需要的内容。

elasticsearch结合kibana、Logstash、Beats,也就是elastic stack (ELK)。被广泛应用在日志数据分析、实时监控等领域。
elasticsearch是elastic stack的核心,负责存储、搜索、分析数据。

elasticsearch的发展

Lucene的优缺点

Lucene的优势:

1、易扩展
2、高性能(基于倒排索引)

Lucene的缺点:
1、只限于Java语言开发

2、学习曲线陡峭
3、不支持水平扩展 

elasticsearch的优势

相比与lucene, elasticsearch具备下列优势:

1、支持分布式,可水平扩展
2、提供Restful接口,可被任何语言调用

倒排索引 

elasticsearch采用倒排索引:
1、文档(document):每条数据就是一个文档

2、词条(term):文档按照语义分成的词语

es与mysql的概念对比

文档

1、elasticsearch是面向文档存储的,可以是数据库中的一条商品数据,一个订单信息。

2、文档数据会被序列化为json格式后存储在elasticsearch中。 

索引

索引 ( index):相同类型的文档的集合
映射(mapping):索引中文档的字段约束信息,类似表的结构约束

概念对比

MySQLElasticsearch说明
TableIndex索引(index),就是文档的集合,类似数据库的表(table)
RowDocument文档(Document),就是一条条的数据,类似数据库中的行(ROW),文档都是JSON格式
ColumnField字段(Field),就是JSON文档中的字段,类似数据库中的列(Column)
SchemaMappingMapping(映射)是索引中文档的约束,例如字段类型约束。类似数据库的表结构(Schema)
SQLDSLDSL是elasticsearch提供的JSON风格的请求语句,用来操作elasticsearch,实现CRUD

架构

Mysql:擅长事务类型操作,可以确保数据的安全和一致性

Elasticsearch:擅长海量数据的搜索、分析、计算

安装es

创建网络:es-net是自己取的名字,随便取

docker network create es-net

加载镜像:使用提供的es.tar,拖到虚拟机的tmp目录下  

es.taricon-default.png?t=N7T8https://pan.baidu.com/s/13Z74D-liQaDL0_tM-Rl7Rg?pwd=47qm运行加载命令:

docker load -i es.tar

运行docker命令:

docker run -d \
    --name es \
    -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
    -e "discovery.type=single-node" \
    -v es-data:/usr/share/elasticsearch/data \
    -v es-plugins:/usr/share/elasticsearch/plugins \
    --privileged \
    --network es-net \
    -p 9200:9200 \
    -p 9300:9300 \
elasticsearch:7.12.1

访问网页:虚拟机ip和9200端口。成功

安装kibana

加载镜像:使用提供的es.tar,拖到虚拟机的tmp目录下  

kibanaicon-default.png?t=N7T8https://pan.baidu.com/s/1N3NiLRxLzX42jMxkK9ackQ?pwd=lh2y运行docker命令

docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://es:9200 \
--network=es-net \
-p 5601:5601  \
kibana:7.12.1

访问网页:虚拟机ip和5601端口。成功

点击主页的Explore on my own后,打开Dev Tools

模拟一次请求:可以看到右边的数据和端口9200的数据一模一样 

安装ik分词器 

分词器

es在创建倒排索引时需要对文档分词;在搜索时,需要对用户输入内容分词。但默认的分词规则对中文处理并不友好。

语法说明:
POST:请求方式
/_analyze:请求路径

请求参数,json风格:analyzer:分词器类型。text:要分词的内容 

我们可以看到:右边的数据分词并不友好,比如:世界本应该是一起的,它却分开了

安装ik分词器

查看es-plugins数据卷所在的位置

docker volume inspect es-plugins 

把ik文件夹放到该路径:Mountpoint就是要的位置

ik文件夹icon-default.png?t=N7T8https://pan.baidu.com/s/1EIkGJDvVjcGx06hDUo34Eg?pwd=dads重启es

docker restart es

测试

ik_smart:最少切分

ik_max_word:最细切分  

ik_smart:从最多字开始判断是否切分,若切分,则不会再继续判断已被切分的词是否继续切分 

ik_max_word:会判断每个词是否能再继续分 

ik分词器的拓展和停用词典

修改一个ik分词器目录中的config目录中的lkAnalyzer.cfg.xml文件

在第一个箭头这行,添加ext.dic:这是要用来拓展词典的文件,要在lkAnalyzer.cfg.xml所在的同级目录下创建出来。

在第二个箭头这行,添加stopword.dic:这是要用来停用词典的文件,在lkAnalyzer.cfg.xml所在的同级目录已经创建好了,不需要再创建。

在ext.dic添加要拓展的词典

在stopword.dic添加要停用的词典

重启es:这次重启后,需要几十秒的时间才能访问网站,否则网站会报错

docker restart es 

再次访问网站,可以看到这次测试“奥利给”并没有被分开,并且“的”字并没有出现在右边。

是因为“奥利给”被写进了ext.dic, “的”被写进了stopword.dic

操作索引库

mapping属性

mapping是对索引库中文档的约束,常见的mapping属性包括:

1、type:字段数据类型,常见的简单类型有:
1.1、字符串:text(可分词的文本)、keyword(精确值,例如:品牌、国家、ip地址)

1.2、数值:long、integer、short、byte、double、float

1.3、布尔:boolean
1.4、日期:date

1.5、对象:object

2、index:是否创建索引,默认为true

3、analyzer:使用哪种分词器
4、properties:该字段的子字段

创建索引库

这里创建了一个名叫heima的索引库,mappings代表它是做映射的,properties代表里面是具体的字段,type代表该字段的数据类型,analyzer代表该字段的分词器,index代表该字段是否创建索引

PUT /heima
{
  "mappings": {
    "properties": {
      "info":{
        "type": "text",
        "analyzer": "ik_smart"
      },
      "email": {
        "type": "keyword",
        "index": false
      },
      "name": {
        "type": "object",
        "properties": {
          "firstName": {
            "type": "keyword"
          },
          "lastName": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

运行该DSL语句后,右边会出现创建的索引库名字,代表创建成功

查询、删除、修改索引库

查看索引库语法

GET /索引库名称 

删除索引库语法 

DELETE /索引库名

索引库和mapping一旦创建无法修改,但是可以添加新的字段

PUT /索引库名称/_mapping
{
  "properties": {
    "新字段名": {
    }
  }
}

文档操作

新增查询删除文档

新增文档语法

POST /索引库名/_doc/文档id
{
  "字段1": "值1",
  "字段2": "值2",
  "字段3": {
    "子属性1": "值3",
    "子属性2": "值4"
  }
}

新增文档,右边的数据result为created。成功 

查询文档语法

GET /索引库名/_doc/文档id 

查询文档,右边会出现文档的数据。成功

删除文档语法

DELETE /索引库名/_doc/文档id

删除文档,右边数据result为deleted,成功 

修改文档

方法一:全量修改

会删除旧文档,添加新文档

PUT /索引库名/_doc/文档id
{
  "字段1": "值1",
  "字段2": "值2"

当文档存在时:修改后,右边的数据result为updated。修改成功

当文档不存在时:修改变成了创建。

方法二:增量修改

修改指定的字段,注意只能写指定的字段,而不是把所有字段都写上

POST /索引库名/_update/文档id
{
  "doc": {
    "字段1": "值1"
  }

修改后,右边的result为updated。修改成功 

RestClient的操作

什么是RestClient

ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,通过http请求发送给ES。

下载提供的资料

hotel的demo和sqlicon-default.png?t=N7T8https://pan.baidu.com/s/1uxl7PzshHu09PXsd9zDWbA?pwd=v8bf在本地新建一个数据库:heima,若使用其他数据库名,记得在demo里修改yml文件 

hotel数据结构分析

写出数据库中该表的DSL语句,但是不要执行。我们要使用java来执行

PUT /hotel
{
  "mappings": {
    "properties": {
      "id": {
        "type": "keyword"
      },
      "name": {
        "type": "text",
        "analyzer": "ik_max_word",
        "copy_to": "all"
      },
      "address": {
        "type": "keyword",
        "index": false
      },
      "price": {
        "type": "integer"
      },
      "score": {
        "type": "integer"
      },
      "brand": {
        "type": "keyword",
        "copy_to": "all"
      },
      "city": {
        "type": "keyword"
      },
      "starName": {
        "type": "keyword"
      },
      "business": {
        "type": "keyword",
        "copy_to": "all"
      },
      "location": {
        "type": "geo_point"
      },
      "pic": {
        "type": "keyword",
        "index": false
      },
      "all": {
        "type": "text",
        "analyzer": "ik_max_word"
      }
    }
  }

索引库操作

初始化JavaRestClient

在hotel.pom文件中引入es的RestHighLevelclient依赖

<!--elasticsearch-->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本

<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.12.1</elasticsearch.version>
</properties> 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.3.10.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>cn.itcast.demo</groupId>
  12. <artifactId>hotel-demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>hotel-demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <elasticsearch.version>7.12.1</elasticsearch.version>
  19. </properties>
  20. <dependencies>
  21. <!--elasticsearch-->
  22. <dependency>
  23. <groupId>org.elasticsearch.client</groupId>
  24. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot</groupId>
  28. <artifactId>spring-boot-starter-web</artifactId>
  29. </dependency>
  30. <dependency>
  31. <groupId>com.baomidou</groupId>
  32. <artifactId>mybatis-plus-boot-starter</artifactId>
  33. <version>3.4.2</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>mysql</groupId>
  37. <artifactId>mysql-connector-java</artifactId>
  38. <scope>runtime</scope>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.projectlombok</groupId>
  42. <artifactId>lombok</artifactId>
  43. <optional>true</optional>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-test</artifactId>
  48. <scope>test</scope>
  49. <exclusions>
  50. <exclusion>
  51. <groupId>org.junit.vintage</groupId>
  52. <artifactId>junit-vintage-engine</artifactId>
  53. </exclusion>
  54. </exclusions>
  55. </dependency>
  56. <!--FastJson-->
  57. <dependency>
  58. <groupId>com.alibaba</groupId>
  59. <artifactId>fastjson</artifactId>
  60. <version>1.2.71</version>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.apache.commons</groupId>
  64. <artifactId>commons-lang3</artifactId>
  65. </dependency>
  66. </dependencies>
  67. <build>
  68. <plugins>
  69. <plugin>
  70. <groupId>org.springframework.boot</groupId>
  71. <artifactId>spring-boot-maven-plugin</artifactId>
  72. <configuration>
  73. <excludes>
  74. <exclude>
  75. <groupId>org.projectlombok</groupId>
  76. <artifactId>lombok</artifactId>
  77. </exclude>
  78. </excludes>
  79. </configuration>
  80. </plugin>
  81. </plugins>
  82. </build>
  83. </project>

初始化RestHighLevelClient:xxx填虚拟机ip

RestHighLevelclient client = new RestHighLevelclgent(RestClient.builder(
        HttpHost.create( "http://xxx.xxx.xxx.xxx:9200")
) );

该代码我写成了

@BeforeEach
void setUp() {
    this.client = new RestHighLevelClient(RestClient.builder(
            HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
    ));
}

@AfterEach
void tearDown() throws IOException {
    this.client.close();
}
  1. package cn.itcast.hotel;
  2. import net.sf.jsqlparser.statement.create.index.CreateIndex;
  3. import org.apache.http.HttpHost;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.client.RequestOptions;
  6. import org.elasticsearch.client.RestClient;
  7. import org.elasticsearch.client.RestHighLevelClient;
  8. import org.elasticsearch.client.indices.CreateIndexRequest;
  9. import org.elasticsearch.client.indices.GetIndexRequest;
  10. import org.elasticsearch.common.xcontent.XContentType;
  11. import org.junit.jupiter.api.AfterEach;
  12. import org.junit.jupiter.api.BeforeEach;
  13. import org.junit.jupiter.api.Test;
  14. import java.io.IOException;
  15. import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
  16. public class HotelIndexTest {
  17. private RestHighLevelClient client;
  18. @Test
  19. void testInit() {
  20. System.out.println(client);
  21. }
  22. @BeforeEach
  23. void setUp() {
  24. this.client = new RestHighLevelClient(RestClient.builder(
  25. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  26. ));
  27. }
  28. @AfterEach
  29. void tearDown() throws IOException {
  30. this.client.close();
  31. }
  32. }

创建索引库

这个MAPPING_TEMPLATE的位置是要填写DSL语句,但因为太长,我就把它写成了常量

@Test
void createHotelIndex() throws IOException {
    //1、创建Request对象
    CreateIndexRequest request = new CreateIndexRequest("hotel");
    //2、准备请求的参数,DSL语句
    request.source(MAPPING_TEMPLATE, XContentType.JSON);
    //3、发送请求
    client.indices().create(request, RequestOptions.DEFAULT);
} 
  1. package cn.itcast.hotel;
  2. import net.sf.jsqlparser.statement.create.index.CreateIndex;
  3. import org.apache.http.HttpHost;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.client.RequestOptions;
  6. import org.elasticsearch.client.RestClient;
  7. import org.elasticsearch.client.RestHighLevelClient;
  8. import org.elasticsearch.client.indices.CreateIndexRequest;
  9. import org.elasticsearch.client.indices.GetIndexRequest;
  10. import org.elasticsearch.common.xcontent.XContentType;
  11. import org.junit.jupiter.api.AfterEach;
  12. import org.junit.jupiter.api.BeforeEach;
  13. import org.junit.jupiter.api.Test;
  14. import java.io.IOException;
  15. import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
  16. public class HotelIndexTest {
  17. private RestHighLevelClient client;
  18. @Test
  19. void testInit() {
  20. System.out.println(client);
  21. }
  22. @Test
  23. void createHotelIndex() throws IOException {
  24. //1、创建Request对象
  25. CreateIndexRequest request = new CreateIndexRequest("hotel");
  26. //2、准备请求的参数,DSL语句
  27. request.source(MAPPING_TEMPLATE, XContentType.JSON);
  28. //3、发送请求
  29. client.indices().create(request, RequestOptions.DEFAULT);
  30. }
  31. @BeforeEach
  32. void setUp() {
  33. this.client = new RestHighLevelClient(RestClient.builder(
  34. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  35. ));
  36. }
  37. @AfterEach
  38. void tearDown() throws IOException {
  39. this.client.close();
  40. }
  41. }
public static final String MAPPING_TEMPLATE = "";
  1. package cn.itcast.hotel.constants;
  2. public class HotelConstants {
  3. public static final String MAPPING_TEMPLATE = "{\n" +
  4. " \"mappings\": {\n" +
  5. " \"properties\": {\n" +
  6. " \"id\": {\n" +
  7. " \"type\": \"keyword\"\n" +
  8. " },\n" +
  9. " \"name\": {\n" +
  10. " \"type\": \"text\",\n" +
  11. " \"analyzer\": \"ik_max_word\",\n" +
  12. " \"copy_to\": \"all\"\n" +
  13. " },\n" +
  14. " \"address\": {\n" +
  15. " \"type\": \"keyword\",\n" +
  16. " \"index\": false\n" +
  17. " },\n" +
  18. " \"price\": {\n" +
  19. " \"type\": \"integer\"\n" +
  20. " },\n" +
  21. " \"score\": {\n" +
  22. " \"type\": \"integer\"\n" +
  23. " },\n" +
  24. " \"brand\": {\n" +
  25. " \"type\": \"keyword\",\n" +
  26. " \"copy_to\": \"all\"\n" +
  27. " },\n" +
  28. " \"city\": {\n" +
  29. " \"type\": \"keyword\"\n" +
  30. " },\n" +
  31. " \"starName\": {\n" +
  32. " \"type\": \"keyword\"\n" +
  33. " },\n" +
  34. " \"business\": {\n" +
  35. " \"type\": \"keyword\",\n" +
  36. " \"copy_to\": \"all\"\n" +
  37. " },\n" +
  38. " \"location\": {\n" +
  39. " \"type\": \"geo_point\"\n" +
  40. " },\n" +
  41. " \"pic\": {\n" +
  42. " \"type\": \"keyword\",\n" +
  43. " \"index\": false\n" +
  44. " },\n" +
  45. " \"all\": {\n" +
  46. " \"type\": \"text\",\n" +
  47. " \"analyzer\": \"ik_max_word\"\n" +
  48. " }\n" +
  49. " }\n" +
  50. " }\n" +
  51. "}";
  52. }

运行测试代码,可以看到控制台运行成功。

删除索引库

@Test
void testDeleteHotelIndex() throws IOException {
    //1、创建Request对象
    DeleteIndexRequest request = new DeleteIndexRequest("hotel");
    //2、发送请求
    client.indices().delete(request, RequestOptions.DEFAULT);
}
  1. package cn.itcast.hotel;
  2. import net.sf.jsqlparser.statement.create.index.CreateIndex;
  3. import org.apache.http.HttpHost;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.client.RequestOptions;
  6. import org.elasticsearch.client.RestClient;
  7. import org.elasticsearch.client.RestHighLevelClient;
  8. import org.elasticsearch.client.indices.CreateIndexRequest;
  9. import org.elasticsearch.client.indices.GetIndexRequest;
  10. import org.elasticsearch.common.xcontent.XContentType;
  11. import org.junit.jupiter.api.AfterEach;
  12. import org.junit.jupiter.api.BeforeEach;
  13. import org.junit.jupiter.api.Test;
  14. import java.io.IOException;
  15. import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
  16. public class HotelIndexTest {
  17. private RestHighLevelClient client;
  18. @Test
  19. void testInit() {
  20. System.out.println(client);
  21. }
  22. @Test
  23. void createHotelIndex() throws IOException {
  24. //1、创建Request对象
  25. CreateIndexRequest request = new CreateIndexRequest("hotel");
  26. //2、准备请求的参数,DSL语句
  27. request.source(MAPPING_TEMPLATE, XContentType.JSON);
  28. //3、发送请求
  29. client.indices().create(request, RequestOptions.DEFAULT);
  30. }
  31. @Test
  32. void testDeleteHotelIndex() throws IOException {
  33. //1、创建Request对象
  34. DeleteIndexRequest request = new DeleteIndexRequest("hotel");
  35. //2、发送请求
  36. client.indices().delete(request, RequestOptions.DEFAULT);
  37. }
  38. @BeforeEach
  39. void setUp() {
  40. this.client = new RestHighLevelClient(RestClient.builder(
  41. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  42. ));
  43. }
  44. @AfterEach
  45. void tearDown() throws IOException {
  46. this.client.close();
  47. }
  48. }

 点击该测试,控制台运行成功。 

判断索引库是否存在

@Test
void testExistsHotelIndex() throws IOException {
    //1、创建Request对象
    GetIndexRequest request = new GetIndexRequest("hotel");
    //2、发送请求
    boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
    //3、输出
    System.err.println(exists ? "索引库存在" : "索引库不存在");
}
  1. package cn.itcast.hotel;
  2. import net.sf.jsqlparser.statement.create.index.CreateIndex;
  3. import org.apache.http.HttpHost;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.client.RequestOptions;
  6. import org.elasticsearch.client.RestClient;
  7. import org.elasticsearch.client.RestHighLevelClient;
  8. import org.elasticsearch.client.indices.CreateIndexRequest;
  9. import org.elasticsearch.client.indices.GetIndexRequest;
  10. import org.elasticsearch.common.xcontent.XContentType;
  11. import org.junit.jupiter.api.AfterEach;
  12. import org.junit.jupiter.api.BeforeEach;
  13. import org.junit.jupiter.api.Test;
  14. import java.io.IOException;
  15. import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
  16. public class HotelIndexTest {
  17. private RestHighLevelClient client;
  18. @Test
  19. void testInit() {
  20. System.out.println(client);
  21. }
  22. @Test
  23. void createHotelIndex() throws IOException {
  24. //1、创建Request对象
  25. CreateIndexRequest request = new CreateIndexRequest("hotel");
  26. //2、准备请求的参数,DSL语句
  27. request.source(MAPPING_TEMPLATE, XContentType.JSON);
  28. //3、发送请求
  29. client.indices().create(request, RequestOptions.DEFAULT);
  30. }
  31. @Test
  32. void testDeleteHotelIndex() throws IOException {
  33. //1、创建Request对象
  34. DeleteIndexRequest request = new DeleteIndexRequest("hotel");
  35. //2、发送请求
  36. client.indices().delete(request, RequestOptions.DEFAULT);
  37. }
  38. @Test
  39. void testExistsHotelIndex() throws IOException {
  40. //1、创建Request对象
  41. GetIndexRequest request = new GetIndexRequest("hotel");
  42. //2、发送请求
  43. boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
  44. //3、输出
  45. System.err.println(exists ? "索引库存在" : "索引库不存在");
  46. }
  47. @BeforeEach
  48. void setUp() {
  49. this.client = new RestHighLevelClient(RestClient.builder(
  50. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  51. ));
  52. }
  53. @AfterEach
  54. void tearDown() throws IOException {
  55. this.client.close();
  56. }
  57. }

运行该测试,控制台运行成功,并且打印了“索引库不存在”,因为刚刚删除了索引库

文档操作

初始化JavaRestClient

xxx写虚拟机ip

    @BeforeEach
    void setUp() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
        ));
    }

    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }
  1. @SpringBootTest
  2. public class HotelDocumentTest {
  3. @Autowired
  4. private IHotelService hotelService;
  5. private RestHighLevelClient client;
  6. @BeforeEach
  7. void setUp() {
  8. this.client = new RestHighLevelClient(RestClient.builder(
  9. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  10. ));
  11. }
  12. @AfterEach
  13. void tearDown() throws IOException {
  14. this.client.close();
  15. }
  16. }

添加文档

@Test
void testAddDocument() throws IOException {
    //根据id查询酒店数据
    Hotel hotel = hotelService.getById(61083L);
    //转换成文档类型
    HotelDoc hotelDoc = new HotelDoc(hotel);

    //1、准备Request对象
    IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
    //2、准备json文档
    request.source("{\"name"\:\"Jack\",\"age\":21}",XContentType.JSON);
    //3、发送请求
    client.index(request,RequestOptions.DEFAULT);
} 

我这里的第二步用的是实体类转JSON的方式 

  1. package cn.itcast.hotel;
  2. import cn.itcast.hotel.pojo.Hotel;
  3. import cn.itcast.hotel.pojo.HotelDoc;
  4. import cn.itcast.hotel.service.IHotelService;
  5. import com.alibaba.fastjson.JSON;
  6. import org.apache.http.HttpHost;
  7. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  8. import org.elasticsearch.action.bulk.BulkRequest;
  9. import org.elasticsearch.action.delete.DeleteRequest;
  10. import org.elasticsearch.action.get.GetRequest;
  11. import org.elasticsearch.action.get.GetResponse;
  12. import org.elasticsearch.action.index.IndexRequest;
  13. import org.elasticsearch.action.update.UpdateRequest;
  14. import org.elasticsearch.client.RequestOptions;
  15. import org.elasticsearch.client.RestClient;
  16. import org.elasticsearch.client.RestHighLevelClient;
  17. import org.elasticsearch.client.indices.CreateIndexRequest;
  18. import org.elasticsearch.client.indices.GetIndexRequest;
  19. import org.elasticsearch.common.xcontent.XContentType;
  20. import org.junit.jupiter.api.AfterEach;
  21. import org.junit.jupiter.api.BeforeEach;
  22. import org.junit.jupiter.api.Test;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.boot.test.context.SpringBootTest;
  25. import java.io.IOException;
  26. import java.util.List;
  27. import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
  28. @SpringBootTest
  29. public class HotelDocumentTest {
  30. @Autowired
  31. private IHotelService hotelService;
  32. private RestHighLevelClient client;
  33. @Test
  34. void testAddDocument() throws IOException {
  35. //根据id查询酒店数据
  36. Hotel hotel = hotelService.getById(61083L);
  37. //转换成文档类型
  38. HotelDoc hotelDoc = new HotelDoc(hotel);
  39. //1、准备Request对象
  40. IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
  41. //2、准备json文档
  42. request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
  43. //3、发送请求
  44. client.index(request,RequestOptions.DEFAULT);
  45. }
  46. @BeforeEach
  47. void setUp() {
  48. this.client = new RestHighLevelClient(RestClient.builder(
  49. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  50. ));
  51. }
  52. @AfterEach
  53. void tearDown() throws IOException {
  54. this.client.close();
  55. }
  56. }

实体类

  1. package cn.itcast.hotel.pojo;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. @Data
  5. @NoArgsConstructor
  6. public class HotelDoc {
  7. private Long id;
  8. private String name;
  9. private String address;
  10. private Integer price;
  11. private Integer score;
  12. private String brand;
  13. private String city;
  14. private String starName;
  15. private String business;
  16. private String location;
  17. private String pic;
  18. public HotelDoc(Hotel hotel) {
  19. this.id = hotel.getId();
  20. this.name = hotel.getName();
  21. this.address = hotel.getAddress();
  22. this.price = hotel.getPrice();
  23. this.score = hotel.getScore();
  24. this.brand = hotel.getBrand();
  25. this.city = hotel.getCity();
  26. this.starName = hotel.getStarName();
  27. this.business = hotel.getBusiness();
  28. this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
  29. this.pic = hotel.getPic();
  30. }
  31. }

运行该测试,控制台运行成功

查询文档

@Test
void testGetDocumentById() throws IOException {
    //1、准备Request
    GetRequest request = new GetRequest("hotel", "61083");
    //2、发送请求,得到响应
    GetResponse response = client.get(request, RequestOptions.DEFAULT);
    //3、解析响应结果
    String json = response.getSourceAsString();
    //反序列化
    HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);
    System.out.println(hotelDoc);
}
  1. package cn.itcast.hotel;
  2. import cn.itcast.hotel.pojo.Hotel;
  3. import cn.itcast.hotel.pojo.HotelDoc;
  4. import cn.itcast.hotel.service.IHotelService;
  5. import com.alibaba.fastjson.JSON;
  6. import org.apache.http.HttpHost;
  7. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  8. import org.elasticsearch.action.bulk.BulkRequest;
  9. import org.elasticsearch.action.delete.DeleteRequest;
  10. import org.elasticsearch.action.get.GetRequest;
  11. import org.elasticsearch.action.get.GetResponse;
  12. import org.elasticsearch.action.index.IndexRequest;
  13. import org.elasticsearch.action.update.UpdateRequest;
  14. import org.elasticsearch.client.RequestOptions;
  15. import org.elasticsearch.client.RestClient;
  16. import org.elasticsearch.client.RestHighLevelClient;
  17. import org.elasticsearch.client.indices.CreateIndexRequest;
  18. import org.elasticsearch.client.indices.GetIndexRequest;
  19. import org.elasticsearch.common.xcontent.XContentType;
  20. import org.junit.jupiter.api.AfterEach;
  21. import org.junit.jupiter.api.BeforeEach;
  22. import org.junit.jupiter.api.Test;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.boot.test.context.SpringBootTest;
  25. import java.io.IOException;
  26. import java.util.List;
  27. import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
  28. @SpringBootTest
  29. public class HotelDocumentTest {
  30. @Autowired
  31. private IHotelService hotelService;
  32. private RestHighLevelClient client;
  33. @Test
  34. void testAddDocument() throws IOException {
  35. //根据id查询酒店数据
  36. Hotel hotel = hotelService.getById(61083L);
  37. //转换成文档类型
  38. HotelDoc hotelDoc = new HotelDoc(hotel);
  39. //1、准备Request对象
  40. IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
  41. //2、准备json文档
  42. request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
  43. //3、发送请求
  44. client.index(request,RequestOptions.DEFAULT);
  45. }
  46. @Test
  47. void testGetDocumentById() throws IOException {
  48. //1、准备Request
  49. GetRequest request = new GetRequest("hotel", "61083");
  50. //2、发送请求,得到响应
  51. GetResponse response = client.get(request, RequestOptions.DEFAULT);
  52. //3、解析响应结果
  53. String json = response.getSourceAsString();
  54. //反序列化
  55. HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);
  56. System.out.println(hotelDoc);
  57. }
  58. @BeforeEach
  59. void setUp() {
  60. this.client = new RestHighLevelClient(RestClient.builder(
  61. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  62. ));
  63. }
  64. @AfterEach
  65. void tearDown() throws IOException {
  66. this.client.close();
  67. }
  68. }

运行该测试,控制台运行成功,并且把数据打印了出来

修改文档

这里我只演示局部更新 

@Test
void testUpdateDocument() throws IOException {
    //1、准备Request
    UpdateRequest request = new UpdateRequest("hotel", "61083");
    //2、准备请求参数
    request.doc(
            "price", "952",
            "starName", "四钻"
    );
    //3、发送请求
    client.update(request,RequestOptions.DEFAULT);
}
  1. package cn.itcast.hotel;
  2. import cn.itcast.hotel.pojo.Hotel;
  3. import cn.itcast.hotel.pojo.HotelDoc;
  4. import cn.itcast.hotel.service.IHotelService;
  5. import com.alibaba.fastjson.JSON;
  6. import org.apache.http.HttpHost;
  7. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  8. import org.elasticsearch.action.bulk.BulkRequest;
  9. import org.elasticsearch.action.delete.DeleteRequest;
  10. import org.elasticsearch.action.get.GetRequest;
  11. import org.elasticsearch.action.get.GetResponse;
  12. import org.elasticsearch.action.index.IndexRequest;
  13. import org.elasticsearch.action.update.UpdateRequest;
  14. import org.elasticsearch.client.RequestOptions;
  15. import org.elasticsearch.client.RestClient;
  16. import org.elasticsearch.client.RestHighLevelClient;
  17. import org.elasticsearch.client.indices.CreateIndexRequest;
  18. import org.elasticsearch.client.indices.GetIndexRequest;
  19. import org.elasticsearch.common.xcontent.XContentType;
  20. import org.junit.jupiter.api.AfterEach;
  21. import org.junit.jupiter.api.BeforeEach;
  22. import org.junit.jupiter.api.Test;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.boot.test.context.SpringBootTest;
  25. import java.io.IOException;
  26. import java.util.List;
  27. import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
  28. @SpringBootTest
  29. public class HotelDocumentTest {
  30. @Autowired
  31. private IHotelService hotelService;
  32. private RestHighLevelClient client;
  33. @Test
  34. void testAddDocument() throws IOException {
  35. //根据id查询酒店数据
  36. Hotel hotel = hotelService.getById(61083L);
  37. //转换成文档类型
  38. HotelDoc hotelDoc = new HotelDoc(hotel);
  39. //1、准备Request对象
  40. IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
  41. //2、准备json文档
  42. request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
  43. //3、发送请求
  44. client.index(request,RequestOptions.DEFAULT);
  45. }
  46. @Test
  47. void testGetDocumentById() throws IOException {
  48. //1、准备Request
  49. GetRequest request = new GetRequest("hotel", "61083");
  50. //2、发送请求,得到响应
  51. GetResponse response = client.get(request, RequestOptions.DEFAULT);
  52. //3、解析响应结果
  53. String json = response.getSourceAsString();
  54. //反序列化
  55. HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);
  56. System.out.println(hotelDoc);
  57. }
  58. @Test
  59. void testUpdateDocument() throws IOException {
  60. //1、准备Request
  61. UpdateRequest request = new UpdateRequest("hotel", "61083");
  62. //2、准备请求参数
  63. request.doc(
  64. "price", "952",
  65. "starName", "四钻"
  66. );
  67. //3、发送请求
  68. client.update(request,RequestOptions.DEFAULT);
  69. }
  70. @BeforeEach
  71. void setUp() {
  72. this.client = new RestHighLevelClient(RestClient.builder(
  73. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  74. ));
  75. }
  76. @AfterEach
  77. void tearDown() throws IOException {
  78. this.client.close();
  79. }
  80. }

运行该测试,控制台运行成功

删除文档

@Test
void testDeleteDocument() throws IOException {
    //1、准备Request
    DeleteRequest request = new DeleteRequest("hotel", "61083");
    //2、发送请求
    client.delete(request,RequestOptions.DEFAULT);
}
  1. package cn.itcast.hotel;
  2. import cn.itcast.hotel.pojo.Hotel;
  3. import cn.itcast.hotel.pojo.HotelDoc;
  4. import cn.itcast.hotel.service.IHotelService;
  5. import com.alibaba.fastjson.JSON;
  6. import org.apache.http.HttpHost;
  7. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  8. import org.elasticsearch.action.bulk.BulkRequest;
  9. import org.elasticsearch.action.delete.DeleteRequest;
  10. import org.elasticsearch.action.get.GetRequest;
  11. import org.elasticsearch.action.get.GetResponse;
  12. import org.elasticsearch.action.index.IndexRequest;
  13. import org.elasticsearch.action.update.UpdateRequest;
  14. import org.elasticsearch.client.RequestOptions;
  15. import org.elasticsearch.client.RestClient;
  16. import org.elasticsearch.client.RestHighLevelClient;
  17. import org.elasticsearch.client.indices.CreateIndexRequest;
  18. import org.elasticsearch.client.indices.GetIndexRequest;
  19. import org.elasticsearch.common.xcontent.XContentType;
  20. import org.junit.jupiter.api.AfterEach;
  21. import org.junit.jupiter.api.BeforeEach;
  22. import org.junit.jupiter.api.Test;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.boot.test.context.SpringBootTest;
  25. import java.io.IOException;
  26. import java.util.List;
  27. import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
  28. @SpringBootTest
  29. public class HotelDocumentTest {
  30. @Autowired
  31. private IHotelService hotelService;
  32. private RestHighLevelClient client;
  33. @Test
  34. void testAddDocument() throws IOException {
  35. //根据id查询酒店数据
  36. Hotel hotel = hotelService.getById(61083L);
  37. //转换成文档类型
  38. HotelDoc hotelDoc = new HotelDoc(hotel);
  39. //1、准备Request对象
  40. IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
  41. //2、准备json文档
  42. request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
  43. //3、发送请求
  44. client.index(request,RequestOptions.DEFAULT);
  45. }
  46. @Test
  47. void testGetDocumentById() throws IOException {
  48. //1、准备Request
  49. GetRequest request = new GetRequest("hotel", "61083");
  50. //2、发送请求,得到响应
  51. GetResponse response = client.get(request, RequestOptions.DEFAULT);
  52. //3、解析响应结果
  53. String json = response.getSourceAsString();
  54. //反序列化
  55. HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);
  56. System.out.println(hotelDoc);
  57. }
  58. @Test
  59. void testUpdateDocument() throws IOException {
  60. //1、准备Request
  61. UpdateRequest request = new UpdateRequest("hotel", "61083");
  62. //2、准备请求参数
  63. request.doc(
  64. "price", "952",
  65. "starName", "四钻"
  66. );
  67. //3、发送请求
  68. client.update(request,RequestOptions.DEFAULT);
  69. }
  70. @Test
  71. void testDeleteDocument() throws IOException {
  72. //1、准备Request
  73. DeleteRequest request = new DeleteRequest("hotel", "61083");
  74. //2、发送请求
  75. client.delete(request,RequestOptions.DEFAULT);
  76. }
  77. @BeforeEach
  78. void setUp() {
  79. this.client = new RestHighLevelClient(RestClient.builder(
  80. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  81. ));
  82. }
  83. @AfterEach
  84. void tearDown() throws IOException {
  85. this.client.close();
  86. }
  87. }

运行该测试,控制台运行成功

批量导入文档

@Test
void testBulkRequest() throws IOException {
    //1、创建Request
    BulkRequest request = new BulkRequest();
    //2、准备参数,添加多个新增的Request,这里添加两个数据,分别是id为101和102的数据
    request.add(new IndexRequest("hotel")
           .id("101").source("json source", XContentType.JSON));
    request.add(new IndexRequest("hotel")
           .id("102").source("json source", XContentType.JSON));
    //3、发送请求
    client.bulk(request,RequestOptions.DEFAULT);
}

我这里改成这样,是因为我要把该表的所有数据都导入文档

 @Test
    void testBulkRequest() throws IOException {
        //批量查询酒店数据
        List<Hotel> hotels = hotelService.list();
        //1、创建Request
        BulkRequest request = new BulkRequest();
        //2、准备参数,添加多个新增的Request
        for(Hotel hotel:hotels){
            //转换为文档类型HotelDoc
            HotelDoc hotelDoc = new HotelDoc(hotel);
            //创建新增文档的Request对象
            request.add(new IndexRequest("hotel")
            .id(hotelDoc.getId().toString())
            .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
        }
        //3、发送请求
        client.bulk(request,RequestOptions.DEFAULT);
    }

  1. package cn.itcast.hotel;
  2. import cn.itcast.hotel.pojo.Hotel;
  3. import cn.itcast.hotel.pojo.HotelDoc;
  4. import cn.itcast.hotel.service.IHotelService;
  5. import com.alibaba.fastjson.JSON;
  6. import org.apache.http.HttpHost;
  7. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  8. import org.elasticsearch.action.bulk.BulkRequest;
  9. import org.elasticsearch.action.delete.DeleteRequest;
  10. import org.elasticsearch.action.get.GetRequest;
  11. import org.elasticsearch.action.get.GetResponse;
  12. import org.elasticsearch.action.index.IndexRequest;
  13. import org.elasticsearch.action.update.UpdateRequest;
  14. import org.elasticsearch.client.RequestOptions;
  15. import org.elasticsearch.client.RestClient;
  16. import org.elasticsearch.client.RestHighLevelClient;
  17. import org.elasticsearch.client.indices.CreateIndexRequest;
  18. import org.elasticsearch.client.indices.GetIndexRequest;
  19. import org.elasticsearch.common.xcontent.XContentType;
  20. import org.junit.jupiter.api.AfterEach;
  21. import org.junit.jupiter.api.BeforeEach;
  22. import org.junit.jupiter.api.Test;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.boot.test.context.SpringBootTest;
  25. import java.io.IOException;
  26. import java.util.List;
  27. import static cn.itcast.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
  28. @SpringBootTest
  29. public class HotelDocumentTest {
  30. @Autowired
  31. private IHotelService hotelService;
  32. private RestHighLevelClient client;
  33. @Test
  34. void testAddDocument() throws IOException {
  35. //根据id查询酒店数据
  36. Hotel hotel = hotelService.getById(61083L);
  37. //转换成文档类型
  38. HotelDoc hotelDoc = new HotelDoc(hotel);
  39. //1、准备Request对象
  40. IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
  41. //2、准备json文档
  42. request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
  43. //3、发送请求
  44. client.index(request,RequestOptions.DEFAULT);
  45. }
  46. @Test
  47. void testGetDocumentById() throws IOException {
  48. //1、准备Request
  49. GetRequest request = new GetRequest("hotel", "61083");
  50. //2、发送请求,得到响应
  51. GetResponse response = client.get(request, RequestOptions.DEFAULT);
  52. //3、解析响应结果
  53. String json = response.getSourceAsString();
  54. //反序列化
  55. HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);
  56. System.out.println(hotelDoc);
  57. }
  58. @Test
  59. void testUpdateDocument() throws IOException {
  60. //1、准备Request
  61. UpdateRequest request = new UpdateRequest("hotel", "61083");
  62. //2、准备请求参数
  63. request.doc(
  64. "price", "952",
  65. "starName", "四钻"
  66. );
  67. //3、发送请求
  68. client.update(request,RequestOptions.DEFAULT);
  69. }
  70. @Test
  71. void testDeleteDocument() throws IOException {
  72. //1、准备Request
  73. DeleteRequest request = new DeleteRequest("hotel", "61083");
  74. //2、发送请求
  75. client.delete(request,RequestOptions.DEFAULT);
  76. }
  77. @Test
  78. void testBulkRequest() throws IOException {
  79. //批量查询酒店数据
  80. List<Hotel> hotels = hotelService.list();
  81. //1、创建Request
  82. BulkRequest request = new BulkRequest();
  83. //2、准备参数,添加多个新增的Request
  84. for(Hotel hotel:hotels){
  85. //转换为文档类型HotelDoc
  86. HotelDoc hotelDoc = new HotelDoc(hotel);
  87. //创建新增文档的Request对象
  88. request.add(new IndexRequest("hotel")
  89. .id(hotelDoc.getId().toString())
  90. .source(JSON.toJSONString(hotelDoc),XContentType.JSON));
  91. }
  92. //3、发送请求
  93. client.bulk(request,RequestOptions.DEFAULT);
  94. }
  95. @BeforeEach
  96. void setUp() {
  97. this.client = new RestHighLevelClient(RestClient.builder(
  98. HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
  99. ));
  100. }
  101. @AfterEach
  102. void tearDown() throws IOException {
  103. this.client.close();
  104. }
  105. }

运行该测试,控制台运行成功,并且可以看到导入了201条数据,正好表的数据量

代码文件点击下载icon-default.png?t=N7T8https://pan.baidu.com/s/1NJnwlfThymqPRhqWqqP0FQ?pwd=6c0n 上一篇:SpringAMQP的配置和使用

下一篇:DSL查询语法和RestClient查询文档

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

闽ICP备14008679号