?ES官方提供了各种不同语言的客户端,用来操作ES。这些客户端的本质就是组装DSL语句,
通过http请求发送给ES。官方文档地址: https://www.elastic.co/quide/en/elasticsearch/client/index.html
目录
(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>?
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字符串
??