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类。

相关推荐
StarRocks_labs23 分钟前
从InfluxDB到StarRocks:Grab实现Spark监控平台10倍性能提升
大数据·数据库·starrocks·分布式·spark·iris·物化视图
若兰幽竹1 小时前
【Spark分析HBase数据】Spark读取并分析HBase数据
大数据·spark·hbase
R²AIN SUITE2 小时前
金融合规革命:R²AIN SUITE 如何重塑银行业务智能
大数据·人工智能
绿算技术3 小时前
“强强联手,智启未来”凯创未来与绿算技术共筑高端智能家居及智能照明领域新生态
大数据·人工智能·智能家居
只因只因爆4 小时前
spark的缓存
大数据·缓存·spark
Leo.yuan5 小时前
3D 数据可视化系统是什么?具体应用在哪方面?
大数据·数据库·3d·信息可视化·数据分析
只因只因爆5 小时前
spark小任务
大数据·分布式·spark
cainiao0806055 小时前
Java 大视界——Java 大数据在智慧交通智能停车诱导系统中的数据融合与实时更新
java·大数据·开发语言
End9288 小时前
Spark之搭建Yarn模式
大数据·分布式·spark
我爱写代码?8 小时前
Spark 集群配置、启动与监控指南
大数据·开发语言·jvm·spark·mapreduce