引入依赖
ElasticSearch就一个核心依赖,这里引入阿里的fastjson是因为ES只接受json类型的数据,不接受对象。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.33</version>
</dependency>
集成ES并使用
文章使用的连接对象
ES Client的官方文档有两套,一套低版本,一套高版本。两个版本使用的方式不同。以下使用的高版本
连接ES代码
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
return client;
}
}
索引测试代码
@SpringBootTest
public class ESIndexTest {
@Autowired
@Qualifier("restHighLevelClient")//配置类中的方法名
private RestHighLevelClient client;
/**
* 创建索引
* @throws IOException
*/
@Test
public void testCreateIndex() throws IOException {
// 创建索引请求
CreateIndexRequest request = new CreateIndexRequest("index_test1");
// 客户端执行请求
IndicesClient indices = client.indices();
CreateIndexResponse response = indices.create(request, RequestOptions.DEFAULT);
System.out.println(response);
}
/**
* 判断索引是否存在
*/
@Test
public void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("index_test1");
IndicesClient indices = client.indices();
boolean exists = indices.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
/**
* 删除指定索引
* @throws IOException
*/
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("index_test1");
IndicesClient indices = client.indices();
AcknowledgedResponse response = indices.delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
}
文档测试代码
/**
* @author evader
* @date 2021-11-14 21:57
*/
@SpringBootTest
public class ESDocumentTest {
@Autowired
// @Qualifier("restHighLevelClient") 变量名跟方法名一致时即可不指定
private RestHighLevelClient restHighLevelClient;
/**
* 添加文档
*/
@Test
public void testAddDocument() throws IOException {
// 构造插入ES的对象
User user = new User("evader", 21);
// 创建请求
IndexRequest request = new IndexRequest("document_test");
// 设置请求参数
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
// 将要插入的对象放入请求
request.source(JSON.toJSONString(user), XContentType.JSON);
// 发送请求并获取响应结果
IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println(response.status());
}
/**
* 判断文档是否存在
*/
@Test
public void testExistsDocu() throws IOException {
GetRequest request = new GetRequest("document_test", "1");
// 设置不返回_source
request.fetchSourceContext(new FetchSourceContext(false));
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
/**
* 获取文档信息
*/
@Test
public void testGetDocu() throws IOException {
GetRequest request = new GetRequest("document_test", "1");
GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
System.out.println(response);
System.out.println(response.getVersion());
System.out.println(response.getSourceAsString());
}
/**
* 更新文档信息
*/
@Test
public void testUpdateDocu() throws IOException {
UpdateRequest request = new UpdateRequest("document_test", "1");
request.timeout("1s");
User user = new User("evader1997", 24);
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
/**
* 删除文档信息
*/
@Test
public void testDeleteDocu() throws IOException {
DeleteRequest request = new DeleteRequest("document_test", "1");
request.timeout("1s");
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
}