ElasticSearch 7.6.x 版本 2020最新版 JavaRest api
點個關注吧,球球啦!
持續更新中……
前言
週末閒來無事,到官網學習(fanyi)了下最新版本的ESJavaRest API。
ES版本:7.6.2
Springboot版本:2.2.6
目錄
準備工作及注意事項
1. Maven導包
Springboot 2.2.6預設幫我們匯入的是ES 6.8.7的包,所以我們需要更改下版本
<elasticsearch.version>7.6.1</elasticsearch.version>
2. 初始化Client
@Configuration
public class ElasticSearchConfig {
@Bean
public RestHighLevelClient client(){
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("localhost",9200,"http")
));
return client;
}
}
索引(index)
增加索引
//建立索引
@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);
}
文件(document)
新增文件
//新增文件
@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());
}
資料(datas)
批量插入資料
//批量插入資料
@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
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
點贊!收藏!轉發!!!麼麼噠!
————————————————
相關文章
- 容器化部署目前最新版本的Elasticsearch--8.15.1Elasticsearch
- ElasticSearch—— Java APIElasticsearchJavaAPI
- Elasticsearch Search APIElasticsearchAPI
- ElasticSearch Java APIElasticsearchJavaAPI
- ElasticSearch Java API使用ElasticsearchJavaAPI
- elasticsearch(八)---search apiElasticsearchAPI
- Elasticsearch(二)——Rest APIElasticsearchRESTAPI
- ElasticSearch – API ConventionsElasticsearchAPI
- elasticsearch api client使用ElasticsearchAPIclient
- Elasticsearch cat api的用法ElasticsearchAPI
- ElasticSearch之基本用法APIElasticsearchAPI
- 使用 Java API 操作 elasticsearchJavaAPIElasticsearch
- Elasticsearch的Bulk API使用ElasticsearchAPI
- JDK14最新版本中的新增80種新功能和API - AzulJDKAPI
- ElasticSearch 2.3.3 java API操作二ElasticsearchJavaAPI
- Elasticsearch 7.6.2版本搭建Elasticsearch
- ElasticSearch安裝及java Api使用ElasticsearchJavaAPI
- Elasticsearch的PHP的API使用(一)ElasticsearchPHPAPI
- YOYOW最新版本“繁星”釋出
- CocoaPods最新版本升級
- OpenStack釋出最新版本Ocata
- Elasticsearch7.6.2(目前最新版本)叢集搭建及Head外掛——最詳細安裝及配置Elasticsearch
- Elasticsearch-PHP 公用類 Laravel 版本ElasticsearchPHPLaravel
- 【小白學PyTorch】7 最新版本torchvision.transforms常用API翻譯與講解PyTorchORMAPI
- Android Sdk版本、Support包版本及常用框架最新版本彙總Android框架
- WebMisSharp更新了,最新版本1.5.2,WebMisCentral-Client最新版Webclient
- Elasticsearch 入門實戰(8)--REST API 使用二(Search API)ElasticsearchRESTAPI
- MacOs 升級 Git 到最新版本MacGit
- npm node升級到最新版本NPM
- 最新版本;及加油站分析
- JSDM框架最新版本介紹JS框架
- elasticsearch常用請求介面Rest API示例ElasticsearchRESTAPI
- Elasticsearch和SpringBoot版本對應ElasticsearchSpring Boot
- 微服務--API版本控制微服務API
- 2020-12-11 elasticsearchElasticsearch
- Ubuntu 安裝最新版本 Node.jsUbuntuNode.js
- MacOS X APK 最新版本 反編譯MacAPK編譯
- 最大乘積;及最新版本分析