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

相关推荐
大数据追光猿3 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
人类群星闪耀时4 小时前
物联网与大数据:揭秘万物互联的新纪元
大数据·物联网·struts
risc1234564 小时前
【Elasticsearch】Search Templates(搜索模板)
elasticsearch
桃林春风一杯酒10 小时前
HADOOP_HOME and hadoop.home.dir are unset.
大数据·hadoop·分布式
桃木山人11 小时前
BigData File Viewer报错
大数据·java-ee·github·bigdata
B站计算机毕业设计超人11 小时前
计算机毕业设计Python+DeepSeek-R1高考推荐系统 高考分数线预测 大数据毕设(源码+LW文档+PPT+讲解)
大数据·python·机器学习·网络爬虫·课程设计·数据可视化·推荐算法
数造科技11 小时前
紧随“可信数据空间”政策风潮,数造科技正式加入开放数据空间联盟
大数据·人工智能·科技·安全·敏捷开发
逸Y 仙X14 小时前
Git常见命令--助力开发
java·大数据·git·java-ee·github·idea
元气满满的热码式15 小时前
logstash中的input插件(http插件,graphite插件)
网络·网络协议·http·elasticsearch·云原生
caihuayuan415 小时前
PHP建立MySQL持久化连接(长连接)及mysql与mysqli扩展的区别
java·大数据·sql·spring