中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

elasticsearch7.2的增刪改查語法

發布時間:2021-08-16 17:37:12 來源:億速云 閱讀:295 作者:chen 欄目:編程語言

本篇內容主要講解“elasticsearch7.2的增刪改查語法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“elasticsearch7.2的增刪改查語法”吧!

首先在pom.xml里面導入es相關依賴:
  <!-- 引入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);   
        }
  }

}

到此,相信大家對“elasticsearch7.2的增刪改查語法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

时尚| 南城县| 黔西| 平原县| 任丘市| 玛多县| 贺兰县| 澄江县| 威信县| 闽清县| 治多县| 徐闻县| 壶关县| 华坪县| 兰西县| 桃源县| 顺义区| 鄱阳县| 两当县| 望奎县| 衢州市| 五莲县| 通山县| 正安县| 南华县| 盐亭县| 重庆市| 新源县| 兴安县| 西平县| 正蓝旗| 湟中县| 纳雍县| 宿州市| 肃北| 台中县| 平南县| 婺源县| 泊头市| 禹州市| 宁武县|