elasticsearch7.2增刪改查
<!-- 引入Elasticsearch相關jar包-->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.2.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.elasticsearch.client</groupId>-->
<!-- <artifactId>transport</artifactId>-->
<!-- <version>7.2.0</version>-->
<!-- </dependency>-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.2.0</version>
</dependency>
<!-- <dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>8.0.0</version>
</dependency> -->
程式碼如下:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpHost; //import org.elasticsearch.action.get.GetRequest; //import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.index.query.BoolQueryBuilder; //import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.*;
public class Demo1 { private static RestHighLevelClient client; private static final String ip="170.60.140.120"; private static final int port=9200;
public static void main(String[] args) throws Exception{
client=new RestHighLevelClient(RestClient.builder(
new HttpHost(ip, port, "http")));
// indexincrement();
indexsearch();
// indexdelete();
// indexupdate();
client.close();
}
建立索引
private static void indexincrement() throws Exception{
HashMap<String, Object> jsonmap = new HashMap<>();
jsonmap.put("name","王五");
jsonmap.put("age",32);
IndexRequest indexRequest = new IndexRequest("post").source(jsonmap);
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("indexresponse = "+indexResponse);
System.out.println("indexResponse.status() = "+indexResponse.status());
}
刪除索引
private static void indexdelete() throws Exception{
//第一種,按索引id刪除某個文件。
/* DeleteRequest deleteRequest = new DeleteRequest("post", "m56EDXIBK5FuWnazQHHa");
client.delete(deleteRequest,RequestOptions.DEFAULT);*/
//第二種,一次性刪除整個索引
/* DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("post");
client.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT); */
//第三種,先按條件查詢出某範圍內所有Id,再一個一個刪除
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(boolQueryBuilder);
sourceBuilder.size(100);
SearchRequest searchRequest = new SearchRequest().indices("post");
searchRequest.source(sourceBuilder);
SearchResponse response1 = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response1.getHits().getHits();
for(SearchHit hit:hits){
String id = hit.getId();
DeleteRequest deleteRequest = new DeleteRequest("post", id);
client.delete(deleteRequest,RequestOptions.DEFAULT);
}
更新文件 ,(以map方式為例)
private static void indexupdate() throws Exception{
UpdateRequest updateRequest = new UpdateRequest("post","kCfJHHIBCUDx2GUCB0Dk");
HashMap<String, Object> jsonmap = new HashMap<>();
jsonmap.put("name","趙六");
jsonmap.put("age",16);
updateRequest.doc(jsonmap);
client.update(updateRequest,RequestOptions.DEFAULT);
}
查詢索引
private static void indexsearch(Connection conn) throws Exception{
/* 第一種,按某個id號查詢方式
GetRequest getRequest = new GetRequest("risklog", "rpBmNnIB76i_tBrAwvlC");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);*/
/*
第二種,按條件查詢整個索引表方式
*/
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(boolQueryBuilder);
//這裡設定查詢7萬條資料
sourceBuilder.size(70000);
// SearchRequest searchRequest=new SearchRequest();
SearchRequest searchRequest = new SearchRequest().indices("middleware_apache_server_access");
searchRequest.source(sourceBuilder);
SearchResponse response1 = client.search(searchRequest, RequestOptions.DEFAULT);
List<JSONObject> list = new ArrayList<>();
response1.getHits().forEach(item -> list.add(JSON.parseObject(item.getSourceAsString())));
System.out.println("list.size() = "+list.size());
// Iterator<JSONObject> iterator = list.iterator();
for(int i=0;i<list.size();i++){
JSONObject jsonObject = list.get(i);
System.out.println("jsonObject = "+jsonObject);
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31537832/viewspace-2696060/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 增刪改查
- SQL增刪改查SQL
- Mongoose查增改刪Go
- indexedDB 增刪改查Index
- mysql增刪改查MySql
- 列表的增刪改查
- 字典的增刪改查
- mongodb 基本增刪改查MongoDB
- MongoDB增刪改查操作MongoDB
- layui的增刪改查UI
- 單表增刪改查
- mybatis的增刪改查MyBatis
- EFCore之增刪改查
- redist的增刪改查Redis
- sql指令,增,刪,查,改SQL
- mysql基本增刪改查MySql
- iOS CoreData (一) 增刪改查iOS
- Jfinal+layui增刪改查UI
- SQL 基礎增、刪、改、查SQL
- [express+mongoose](增刪改查)ExpressGo
- CoreData - 簡單 增刪改查
- C# SqlSugar增刪改查C#SqlSugar
- rust sqlx 使用---增刪改查RustSQL
- PHP操作MongoDB(增刪改查)PHPMongoDB
- JavaWeb中jdbc增刪查改JavaWebJDBC
- 基本 SQL 之增刪改查(二)SQL
- JavaScript表格增刪改查詳解JavaScript
- MySQL基礎操作(增刪改查)MySql
- Kendo UI:grid完成增刪改查(*)UI
- mysql資料增刪改查操作MySql
- 02-CoreData 的增刪改查
- Layui(4) 增刪改查 完整案例UI
- JS字串操作之增刪改查JS字串
- 筆記03:sqlalchemy增刪改查筆記SQL
- cobra.Command的增刪改查
- Python中CRUD增刪改查教程Python
- js實現表格的增刪改查JS
- linux-MySQL基本指令-增刪改查LinuxMySql