分布式搜索 Elasticsearch —— 刪除索引

為什么80%的碼農都做不了架構師?>>> ??hot3.png

刪除索引的方式很多,這里列舉三種。

  1. 指定 index 、type、id 執行刪除
    1. package com.gsoft.gsearch.util;import org.elasticsearch.action.get.GetResponse;
      import org.junit.Test;import com.gsoft.gsearch.BaseTest;
      import com.gsoft.gsearch.entity.Person;public class DeleteTest extends BaseTest {@Testpublic void delete() {try {String id = "1234567890";Person p = new Person();p.setId(id);p.setAge(20);p.setIsStudent(false);p.setSex("男");p.setName("張三的車");String source = ElasticSearchUtil.BeanToJson(p);// 創建索引client.prepareIndex(index, type, id).setSource(source).execute();System.out.println("休息6秒鐘,以便創建索引");Thread.sleep(6000);// GetSystem.out.println("================================第一次Get");showById(id);client.prepareDelete(index, type, id).execute();System.out.println("休息6秒鐘,以便刪除索引");Thread.sleep(6000);System.out.println("================================第二次Get");showById(id);} catch (Exception e) {e.printStackTrace();} finally {if (null != client) {client.close();}if (null != node) {node.close();}}}/*** 根據ID查詢,使用Get* @param id* @throws Exception*/private void showById(String id) throws Exception {GetResponse response = client.prepareGet(index, type, id).execute().actionGet();String json = response.getSourceAsString();if (null != json) {Person newPerson = mapper.readValue(json, Person.class);System.out.println("id\t\t" + newPerson.getId());System.out.println("name\t\t" + newPerson.getName());System.out.println("sex\t\t" + newPerson.getSex());System.out.println("age\t\t" + newPerson.getAge());System.out.println("isStudent\t\t" + newPerson.getIsStudent());} else {log.info("未查詢到任何結果!");}}
      }
      

      ?

  2. 使用 Bulk 執行批量操作
    1. package com.gsoft.gsearch.util;import org.elasticsearch.action.bulk.BulkRequestBuilder;
      import org.elasticsearch.action.search.SearchResponse;
      import org.elasticsearch.index.query.QueryBuilder;
      import org.elasticsearch.index.query.QueryBuilders;
      import org.elasticsearch.search.SearchHit;
      import org.elasticsearch.search.SearchHits;
      import org.junit.Test;import com.gsoft.gsearch.BaseTest;
      import com.gsoft.gsearch.entity.Person;public class BulkDeleteTest extends BaseTest {@Testpublic void delete() {try {String id = "1234567890";BulkRequestBuilder builder1 = client.prepareBulk();for (int i = 0; i < 5; i++) {String myId = id + "_" + i;Person p = new Person();p.setId(myId);p.setAge(20);p.setIsStudent(false);p.setSex("男");p.setName("張三的車");String source = ElasticSearchUtil.BeanToJson(p);// 創建索引builder1.add(client.prepareIndex(index, type, myId).setSource(source).request());}builder1.execute();System.out.println("休息6秒鐘,以便創建索引");Thread.sleep(6000);// GetSystem.out.println("================================第一次查詢");query();BulkRequestBuilder builder = client.prepareBulk();for (int i = 0; i < 5; i++) {String myId = id + "_" + i;builder.add(client.prepareDelete(index, type, myId).request());}builder.execute();System.out.println("休息6秒鐘,以便刪除索引");Thread.sleep(6000);System.out.println("================================第二次查詢");query();} catch (Exception e) {e.printStackTrace();} finally {if (null != client) {client.close();}if (null != node) {node.close();}}}private void query() throws Exception {// 檢索QueryBuilder qb = QueryBuilders.matchPhraseQuery("name", "張三的車");SearchResponse searchResponse = client.prepareSearch(index).setTypes(type).setQuery(qb).setFrom(0).setSize(12).execute().actionGet();SearchHits hits = searchResponse.getHits();if (null == hits || hits.totalHits() == 0) {log.error("使用\"張三的車\"沒有查詢到任何結果!");} else {for (SearchHit hit : hits) {String json = hit.getSourceAsString();Person newPerson = mapper.readValue(json, Person.class);System.out.println("id\t\t" + newPerson.getId());System.out.println("name\t\t" + newPerson.getName());System.out.println("sex\t\t" + newPerson.getSex());System.out.println("age\t\t" + newPerson.getAge());System.out.println("isStudent\t\t" + newPerson.getIsStudent());System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++");}}}
      }
      

      ?

  3. 根據條件刪除匹配的索引
    1. QueryBuild qb = QueryBuilders.matchAllQuery();
      client.prepareDeleteQuery(indeices).setQuery(qb).execute();

      ?

?

第一種 和 第二種 需要知道 索引的ID后方可執行刪除,局限性較大,第三種根據條件刪除匹配索引,也比比較簡單。

?

?

?

?

轉載于:https://my.oschina.net/exit/blog/806993

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/259920.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/259920.shtml
英文地址,請注明出處:http://en.pswp.cn/news/259920.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

springmvc攔截器對請求參數解密_SpringMVC攔截器如何修改請求參數

攔截器1&#xff0c;基本攔截器&#xff1a;package cn.ijava.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servle…

SQL Server 2008安裝配置說明書+簡單使用 親測可用

SQL Server 2008 序列號&#xff1a;Developer: PTTFM-X467G-P7RH2-3Q6CG-4DMYBEnterprise: JD8Y6-HQG69-P9H84-XDTPG-34MBB 產品秘藥JD8Y6-HQG69-P9H84-XDTPG-34MBB 下面只說企業版安裝說明 SQL Server版本&#xff1a;SQL Server 2008 企業版。 安裝Microsoft SQL Server 20…

計算機云客戶端,藍奏云網盤客戶端 0.3.7電腦版

藍奏云由于不限速、下載速度快被很多用戶所歡迎&#xff0c;不過藍奏云沒有客戶端&#xff0c;上傳下載有時也不太方便,這里有大神寫了藍奏云網盤客戶端&#xff0c;采用藍奏云API項目使用PyQt5實現圖形界面&#xff0c;藍奏云盤API項目實現了對藍奏網盤的基本操作: 登錄、列出…

IT知識免費學習視頻地址大全

Jquery2.0實戰 http://edu.ibeifeng.com/view-index-id-318.html使用SSH框架技術開發學籍管理系統-Hibernate 部分http://edu.ibeifeng.com/view-index-id-319.htmlSpring 實戰:使用 SSH 框架技術開發學籍管理系統http://edu.ibeifeng.com/view-index-id-320.htmlStruts 實戰:使…

三十分鐘學會SED

本文承接之前寫的三十分鐘學會AWK一文&#xff0c;在學習完AWK之后&#xff0c;趁熱打鐵又學習了一下SED&#xff0c;不得不說這兩個工具真的堪稱文本處理神器&#xff0c;誰用誰知道&#xff01;本文大部分內容依舊是翻譯自Tutorialspoint上的入門教程&#xff0c;這次是 Sed …

unity實現圖片輪播效果_Unity實現圖片輪播組件

游戲中有時候會見到圖片輪播的效果&#xff0c;那么這里就自己封裝了一個&#xff0c;包括自動輪播、切頁按鈕控制、頁碼下標更新、滑動輪播、切頁后的回調等等 。下面&#xff0c;先上一個簡陋的gif動態效果圖從圖中可以看出&#xff0c;該示例包括了三張圖片的輪播&#xff0…

[置頂] 2013騰訊編程馬拉松初賽第4場(3月24)(HDU 4520 HDU4521 HDU4522 HDU4523 HDU4524)...

話說昨天比賽終于拿到一個不錯的名次&#xff0c;rank77&#xff0c;對于我們這種ACM弱菜的學校來說已經很好了&#xff0c;可惜我1003用了倆floyd超時&#xff0c;如果我最近稍微搞搞圖論的話&#xff0c;用個bellman&#xff0c;或者SPFA&#xff0c;絕對超不了了就。。。哎。…

計算機學院年會,重慶大學計算機學院舉行2019年迎新晚會

2019年12月6號晚&#xff0c;重慶大學計算機學院2019年迎新晚會在蘭園小劇場舉行。出席本次晚會的嘉賓有計算機學院黨委副書記兼紀委書記郭坤銀、黨委組織員劉霜、2016級輔導員李若菡老師、2017級輔導員古曦老師、2018級輔導員鄭田青老師、2019級輔導員謝璧如老師。本次晚會的主…

[轉貼]Cocos2d-x3.2與OpenGL渲染總結(一)Cocos2d-x3.2的渲染流程

看了opengles有一段時間了&#xff0c;算是了解了一下下。然后&#xff0c;就在基本要決定還是回歸cocos2dx 3.2的&#xff0c;看了這篇好文章&#xff0c;欣喜轉之~ 推薦看原帖&#xff1a; Cocos2d-x3.2與OpenGL渲染總結(一)Cocos2d-x3.2的渲染流程 最近幾天&#xff0c;我都…

省賽熱身賽之Median

原題&#xff1a; Description A median is described as the numeric value separating the higher half of a list, from the lower half. The median of a finite list of numbers can be found by arranging all the elements from lowest value to highest value and pick…

win32 段寄存器怎么尋址

32位cpu 地址線擴展成了32位&#xff0c;這和數據線的寬度是一致的。因此&#xff0c;在32位機里其實并不需要采用“物理地址段&#xff1a;偏移”這種地址表達方式。原來在16位機里規定的 每一個段不大于64kb在32位機里也不是必要的。所以&#xff0c;對于32位機來講&#xff…

聯想拯救者y7000p加內存條_筆記本怎么升級內存和硬盤 聯想Y7000P加裝內存和硬盤圖文教程 (全文)...

一般目前新買的筆記本電腦,大都是標配8GB內存和單塊固態硬盤,內存和硬盤容量適中,但對于一些制圖設計、偏大型游戲,又或者對硬盤存儲要求比較高的用戶來說,顯然就不太夠用,這時候我們一般會通過升級內存和硬盤來解決。那么,筆記本怎么升級內存和硬盤?下面以聯想Y7000P筆…

計算機組裝與維護實訓1,計算機組裝與維護實訓報告[1]

計算機組裝與維護實訓報告[1] (12頁)本資源提供全文預覽&#xff0c;點擊全文預覽即可全文預覽,如果喜歡文檔就下載吧&#xff0c;查找使用更方便哦&#xff01;11.90 積分實習報告設計題目&#xff1a; 計算機組裝與維護實習 專業班級&#xff1a; 計算機應用103班 學生姓名&a…

node.js-------使用路由模塊

路由需要的信息&#xff0c;包括URL 及GET 或 POST參數。路由根據這些參數執行相應的js處理程序&#xff0c;因此&#xff0c;需要在HTTP請求中提取出URL以及GET或POST參數。這些請求參數在request對象中&#xff0c;這個對象是onRequest()回調函數的第一個參數。需要提取這些信…

Docker - 在CentOS 7中安裝Docker

在CentOS 7中安裝Docker 1-確認系統信息 # cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) # uname -a Linux CentOS-7 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux 2-安裝docker # yum -y install docker 3…

HDU 1715 大菲波數 (大數問題)

/* 復習大數問題&#xff1b; */ #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <iomanip> using namespace std;int nu…

springcloud 相同服務名_SpringCloud系列之SpringCloud Stream

SpringCloud Stream技術興起的原因&#xff1a;為了解決系統中不同中間件的適配問題&#xff0c;出現了cloud stream&#xff0c;采用適配綁定的方式&#xff0c;自動給不同的MQ之間進行切換。屏蔽底層消息中間件的差異&#xff0c;降低切換成本&#xff0c;統一消息的編程模型…

計算機意外重啟或遇錯誤無法繼續,計算機意外地重新啟動或遇到錯誤如何解決?...

電腦小白在重裝系統后難免會遇到些問題&#xff0c;有的容易處理&#xff0c;有的會有些棘手。那么&#xff0c;計算機意外地重新啟動或遇到錯誤如何解決?今天快啟動小編為大家分享詳細的計算機意外地重新啟動或遇到錯誤的解決方法&#xff0c;獻給對系統重裝知識不太了解的小…

jqueryui的Tooltip使用方法

http://api.jqueryui.com/tooltip/#option-position&#xff0c;詳細使用方法。 http://jqueryui.com/tooltip/&#xff0c;DEMO。 content使用 $( ".selector" ).tooltip({ content: "Awesome title!" });//div及相關標簽使用樣式&#xff0c;鼠標放上去時…

iOS 開發者賬號共用發布證書 (Distribution)問題

蘋果客服回復&#xff1a; 1.第一臺申請發布證書的電腦&#xff0c;從鑰匙串中導出發布證書(Distribution)頒發的request文件&#xff1f;然后在第二臺電腦上用request文件新生成一個Distribution證書&#xff0c;這個是可以共用的&#xff1f;&#xff08;不理解還是理解錯了&…