点个关注吧,球球啦!
持续更新中……
周末闲来无事,到官网学习(fanyi)了下最新版本的ESJavaRest API。
ES版本:7.6.2
Springboot版本:2.2.6
Springboot 2.2.6默认帮我们导入的是ES 6.8.7的包,所以我们需要更改下版本
<elasticsearch.version>7.6.1elasticsearch.version>
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient client(){
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost",9200,"http")
));
return client;
}
}
增加索引
//创建索引
@Test
void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("index1");
CreateIndexResponse createIndexResponse =
client.indices().create(request, RequestOptions.DEFAULT);
}
删除索引
//删除索引
@Test
void deleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("index1");
AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
boolean isSuccessful = delete.isAcknowledged();
System.out.println(isSuccessful);
}
查询索引是否存在
//返回索引是否存在
@Test
void existIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("index1");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
添加文档
//添加文档
@Test
void addDocument() throws IOException {
User user = new User("dai", 22);
IndexRequest request = new IndexRequest("index1")
.id("1")
.timeout(TimeValue.timeValueSeconds(1));
IndexRequest source = request.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
}
删除文档
//删除文档
@Test
void deleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("index1", "2");
request.timeout("1s");
DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
System.out.println(deleteResponse.status());
}
查询文档是否存在
//判断文档是否存在
@Test
void isExistDocument() throws IOException {
GetRequest request = new GetRequest("index1", "1");
//不获取_source内容,提升效率
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = client.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
更新文档
//更新文档
@Test
void updateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("index1", "1");
request.timeout("1s");
User user = new User("dai2", 22);
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
System.out.println(updateResponse.status());
}
批量插入数据
//批量插入数据
@Test
void BulkRequest() throws IOException {
BulkRequest bulkRequest = new BulkRequest()
.timeout("5s");
List<User> users = Arrays.asList(new User("dai1", 1), new User("dai2", 2), new User("dai3", 3));
for (User user : users) {
bulkRequest.add(new IndexRequest("index1")
//.id("xxx")
.source(JSON.toJSONString(user), XContentType.JSON));
}
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//是否失败,false表示成功
System.out.println(bulkResponse.hasFailures());
System.out.println(bulkResponse.status());
}
查询
//查询
@Test
void search() throws IOException {
SearchRequest request = new SearchRequest("index1");
//构建搜索条件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// SearchRequest 搜索请求
// SearchSourceBuilder 条件构造
// HighlightBuilder 构建高亮
// TermQueryBuilder 精确查询
// MatchAllQueryBuilder .....
MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
sourceBuilder.query(matchAllQueryBuilder)
.timeout(new TimeValue(60, TimeUnit.SECONDS));
request.source(sourceBuilder);
SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(searchResponse.getHits(), true));
System.out.println("===================================");
for (SearchHit documentFields : searchResponse.getHits()) {
System.out.println(documentFields.getSourceAsMap());
}
}
原创不易,白嫖不好,各位的支持和认可,就是我创作的最大动力,我们下篇文章见!
本博客仅发布于CSDN—一个帅到不能再帅的人 Mr_kidBK。转载请标明出处。
https://blog.csdn.net/Mr_kidBK
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
点赞!收藏!转发!!!么么哒!
————————————————