RestClient操作索引库

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

eg:
利用JavaRestClient实现创建,删除索引,判断索引库是否存在。

分析数据结构
mapping要考虑的问题:
字段名、数据类型、是否参与搜索、是否分词、如果分词,分词器是什么

注:

  • 像id数据库中可能是Long但分词时是指定为keyword。
  • 一般城市、价格、评分这一类字段不需要分词。
  • 对于不需要搜索的字段设置index为false。
  • ES中支持两种地理坐标数据类型:geo_point、geo_shape。
  • 对于多个字段都需要组合搜索时ES提供copy_to属性,实现一个字段里搜索多个内容。

eg:

"all": {
  "type": "text",
  "analyzer": "ik_max_word"
},
"city": {
  "type": "keyword",
  "copy_to": "all"
}

初始化JavaRestClient

  1. 引入es的RestHighLevelClient依赖:
    版本要与搭建的ES版本一致
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.12.1</version>
</dependency>

导入后查看对应的包,发现有一些包并不是指定的版本
在这里插入图片描述
这是因为SpringBoot的Parent中指定了关联版本,下载对应Doc后,ctrl点击左键查看版本。(如果无效就重启IDE)
在这里插入图片描述
继续点击查看依赖
在这里插入图片描述
在properties中发现是其它版本
在这里插入图片描述
因此需要在pom的properties中直接定义覆盖

  1. 覆盖默认的ES版本。
<properties>
    <java.version>1.8</java.version>
    <elasticsearch.version>7.12.1</elasticsearch.version>
</properties>

同时第1步中的版本就可以去掉了

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
  1. 初始化RestHighLevelClient并创建索引库:
package com.yy.hotel;

import net.sf.jsqlparser.statement.create.index.CreateIndex;
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 com.yy.hotel.constants.HotelConstants.MAPPING_TEMPLATE;

public class HotelIndexTest {
    private RestHighLevelClient restHighLevelClient;

    @Test
    void testInit(){
        System.out.println(restHighLevelClient);
    }

    @Test
    void createHotelIndex() throws IOException {
//        1.创建Request对象
        CreateIndexRequest request = new CreateIndexRequest("hotel");
//        2.准备请求的参数: DSL语句,MAPPING_TEMPLATE自己定义一类存放DSL语句中json部分,然后直接引入
        request.source(MAPPING_TEMPLATE, XContentType.JSON);
//        3.发送请求,indices包含索引库中所有操作方法
        restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);

    }


//    每个单元测试中都会用,所以定义成成员变量
//    同时提交完成变更初始化就使用@BeforeEach
    @BeforeEach
    void setUp(){
        this.restHighLevelClient = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://192.168.0.106:9200")
        ));
    }

//    集群使用下面这种
//    @BeforeEach
//    void setUp(){
//        this.restHighLevelClient = new RestHighLevelClient(RestClient.builder(
//                HttpHost.create("http://192.168.0.106:9200"),
//                HttpHost.create("http://192.168.0.106:9200"),
//                HttpHost.create("http://192.168.0.106:9200")
//        ));
//    }


//    测试完成销毁
    @AfterEach
    void tearDown() throws IOException {
        this.restHighLevelClient.close();
    }
}
package com.yy.hotel.constants;

public class HotelConstants {
//  kibana中除了put那一行不要,其余都复制下面字符串中
    public static final String MAPPING_TEMPLATE ="略";
}

创建后可通过kibana验证

GET /hotel

删除索引库代码

测试方法中添加以下代码

@Test
void testDeleteHotelIndex() throws IOException {
//        1.创建Request对象
    DeleteIndexRequest request = new DeleteIndexRequest("hotel");
//        2.发送请求,indices包含索引库中所有操作方法
    restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);

}

判断索引库是否存在

@Test
void testExistsHotelIndex() throws IOException {
//        1.创建Request对象
    GetIndexRequest request = new GetIndexRequest("hotel");
//        2.发送请求,indices包含索引库中所有操作方法
    Boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);

    System.err.println(exists ? "索引库存在" : "索引库不存在");

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值