Elasticsearch--java ApI(deleteById)亲测
deleteRecordById.java
package es; import java.util.HashMap; import java.util.Map; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.client.Client; import com.alibaba.fastjson.JSON; public class deleteRecordById { public String deleteRecordById(String index, String type, String id) { ClientHelper clientHelper = new ClientHelper(); Map<String, Object> returnMsg = new HashMap<String, Object>(); Integer num = 0; Client client = clientHelper.getClient(); DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet(); num = response.getShardInfo().getSuccessful(); boolean success = num > 0 ? true : false; returnMsg.put("success", success); return JSON.toJSONString(returnMsg); } }
测试test
/** * 测试删除byId */ @Test public static void delete(){ deleteRecordById recordById = new deleteRecordById(); String index = "es_test4"; String type = "book"; String id = "8276e3679d4b4fca94ed806092a373c0"; String msg = recordById.deleteRecordById(index, type, id); System.out.println(msg); }
ClientHelper.java
package es; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.plugin.deletebyquery.DeleteByQueryPlugin; import org.elasticsearch.shield.ShieldPlugin; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.web.bind.annotation.InitBinder; /** * Es服务器初始化类 * @author Administrator * */ public class ClientHelper { //log4j private static Logger logger = LoggerFactory.getLogger(ClientHelper.class); //存放es集群名称以及嗅探设置 private Settings setting; //es客户端存放集合 private Map<String, Client> clientMap = new ConcurrentHashMap<String, Client>(); //ip端口号集合 private Map<String, Integer> ips = new HashMap<String, Integer>(); //默认集群名称 private String clusterName = "elasticsearch"; public ClientHelper(){ init(); } private static class ClientHolder{ private static final ClientHelper INSTANCE = new ClientHelper(); } public static final ClientHelper getInstance(){ return ClientHolder.INSTANCE; } /** * 得到客户端连接 * @return */ public Client getClient() { return getClient(clusterName); } /** * 得到客户端连接 * @return */ public Client getClient(String clusterName) { Client c = clientMap.get(clusterName); return c; } /** * 初始化EsClient */ public void init(){ Resource resource = new ClassPathResource("config/config.properties"); try { Properties properties = PropertiesLoaderUtils.loadProperties(resource); String host = properties.getProperty("host"); String port = properties.getProperty("port"); clusterName = properties.getProperty("clusterName"); String [] hosts = host.split(";"); String [] ports = port.split(";"); for(int i=0;i< hosts.length;i++){ int portNo = Integer.valueOf(ports[i].trim()); ips.put(hosts[i].trim(), portNo); } } catch (Exception e) { logger.error(e.getMessage()); e.printStackTrace(); } setting = Settings.settingsBuilder() .put("client.transport.sniff",true) .put("cluster.name",clusterName) .build(); addClient(setting, getAllAddress(ips)); } /** * 查询所有ES服务器地址 * * @return */ public List<InetSocketTransportAddress> getAllAddress(Map<String, Integer>ips){ List<InetSocketTransportAddress> addressList = new ArrayList<InetSocketTransportAddress>(); for(String ip:ips.keySet()){ try { addressList.add(new InetSocketTransportAddress(InetAddress.getByName(ip),ips.get(ip))); } catch (UnknownHostException e) { logger.error(e.getMessage()); e.printStackTrace(); } } return addressList; } /** * 往集群中增加ES服务器 * @param setting 设置 * @param transportAddress 传输地址 */ public void addClient(Settings setting, List<InetSocketTransportAddress> transportAddress) { Client client = new TransportClient.Builder().addPlugin(ShieldPlugin.class).addPlugin(DeleteByQueryPlugin.class).settings(setting).build() .addTransportAddresses(transportAddress .toArray(new InetSocketTransportAddress[transportAddress.size()])); clientMap.put(setting.get("cluster.name"), client); } }
划船不用桨、杨帆不等风、一生全靠浪