Elasticsearch的删除也是很灵活的,下次我再介绍,DeleteByQuery
的方式。今天就先介绍一个根据ID删除。上代码。
package com.sojson.core.elasticsearch.manager;
import org.elasticsearch.action.delete.DeleteResponse;
import com.sojson.common.model.SOBanggKey;
import com.sojson.core.elasticsearch.utils.ESTools;
public class DeleteManager {
/**
* 根据ID删除
* @param key
* @return
*/
public static int deleteSOBanggByKey(SOBanggKey key) {
//这里可以忽略,组装一个我业务逻辑的ID
String prefix = "%sx_x%s";
String id = String.format(prefix, key.getId(),key.getGid());
DeleteResponse result = ESTools.client.prepareDelete().setRefresh(true).setIndex(MappingManager.INDEX)
.setType(MappingManager.B_TYPE)
.setId(id)//设置ID
.setRefresh(true)//刷新
.execute().actionGet();
//是否查找并删除
boolean isfound = result.isFound();
return isfound?1:0;
}
}