ES分布式搜索-使用RestClient操作索引库

RestClient操作索引库

1、什么是RestClient?

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

2、利用JavaRestClient实现创建、删除索引库,判断索引库是否存在

基于案例操作:

2.1、导入项目和数据库

数据库结构:

项目:案例Demo

2.2、分析数据库结构编写mapping映射

json 复制代码
# 酒店的mapping
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",
        "copy_to": "all"
      },
      "startName": {
        "type": "keyword"
      },
      "business": {
        "type": "keyword"
      },
      "localtion": {
        "type": "geo_point",
      },
      "pic": {
        "type": "keyword",
        "index": false
      }
    }
  }
}

提示1:ES中支持两种地理坐标数据类型:

  • geo_point:由纬度(latitude)和经度(longitude)确定的一个点。例如:"32.8752345, 120.2981576"

  • geo_shape:有多个geo_point组成的复杂几何图形。例如一条直线,"LINESTRING (-77.03653 38.897676, -77.009051 38.889939)"

提示2:字段拷贝可以使用copy_to属性将当前字段拷贝到指定字段,如:

json 复制代码
"all": {
  "type": "text",
  "analyzer": "ik_max_word"
},
"brand": {
  "type": "keyword",
  "copy_to": "all"
}

2.3、初始化JavaRestClient

  1. 引入es的RestHighLevelClient依赖:

    xml 复制代码
    <!--    elasticsearch    -->
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.12.1</version>
    </dependency>
  2. 因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:

    xml 复制代码
    <properties>
        <java.version>11</java.version>
        <elasticsearch.version>7.12.1</elasticsearch.version>
    </properties>
  3. 初始化RestHighLevelClient:

    java 复制代码
    package cn.mannor.hotel;
    
    import org.apache.http.HttpHost;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.client.indices.CreateIndexRequest;
    import org.elasticsearch.common.xcontent.XContentType;
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    
    import java.io.IOException;
    
    import static cn.mannor.hotel.constants.HotelConstants.MAPPING_TEMPLATE;
    
    public class HotelIndexTest {
    
        private RestHighLevelClient restHighLevelClient; // 高级REST客户端,用于与Elasticsearch进行交互
    
        /**
         * 初始化测试环境,创建并配置RestHighLevelClient实例。
         */
        @BeforeEach
        void setUp() {
            this.restHighLevelClient = new RestHighLevelClient(RestClient.builder(
                    HttpHost.create("http://192.168.12.131:9200") // 设置Elasticsearch的地址和端口
            ));
        }
    
        /**
         * 测试结束后清理资源,关闭RestHighLevelClient实例。
         * @throws IOException 如果关闭客户端时发生IO错误
         */
        @AfterEach
        void tearDown() throws IOException {
            this.restHighLevelClient.close(); // 关闭高级REST客户端,释放资源
        }
    }

2.4、创建索引库

与上面初始化RestHighLevelClient写到同一个test测试

java 复制代码
package cn.mannor.hotel;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static cn.mannor.hotel.constants.HotelConstants.MAPPING_TEMPLATE;

public class HotelIndexTest {

    private RestHighLevelClient restHighLevelClient; // 高级REST客户端,用于与Elasticsearch进行交互

    @Test
    void createHotelIndex() throws IOException {
        //1.创建Request参数
        CreateIndexRequest request = new CreateIndexRequest("hotel");//传入索引名称
        //2.准备请求的参数,DSL语句
        request.source(MAPPING_TEMPLATE, XContentType.JSON);
        //3.发送请求,indices返回库中包含的所有索引库。
        restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    }

}

其中,我将我们准备查询的语句MAPPING_TEMPLATE预先写成一个静态类存放常量,直接使用即可,可以不会显得臃肿。

2.5、删除索引库

与创建索引库类似,我就直接上代码,只是替换了里面的方法DeleteIndexRequest:

java 复制代码
@Test
void deleteHotelIndex() throws IOException {
    //1.创建Request参数
    DeleteIndexRequest request = new DeleteIndexRequest("hotel");//传入索引名称
    //2.发送请求,indices返回库中包含的所有索引库。
    restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
}

2.6、判断索引库

java 复制代码
@Test
void exitsHotelIndex() throws IOException {
    //1.创建Request参数
    GetIndexRequest request = new GetIndexRequest("hotel");//传入索引名称
    //2.发送请求,indices返回库中包含的所有索引库。
    boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
    //3.输出结果
    System.out.println(exists?"存在改索引!":"不存在该索引!");
}

自此我们可以通过运行上述的代码来查看索引库是否存在,特别注意的是restHighLevelClient要返回一个boolean类。

相关推荐
淡酒交魂14 分钟前
「Flink」业务搭建方法总结
大数据·数据挖掘·数据分析
mask哥19 分钟前
详解flink java基础(一)
java·大数据·微服务·flink·实时计算·领域驱动
TDengine (老段)23 分钟前
TDengine IDMP 高级功能(4. 元素引用)
大数据·数据库·人工智能·物联网·数据分析·时序数据库·tdengine
livemetee1 小时前
Flink2.0学习笔记:Flink服务器搭建与flink作业提交
大数据·笔记·学习·flink
zhang98800002 小时前
储能领域大数据平台的设计中如何使用 Hadoop、Spark、Flink 等组件实现数据采集、清洗、存储及实时 / 离线计算,支持储能系统分析与预测
大数据·hadoop·spark
老蒋新思维3 小时前
存量竞争下的破局之道:品牌与IP的双引擎策略|创客匠人
大数据·网络·知识付费·创客匠人·知识变现
Lx3524 小时前
Hadoop日志分析实战:快速定位问题的技巧
大数据·hadoop
Elasticsearch7 小时前
Elastic 的托管 OTLP 端点:为 SRE 提供更简单、可扩展的 OpenTelemetry
elasticsearch
喂完待续7 小时前
【Tech Arch】Hive技术解析:大数据仓库的SQL桥梁
大数据·数据仓库·hive·hadoop·sql·apache
SelectDB7 小时前
5000+ 中大型企业首选的 Doris,在稳定性的提升上究竟花了多大的功夫?
大数据·数据库·apache