ElasticSearch集成SpringBoot
目录
1. 依赖
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
2. 新建项目
导入依赖后我们查看SpringBoot中的ElasticSearch的版本, 发现为 7.6.2 版本, 与我们本地的 7.8.0不匹配
一定要保证我们导入的依赖和我们ES版本一致, 在我们的POM中修改版本即可
3. 配置对象
package com.wang.wangesapi.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
//配置hostname和端口
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
)
);
return client;
}
}
4. API
1. 操作索引
1. 创建索引
package com.wang.wangesapi;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
@SpringBootTest
class WangEsApiApplicationTests {
//Autowired 根据名称首字母转大写匹配类型, 这里和类型不一致,所以我们要用ID绑定
@Autowired
@Qualifier("restHighLevelClient")
private RestHighLevelClient client;
//测试索引的创建 所有的请求都使用Request创建
@Test
public void testCreateIndex() throws IOException {
// 1. 创建索引请求
CreateIndexRequest request = new CreateIndexRequest("wang_index");
// 2. 执行创建请求 第二个参数我们一般使用默认的 RequestOptions.DEFAULT
//indices ==> index的复数
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
}
2. 获取索引
// 测试获取索引
@Test
public void testExitIndex() throws IOException {
GetIndexRequest index = new GetIndexRequest("wang_index");
boolean exists = client.indices().exists(index, RequestOptions.DEFAULT);
System.out.println(exists);
}
只能判断其是否存在
3. 删除索引
// 测试删除索引
@Test
public void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("wang_index");
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
isAcknowledged() ==> 返回状态的布尔值
2. 操作文档
1. 添加文档
//测试添加文档
@Test
public void testAddDocument() throws IOException {
//创建对象
User user = new User("张三", 12);
//创建请求
IndexRequest request = new IndexRequest("wang_index");
//创建规则 put/wang_index/_doc/1
request.id("1");
//设置过期规则, 与 request.timeout("1s") 效果一致
request.timeout(TimeValue.timeValueSeconds(1));
//将我们数据放入请求 (JSON)
IndexRequest source = request.source(JSON.toJSONString(user), XContentType.JSON);
//客户端发送请求
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println(response.status());
}
2. 获取文档
首先查看文档是否存在
//获取文档, 首先判断是否存在 get/index/_doc/1
@Test
public void testIsExist() throws IOException {
GetRequest request = new GetRequest("wang_index", "1");
//不获取我们返回的 _source 的上下文了, 效率更高
request.fetchSourceContext(
new FetchSourceContext(false)
);
request.storedFields("_none_");
boolean exists = client.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
获取文档
//获取文档的信息
@Test
public void testGetDocument() throws IOException {
GetRequest request = new GetRequest("wang_index", "1");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
//打印文档的内容
System.out.println(response.getSourceAsString());
//返回的全内容和命令式是一样的
System.out.println(response);
}
结果如下
3. 更新文档
//更新文档信息
@Test
public void testUpdateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("wang_index", "1");
request.timeout("1s");
User user = new User("李四", 24);
request.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
4. 删除文档
//删除文档记录
@Test
public void testDeleteDocument () throws IOException {
DeleteRequest request = new DeleteRequest("wang_index", "1");
request.timeout("1s");
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
5. 批量操作
使用BulkRequest对象, 其他的和单独操作差不多
//批量导入数据
@Test
void testBulkRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.timeout(TimeValue.timeValueSeconds(10));
ArrayList<User> userList = new ArrayList<>();
userList.add(new User("张三1号", 3));
userList.add(new User("张三2号", 3));
userList.add(new User("张三3号", 3));
userList.add(new User("李四1号", 3));
userList.add(new User("李四2号", 3));
userList.add(new User("李四3号", 3));
//批处理请求
for (int i = 0; i < userList.size(); i++) {
bulkRequest.add(
new IndexRequest("wang_index")
.id("" + i)
.source(JSON.toJSONString(userList.get(i)), XContentType.JSON));
}
BulkResponse responses = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//是否存在失败的数据 ==> 返回false则说明全部插入成功!
System.out.println(responses.hasFailures());
}
6. 查询操作
//查询
//SearchRequest 搜索请求
//SearchSourceBuilder 条件构造
//HighlightBuilder 构建高亮
//TermQueryBuilder 精确查询
//XXXXQueryBuilder 对应我们看到的所有功能
@Test
public void testSearch() throws IOException {
SearchRequest searchRequest = new SearchRequest("wang_index");
//构建搜索的条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//查询条件, 可以使用 QueryBuilders 工具来实现
//QueryBuilders.termQuery 精确匹配
//QueryBuilder.matchAllQuery 匹配所有
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "张三1号");
sourceBuilder.query(matchQueryBuilder)
.timeout(TimeValue.timeValueSeconds(1))
//分页
.from(0).size(3);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits()));
System.out.println("=========================================");
//遍历搜索结果, 取出其中的documentFields.getSourceAsMap, 就可以得到JSON MAP的结果
for (SearchHit documentFields : searchResponse.getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
}