栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

RestClient操作索引库

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版本一致

    org.elasticsearch.client
    elasticsearch-rest-high-level-client
    7.12.1

导入后查看对应的包,发现有一些包并不是指定的版本

这是因为SpringBoot的Parent中指定了关联版本,下载对应Doc后,ctrl点击左键查看版本。(如果无效就重启IDE)

继续点击查看依赖

在properties中发现是其它版本

因此需要在pom的properties中直接定义覆盖

  1. 覆盖默认的ES版本。

    1.8
    7.12.1

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


    org.elasticsearch.client
    elasticsearch-rest-high-level-client

  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 ? "索引库存在" : "索引库不存在");

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/674236.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号