ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

RestClient操作索引库_创建索引库(二)

2026/9/21 22:17:37 拓冰建站 浏览量
RestClient操作索引库_创建索引库(二)

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

官方文档地址: https://www.elastic.co/quide/en/elasticsearch/client/index.html

目录

一、初始化JavaRestClient

1.1.依赖引入

1.2.初始化RestHighLevelClient

二、创建索引库


一、初始化JavaRestClient

1.1.依赖引入

(1) 引入es的RestHighLevelclient依赖

<dependency>
        <groupId>org.elasticsearch.client</groupId>

        <artifactId>elasticsearch-rest-high-level-client</artifactId>

</dependency> 

(2)因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本

<properties>

        <java.version>1.8</java.version>

        <elasticsearch.version>7.12.1</elasticsearch.version>

</properties> 

1.2.初始化RestHighLevelClient

RestHighLevelclient client = new RestHighLevelclient(Restclient.builder(

        HttpHost.create("http://192.168.15.101:9200")  

));

二、创建索引库

java代码创建索引库对比接口请求

(1) 

创建索引库代码  

@Test
void testCreateHotelIndex() throws IOException {// 1.创建Request对象CreateIndexRequest request = new CreateIndexRequest("hotel");//2请求参数,MAPPING_TEMPLATE是静态常量符串,内容是创建索引库的DSL语句    request.source(MAPPING_TEMPLATE,XContentType.JSON);// 3.发起请求client.indices().create(request,RequestOptions.DEFAULT);
}

(2) 

创建 MAPPING_TEMPLATE 请求JSON字符串