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

相关推荐
悻运11 分钟前
Spark论述及其作用
大数据·分布式·spark
szxinmai主板定制专家28 分钟前
国产RK3568+FPGA以 ‌“实时控制+高精度采集+灵活扩展”‌ 为核心的解决方案
大数据·运维·网络·人工智能·fpga开发·机器人
怒放吧德德1 小时前
MySQL篇:MySQL如何实时同步到ES
mysql·elasticsearch·面试
细心的莽夫2 小时前
Elasticsearch复习笔记
java·大数据·spring boot·笔记·后端·elasticsearch·docker
痕5173 小时前
spark和Hadoop之间的对比和联系
大数据·hadoop·spark
CXH7283 小时前
hadoop分布式部署
大数据·hadoop·分布式
码农周4 小时前
Elasticsearch 报错 Limit of total fields [1000] has been exceeded
大数据·elasticsearch
1momomo汉堡包4 小时前
spark和hadoop之间的对比和联系
大数据·hadoop·spark
Elastic 中国社区官方博客5 小时前
Elasticsearch 堆内存使用情况和 JVM 垃圾回收
大数据·jvm·数据库·elasticsearch·搜索引擎·全文检索
博弈美业系统Java源码5 小时前
连锁美业管理系统「数据分析」的重要作用分析︳博弈美业系统疗愈系统分享
java·大数据·前端·后端·创业创新