分布式搜索Elasticsearch——删除指定索引

来源:互联网 发布:mac系统怎样翻墙 编辑:程序博客网 时间:2024/06/09 15:42

   删除索引的方式很多,这里列举三种。

      第一种是指定index、type、id执行删除,示例代码如下:

[java] view plain copy
  1. /** 
  2.  * @author Geloin 
  3.  */  
  4. package com.gsoft.gsearch.util;  
  5.   
  6. import org.elasticsearch.action.get.GetResponse;  
  7. import org.junit.Test;  
  8.   
  9. import com.gsoft.gsearch.BaseTest;  
  10. import com.gsoft.gsearch.entity.Person;  
  11.   
  12. /** 
  13.  * @author Geloin 
  14.  *  
  15.  */  
  16. public class DeleteTest extends BaseTest {  
  17.       
  18.     @Test  
  19.     public void delete() {  
  20.         try {  
  21.   
  22.             String id = "1234567890";  
  23.             Person p = new Person();  
  24.             p.setId(id);  
  25.             p.setAge(20);  
  26.             p.setIsStudent(false);  
  27.             p.setSex("男");  
  28.             p.setName("张三的车");  
  29.   
  30.             String source = ElasticSearchUtil.BeanToJson(p);  
  31.   
  32.             // 创建索引  
  33.             client.prepareIndex(index, type, id).setSource(source).execute();  
  34.               
  35.             System.out.println("休息6秒钟,以便创建索引");  
  36.             Thread.sleep(6000);  
  37.   
  38.             // Get  
  39.             System.out.println("================================第一次Get");  
  40.             showById(id);  
  41.   
  42.             client.prepareDelete(index, type, id).execute();  
  43.               
  44.             System.out.println("休息6秒钟,以便删除索引");  
  45.             Thread.sleep(6000);  
  46.   
  47.             System.out.println("================================第二次Get");  
  48.             showById(id);  
  49.   
  50.         } catch (Exception e) {  
  51.             e.printStackTrace();  
  52.         } finally {  
  53.             if (null != client) {  
  54.                 client.close();  
  55.             }  
  56.             if (null != node) {  
  57.                 node.close();  
  58.             }  
  59.         }  
  60.     }  
  61.   
  62.     /** 
  63.      * 根据ID查询,使用Get 
  64.      *  
  65.      * @author Geloin 
  66.      * @param id 
  67.      * @throws Exception 
  68.      */  
  69.     private void showById(String id) throws Exception {  
  70.         GetResponse response = client.prepareGet(index, type, id).execute()  
  71.                 .actionGet();  
  72.         String json = response.getSourceAsString();  
  73.         if (null != json) {  
  74.             Person newPerson = mapper.readValue(json, Person.class);  
  75.             System.out.println("id\t\t" + newPerson.getId());  
  76.             System.out.println("name\t\t" + newPerson.getName());  
  77.             System.out.println("sex\t\t" + newPerson.getSex());  
  78.             System.out.println("age\t\t" + newPerson.getAge());  
  79.             System.out.println("isStudent\t\t" + newPerson.getIsStudent());  
  80.         } else {  
  81.             log.info("未查询到任何结果!");  
  82.         }  
  83.     }  
  84. }  

        第二种则使用Bulk执行批量操作,代码如下所示:

[java] view plain copy
  1. /** 
  2.  * @author Geloin 
  3.  */  
  4. package com.gsoft.gsearch.util;  
  5.   
  6. import org.elasticsearch.action.bulk.BulkRequestBuilder;  
  7. import org.elasticsearch.action.search.SearchResponse;  
  8. import org.elasticsearch.index.query.QueryBuilder;  
  9. import org.elasticsearch.index.query.QueryBuilders;  
  10. import org.elasticsearch.search.SearchHit;  
  11. import org.elasticsearch.search.SearchHits;  
  12. import org.junit.Test;  
  13.   
  14. import com.gsoft.gsearch.BaseTest;  
  15. import com.gsoft.gsearch.entity.Person;  
  16.   
  17. /** 
  18.  * @author Geloin 
  19.  *  
  20.  */  
  21. public class BulkDeleteTest extends BaseTest {  
  22.   
  23.     @Test  
  24.     public void delete() {  
  25.         try {  
  26.   
  27.             String id = "1234567890";  
  28.   
  29.             BulkRequestBuilder builder1 = client.prepareBulk();  
  30.             for (int i = 0; i < 5; i++) {  
  31.                 String myId = id + "_" + i;  
  32.                 Person p = new Person();  
  33.                 p.setId(myId);  
  34.                 p.setAge(20);  
  35.                 p.setIsStudent(false);  
  36.                 p.setSex("男");  
  37.                 p.setName("张三的车");  
  38.   
  39.                 String source = ElasticSearchUtil.BeanToJson(p);  
  40.   
  41.                 // 创建索引  
  42.                 builder1.add(client.prepareIndex(index, type, myId)  
  43.                         .setSource(source).request());  
  44.             }  
  45.   
  46.             builder1.execute();  
  47.   
  48.             System.out.println("休息6秒钟,以便创建索引");  
  49.             Thread.sleep(6000);  
  50.   
  51.             // Get  
  52.             System.out.println("================================第一次查询");  
  53.             query();  
  54.   
  55.             BulkRequestBuilder builder = client.prepareBulk();  
  56.             for (int i = 0; i < 5; i++) {  
  57.                 String myId = id + "_" + i;  
  58.                 builder.add(client.prepareDelete(index, type, myId)  
  59.                         .request());  
  60.             }  
  61.             builder.execute();  
  62.   
  63.             System.out.println("休息6秒钟,以便删除索引");  
  64.             Thread.sleep(6000);  
  65.   
  66.             System.out.println("================================第二次查询");  
  67.             query();  
  68.   
  69.         } catch (Exception e) {  
  70.             e.printStackTrace();  
  71.         } finally {  
  72.             if (null != client) {  
  73.                 client.close();  
  74.             }  
  75.             if (null != node) {  
  76.                 node.close();  
  77.             }  
  78.         }  
  79.     }  
  80.   
  81.     private void query() throws Exception {  
  82.         // 检索  
  83.         QueryBuilder qb = QueryBuilders.matchPhraseQuery("name""张三的车");  
  84.         SearchResponse searchResponse = client.prepareSearch(index)  
  85.                 .setTypes(type).setQuery(qb).setFrom(0).setSize(12).execute()  
  86.                 .actionGet();  
  87.   
  88.         SearchHits hits = searchResponse.getHits();  
  89.         if (null == hits || hits.totalHits() == 0) {  
  90.             log.error("使用\"张三的车\"没有查询到任何结果!");  
  91.         } else {  
  92.             for (SearchHit hit : hits) {  
  93.                 String json = hit.getSourceAsString();  
  94.   
  95.                 Person newPerson = mapper.readValue(json, Person.class);  
  96.                 System.out.println("id\t\t" + newPerson.getId());  
  97.                 System.out.println("name\t\t" + newPerson.getName());  
  98.                 System.out.println("sex\t\t" + newPerson.getSex());  
  99.                 System.out.println("age\t\t" + newPerson.getAge());  
  100.                 System.out.println("isStudent\t\t" + newPerson.getIsStudent());  
  101.                 System.out  
  102.                         .println("+++++++++++++++++++++++++++++++++++++++++++++++++++");  
  103.             }  
  104.         }  
  105.     }  
  106. }  

        第一二种方案,要求你先知道要删除的索引的ID后,方可执行删除,这种方案的局限性较大。

        第三种方案则较简单,即根据条件删除匹配的索引,如下所示:

[java] view plain copy
  1. QueryBuilder qb = QueryBuilders.matchAllQuery();  
  2. client.prepareDeleteByQuery(indeices).setQuery(qb).execute();  
0 0
原创粉丝点击