分布式搜索 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.mzph.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;不理解还是理解错了&…