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

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

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

/**
 * @author Geloin
 */
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;

/**
 * @author Geloin
 * 
 */
public class DeleteTest extends BaseTest {
	
	@Test
	public 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);

			// Get
			System.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
	 * 
	 * @author Geloin
	 * @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("未查询到任何结果!");
		}
	}
}

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

/**
 * @author Geloin
 */
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;

/**
 * @author Geloin
 * 
 */
public class BulkDeleteTest extends BaseTest {

	@Test
	public 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);

			// Get
			System.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("+++++++++++++++++++++++++++++++++++++++++++++++++++");
			}
		}
	}
}

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

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

		QueryBuilder qb = QueryBuilders.matchAllQuery();
		client.prepareDeleteByQuery(indeices).setQuery(qb).execute();


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值