赞
踩
环境搭建:project structure里面project环境配置成8(相对应的),module中source配置成8,setting里面javac编译配置成8,以及js配置6+;
自定义es版本依赖,保证和本地版本一致:
<elasticsearch.version>7.6.1</elasticsearch.version>
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("127.0.0.1", 9200, "http")
)
);
return client;
}
}
具体的Api测试
打开ElasticSearch和head。注入客户端:
@Autowired
private RestHighLevelClient restHighLevelClient;
1、创建索引
//测试索引的创建 Request
@Test
void testCreateIndex() throws IOException {
// 1.创建索引请求
CreateIndexRequest request = new CreateIndexRequest("heze_index");
// 2.客户端执行请求,IndicesClient,请求后获得响应
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(createIndexResponse);
}
2、判断索引是否存在
// 3.测试获取索引
@Test
void testExistIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("heze_index");
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
3、删除索引
// 4.测试删除索引
@Test
void testDelIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("heze_index");
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete);
}
4、创建文档
// 5.测试添加文档 @Test void testAddDocument() throws IOException { // 创建对象 User user = new User("玉环", 3); // 创建请求 IndexRequest request = new IndexRequest("heze_index"); // 规则 put /heze_index/_doc/1 request.id("1"); request.timeout(TimeValue.timeValueSeconds(1)); // request.timeout("1s"); // 将我们的数据放入请求 json request.source(JSON.toJSONString(user), XContentType.JSON); // 客户端发送请求, 获取响应的结果 IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(indexResponse.toString()); System.out.println(indexResponse.status()); }
5、crud文档
// 5.测试添加文档 @Test void testAddDocument() throws IOException { // 创建对象 User user = new User("玉环", 3); // 创建请求 IndexRequest request = new IndexRequest("heze_index"); // 规则 put /heze_index/_doc/1 request.id("1"); request.timeout(TimeValue.timeValueSeconds(1)); // request.timeout("1s"); // 将我们的数据放入请求 json request.source(JSON.toJSONString(user), XContentType.JSON); // 客户端发送请求, 获取响应的结果 IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT); System.out.println(indexResponse.toString()); System.out.println(indexResponse.status()); } // 6.获取文档,判断是否存在, get /index/doc/1 @Test void testIsExist() throws IOException { GetRequest getRequest = new GetRequest("heze_index", "1"); // 不获取返回的_source 的上下文效率更高 getRequest.fetchSourceContext(new FetchSourceContext(false)); boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT); System.out.println(exists); } // 7.获取文档信息 @Test void testGetRequest() throws IOException { GetRequest getRequest = new GetRequest("heze_index", "1"); GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); System.out.println(getResponse.getSourceAsString()); //打印文档内容 System.out.println(getResponse); //返回全部内容和命令式一样的 } // 8.更新文档信息 @Test void testUpdateRequest() throws IOException { UpdateRequest updateRequest = new UpdateRequest("heze_index", "3"); updateRequest.timeout("1s"); User user = new User("zhangsan", 18); updateRequest.doc(JSON.toJSONString(user), XContentType.JSON); UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT); System.out.println(update.status()); } // 9.删除文档记录 @Test void testDeleteRequest() throws IOException { DeleteRequest deleteRequest = new DeleteRequest("heze_index", "1"); deleteRequest.timeout("1s"); DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(delete.status()); } // 11.查询 // SearchRequest 搜索请求 // SearchSourceBuilder 条件构造 // HighlightBuilder 构建高亮 // TermQueryBuilder 精确查询 // MatchAllQueryBuilder 匹配全部 @Test void testSearch() throws IOException { SearchRequest searchRequest = new SearchRequest("heze_index"); // 构建搜索条件 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // 查询条件,我们可以使用 QueryBuilders 工具来实现 // QueryBuilders.termQuery() 匹配精确 // QueryBuilders.matchAllQuery() 匹配所有 TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zhangsan"); sourceBuilder.query(termQueryBuilder); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); searchRequest.source(sourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); System.out.println(JSON.toJSONString(searchResponse.getHits())); System.out.println("========="); for (SearchHit documentFields : searchResponse.getHits().getHits()) { System.out.println(documentFields.getSourceAsMap()); } System.out.println("========="); }
6、批量操作
// 10.特殊的,真实的项目一般都会批量插入数据 @Test void testBulkRequest() throws IOException { BulkRequest bulkRequest = new BulkRequest(); bulkRequest.timeout("10s"); ArrayList<User> users = new ArrayList<>(); users.add(new User("玉环", 3)); users.add(new User("何泽", 18)); users.add(new User("张三", 10)); users.add(new User("李四", 11)); // 批处理请求 for (int i = 0; i < users.size(); i++) { // 批量更新和批量删除,就在这里修改对应的请求就可以了 bulkRequest.add( new IndexRequest("heze_index") .id("" + (i + 1)) .source(JSON.toJSONString(users.get(i)), XContentType.JSON) ); } BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); System.out.println(bulkResponse.hasFailures()); // 返回false为添加成功 }
总结:这里是在idea中使用api进行es等一些基本的操作,与Kibana相似。简单记录基本的使用方法。下一篇实操实验。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。