一、添加依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
二、创建索引库
1. 创建Request对象。因为是创建索引库的操作,因此Request是CreateIndexRequest。
2. 添加请求参数,其实就是DSL的JSON参数部分。因为json字符串很长,这里是定义了静态字符串常量MAPPING_TEMPLATE,让代码看起来更加优雅。
3. 发送请求,client.indices()方法的返回值是IndicesClient类型,封装了所有与索引库操作有关的方法。
示例代码:
package com.example.esdemo;
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.client.indices.GetIndexRequest;
import org.elasticsearch.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class EsDemoApplicationTests {
public static final String MAPPING_TEMPLATE = "{\n" +
" \"mappings\": {\n" +
" \"language\":{\n" +
" \"properties\": {\n" +
" \"info\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"standard\"\n" +
" },\n" +
" \"email\": {\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"name\": {\n" +
" \"type\": \"object\",\n" +
" \"properties\": {\n" +
" \"firstName\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"lastName\":{\n" +
" \"type\": \"keyword\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" \n" +
" }\n" +
"}";
private RestHighLevelClient client;
@BeforeEach
void setUp() {
// 初始化代码 建立连接
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://127.0.0.1:9200")));
}
/**
* 新增索引库
*
* @throws IOException
*/
@Test
void add() throws IOException {
// 1.创建Request对象
CreateIndexRequest request = new CreateIndexRequest("myindex");
// 2.准备请求的参数:DSL语句
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 3.发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
@AfterEach
void tearDown() throws IOException {
// 结束代码
this.client.close();
}
}
三、删除索引库
1. 创建Request对象。这次是DeleteIndexRequest对象
2.发送请求。改用delete方
package com.example.esdemo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class EsDemoApplicationTests {
private RestHighLevelClient client;
@BeforeEach
void setUp() {
// 初始化代码 建立连接
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://127.0.0.1:9200")));
}
/**
* 删除索引库
*
* @throws IOException
*/
@Test
void delete() throws IOException {
//1.创建Request对象
DeleteIndexRequest request = new DeleteIndexRequest("myindex");
// 2.发送请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
@AfterEach
void tearDown() throws IOException {
// 结束代码
this.client.close();
}
}
四、查询索引库
1. 创建Request对象。这次是GetIndexRequest对象
2.发送请求。改用exists方法
示例代码:
package com.example.esdemo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class EsDemoApplicationTests {
private RestHighLevelClient client;
@BeforeEach
void setUp() {
// 初始化代码 建立连接
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://127.0.0.1:9200")));
}
/**
* 查询索引库
*
* @throws IOException
*/
@Test
void contextLoads() throws IOException {
//1.创建Request对象
GetIndexRequest request = new GetIndexRequest("myindex");
// 2.发送请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists ? "索引库已经存在!" : "索引库不存在");
}
@AfterEach
void tearDown() throws IOException {
// 结束代码
this.client.close();
}
}
输出:
索引库不存在