Monday 23 November 2015

Elasticsearch: Java: Delete API

By using DELETE API we can delete JSON document from a specific index, type based on its id.

The following example deletes the JSON document from an index called organization, under a type called employee, with id valued 1.

DeleteResponse response = client.prepareDelete("organization", "employee", "1").execute().actionGet();

Following procedure explains step-by-step procedure to insert, query, and delete a document from Elasticsearch.
Step 1: Define model class Employee.
package com.self_learn.model;

import java.util.ArrayList;
import java.util.List;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@EqualsAndHashCode()
@ToString
public class Employee {
 @Getter @Setter private String age;
 @Getter @Setter private String firstName;
 @Getter @Setter private String lastName;
 @Getter @Setter private List<String> hobbies = new ArrayList<>();
}


Step 2: Define TransportClientUtil class to initialize Client instance.

package com.self_learn.util;

import static com.self_learn.util.IPUtil.isValidHosts;
import static com.self_learn.util.IPUtil.isValidPorts;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import com.google.common.base.Preconditions;

public class TransportClientUtil {
 private static Map<Map<String, Integer>, Client> localMap = new HashMap<>();

 /**
  * Take machine name and port addresses as map and return transport client.
  * Key is host name, value is port number
  * 
  * @throws UnknownHostException
  */
 public static Client getTransportClient(String clusterName,
   Map<String, Integer> map) throws UnknownHostException {

  Preconditions.checkNotNull(clusterName,
    "clusterName shouldn't be empty");
  Preconditions.checkNotNull(map, "Map shouldn't be empty");

  if (localMap.containsKey(map))
   return localMap.get(map);

  Preconditions.checkState(isValidHostPorts(map),
    "Map contains invalid host (or) port");

  Settings settings = ImmutableSettings.settingsBuilder()
    .put("cluster.name", clusterName)
    .put("client.transport.sniff", true).build();

  TransportClient client = new TransportClient(settings);

  InetSocketTransportAddress addresses[] = getInetSocketTransportAddresses(map);
  client.addTransportAddresses(addresses);
  localMap.put(map, client);
  return client;
 }

 /**
  * @param map
  * @return true, if all the entries in map are valid host, ports. Else
  *         false.
  */
 private static boolean isValidHostPorts(Map<String, Integer> map) {
  Set<String> hostNames = map.keySet();
  Set<Integer> ports = new HashSet<>(map.values());

  if (!isValidHosts(hostNames.toArray(new String[hostNames.size()])))
   return false;

  if (!isValidPorts(ports.toArray(new Integer[ports.size()])))
   return false;

  return true;
 }

 private static InetSocketTransportAddress[] getInetSocketTransportAddresses(
   Map<String, Integer> map) throws UnknownHostException {
  InetSocketTransportAddress addresses[] = new InetSocketTransportAddress[map
    .size()];
  int count = 0;

  Set<String> keys = map.keySet();
  for (String key : keys) {
   InetAddress addr = InetAddress.getByName(key);
   InetSocketTransportAddress address = new InetSocketTransportAddress(
     addr, map.get(key));
   addresses[count] = address;
  }

  return addresses;
 }

 /**
  * Get transport client for localhost.
  * 
  * @param clusterName
  * @param port
  * @return
  * @throws UnknownHostException
  */
 public static Client getLocalTransportClient(String clusterName, int port)
   throws UnknownHostException {
  Settings settings = ImmutableSettings.settingsBuilder()
    .put("cluster.name", clusterName)
    .put("client.transport.sniff", true).build();

  TransportClient client = new TransportClient(settings);
  InetAddress addr = InetAddress.getByName("127.0.0.1");
  InetSocketTransportAddress address = new InetSocketTransportAddress(
    addr, port);

  client.addTransportAddress(address);
  return client;
 }

}


IPUtil class is used to validate hostnames, ports.

package com.self_learn.util;

import org.apache.commons.validator.routines.InetAddressValidator;

import com.google.common.base.Preconditions;

/**
 * Validate IPaddresses, ports
 * 
 * @author harikrishna_gurram
 */
public class IPUtil {
 private static InetAddressValidator inetAddressValidator = InetAddressValidator
   .getInstance();

 /**
  * @param ipAddress
  * @return true if ip address is valid, else false
  */
 public static boolean isValidIPAddress(String ipAddress) {
  Preconditions.checkNotNull(ipAddress, "IP address should not be null");
  return inetAddressValidator.isValid(ipAddress);
 }

 /**
  * @param port
  *            : Port number
  * @return true if port number is valid, else false
  */
 public static boolean isValidPort(int port) {
  if (port > 0 && port < 65536)
   return true;
  return false;
 }

 /**
  * @param hostNames
  * @return true if all the elements of array represents valid hosnames, else
  *         false.
  */
 public static boolean isValidHosts(String[] hostNames) {
  Preconditions.checkNotNull(hostNames, "Host names shouldn't be empty");
  for (String hostName : hostNames) {
   if (!isValidIPAddress(hostName)) {
    return false;
   }
  }
  return true;
 }

 /**
  * 
  * @param ports
  * @return true if all the elements of array represents valid ports, else
  *         false.
  */
 public static boolean isValidPorts(Integer[] ports) {
  Preconditions.checkNotNull(ports, "ports shouldn't be empty");
  for (int port : ports) {
   if (!isValidPort(port)) {
    return false;
   }
  }
  return true;
 }

}


Step 3: Define JSONUtil, to convert object into json, json to object.

package com.self_learn.util;

import org.elasticsearch.common.base.Preconditions;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * Convert object to json string.
 * 
 * @author harikrishna_gurram
 */
public class JSONUtil {
 private static Gson gson = new Gson();
 private static Gson prettyGson = new GsonBuilder().setPrettyPrinting()
   .create();

 /**
  * @param obj
  * @return json string of this object.
  */
 public static String getJson(Object obj) {
  Preconditions.checkNotNull(obj, "obj shouldn't be null");
  return gson.toJson(obj);
 }

 /**
  * @param obj
  * @return json string of this object (Pretty json).
  */
 public static String getPrettyJson(Object obj) {
  Preconditions.checkNotNull(obj, "obj shouldn't be null");
  return prettyGson.toJson(obj);
 }

 /**
  * Convert given json string to object.
  * 
  * @param json
  * @param obj
  * @return an object by populating properties with json data.
  */
 public static <T> T getObject(String json, Class<T> clazz) {
  Preconditions.checkNotNull(json, "json data shouldn't be null");
  Preconditions.checkNotNull(clazz, "clazz shouldn't be null");
  return gson.fromJson(json, clazz);
 }
}


Step 4: Define IndexUtil, to insert a document into Elasticsearch.

package com.self_learn.util;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;

import com.google.common.base.Preconditions;

/**
 * Provides utility methods to store data into Elasticsearch.
 * 
 * @author harikrishna_gurram
 */
public class IndexUtil {

 /**
  * Index given document.
  * 
  * @param client
  *            : Client used to index data
  * @param _index
  *            : Document is stored in this index
  * @param _type
  *            : Document stored in this type
  * @param _id
  *            : Specifies _id of the document
  * @param document
  *            : Represents body of the document
  * @return {@link IndexResponse}
  */
 public static IndexResponse indexData(Client client, String _index,
   String _type, String _id, String document) {
  Preconditions.checkNotNull(client, "client should not be null");
  Preconditions.checkNotNull(_index, "_index should not be null");
  Preconditions.checkNotNull(_type, "_type should not be null");
  Preconditions.checkNotNull(_id, "_id should not be null");
  Preconditions.checkNotNull(document, "data should not be null");

  IndexResponse response = client.prepareIndex(_index, _type, _id)
    .setSource(document).execute().actionGet();
  return response;
 }

 /**
  * Index given object.
  * 
  * @param client
  *            : Client used to index data
  * @param _index
  *            : Document is stored in this index
  * @param _type
  *            : Document stored in this type
  * @param _id
  *            : Specifies _id of the document
  * @param obj
  *            : Object to index
  * @return {@link IndexResponse}
  */
 public static IndexResponse indexData(Client client, String _index,
   String _type, String _id, Object obj) {
  Preconditions.checkNotNull(obj, "data should not be null");
  return indexData(client, _index, _type, _id, JSONUtil.getJson(obj));
 }

}


Step 5: Define SearchUtil, used to query Elasticsearch.

package com.self_learn.util;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;

import com.google.common.base.Preconditions;

/**
 * Provide various utility methods to query Elasticsearch.
 * 
 * @author harikrishna_gurram
 */
public class SearchUtil {

 /**
  * Returns the document by id (Takes _index, _type, _id as input).
  * 
  * @param client
  * @param _index
  * @param _type
  * @param _id
  * @return the document by id
  */
 public static GetResponse getDocumentById(Client client, String _index,
   String _type, String _id) {
  Preconditions.checkNotNull(client, "client should not be null");
  Preconditions.checkNotNull(_index, "_index should not be null");
  Preconditions.checkNotNull(_type, "_type should not be null");
  Preconditions.checkNotNull(_id, "_id should not be null");

  GetResponse response = client.prepareGet(_index, _type, _id).execute()
    .actionGet();
  return response;
 }

}


Step 6: Define DeleteUtil, to delete a document from Elasticsearch.

package com.self_learn.util;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.Client;

import com.google.common.base.Preconditions;

/**
 * Provide utility methods to delete document from Elasticsearch.
 * 
 * @author harikrishna_gurram
 */
public class DeleteUtil {

 /**
  * Removes the docuemnt and return DeleteResponse.
  * @param client
  * @param _index
  * @param _type
  * @param _id
  * @return DeleteResponse
  */
 public static DeleteResponse deleteDocument(Client client, String _index,
   String _type, String _id) {
  Preconditions.checkNotNull(client, "client should not be null");
  Preconditions.checkNotNull(_index, "_index should not be null");
  Preconditions.checkNotNull(_type, "_type should not be null");
  Preconditions.checkNotNull(_id, "_id should not be null");

  DeleteResponse response = client.prepareDelete(_index, _type, _id)
    .execute().actionGet();
  return response;
 }
}


Step 7: Define ResponseUtil, used to print Response in string format.

package com.self_learn.util;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;

import com.google.common.base.Preconditions;

/**
 * Utility class to return respone in string format.
 * 
 * @author harikrishna_gurram
 */
public class ResponseUtil {

 /**
  * @param response
  * @return string representation of {@link IndexResponse}
  */
 public static String getResponseInfo(IndexResponse response) {
  Preconditions.checkNotNull(response, "response shouldn't be null");
  String _index = response.getIndex();
  String _type = response.getType();
  String _id = response.getId();
  long _version = response.getVersion();
  boolean created = response.isCreated();

  StringBuilder builder = new StringBuilder();
  return builder.append("_index: ").append(_index).append("\n")
    .append("_type: ").append(_type).append("\n").append("_id: ")
    .append(_id).append("\n").append("_version: ").append(_version)
    .append("\n").append("created: ").append(created).toString();
 }

 /**
  * @param response
  * @return string representation of {@link GetResponse}
  */
 public static String getResponseInfo(GetResponse response) {
  Preconditions.checkNotNull(response, "response should not be null");

  String _index = response.getIndex();
  String _type = response.getType();
  String _id = response.getId();
  long _version = response.getVersion();
  String source = response.getSourceAsString();

  StringBuilder builder = new StringBuilder();
  return builder.append("_index: ").append(_index).append("\n")
    .append("_type: ").append(_type).append("\n").append("_id: ")
    .append(_id).append("\n").append("_version: ").append(_version)
    .append("\n").append("_source: ").append(source).toString();
 }

 /**
  * Returns source of the result as string
  * 
  * @param response
  * @return
  */
 public static String getSource(GetResponse response) {
  Preconditions.checkNotNull(response, "response shouldn't be null");
  return response.getSourceAsString();
 }

 /**
  * @param response
  * @return string representation of {@link DeleteResponse}
  */
 public static String getResponseInfo(DeleteResponse response) {
  Preconditions.checkNotNull(response, "response shouldn't be null");
  String _index = response.getIndex();
  String _type = response.getType();
  String _id = response.getId();
  long _version = response.getVersion();
  boolean found = response.isFound();

  StringBuilder builder = new StringBuilder();
  return builder.append("_index: ").append(_index).append("\n")
    .append("_type: ").append(_type).append("\n").append("_id: ")
    .append(_id).append("\n").append("_version: ").append(_version)
    .append("\n").append("found: ").append(found).toString();
 }
}


Step 8: Main class will demonstrate complete application.

package com.self_learn.test;

import java.net.UnknownHostException;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.Client;

import com.self_learn.model.Employee;
import com.self_learn.util.DeleteUtil;
import com.self_learn.util.IndexUtil;
import com.self_learn.util.ResponseUtil;
import com.self_learn.util.SearchUtil;
import com.self_learn.util.TransportClientUtil;

public class Main {
 private static String clusterName = "my_cluster_1";
 private static String _index = "organization";
 private static String _type = "employee";

 private static void printStars() {
  System.out.println("*************************************************");
 }

 public static void main(String args[]) throws UnknownHostException {
  /* Get client instance for cluster */
  Client client = TransportClientUtil.getLocalTransportClient(
    clusterName, 9300);

  /* Prepare model object */
  Employee emp = new Employee();
  emp.setAge("27");
  emp.setFirstName("PTR");
  emp.setLastName("Nayan");
  emp.getHobbies().add("Tattoos");
  emp.getHobbies().add("People Watching");
  emp.getHobbies().add("Dagger Collecting");
  emp.getHobbies().add("Confusing People");

  /* Write object into Elasticsearch */
  System.out.println("Writing " + emp + " to Elasticsearch");
  IndexUtil.indexData(client, _index, _type, "1", emp);

  /* Query for the object with id 1 */
  System.out.println("Reading data from Elasticsearch");
  GetResponse response = SearchUtil.getDocumentById(client, _index,
    _type, "1");
  System.out.println(ResponseUtil.getResponseInfo(response));
  printStars();

  /* Delete document */
  System.out.println("Deleting document from Elasticsearch");
  DeleteResponse delResponse = DeleteUtil.deleteDocument(client, _index,
    _type, "1");
  System.out.println(ResponseUtil.getResponseInfo(delResponse));
  printStars();

  /* Query for the object with id 1 */
  System.out.println("Reading data from Elasticsearch");
  response = SearchUtil.getDocumentById(client, _index, _type, "1");
  System.out.println(ResponseUtil.getResponseInfo(response));

  client.close();
 }
}


When you ran Main.java, you will get following kind of output.

Sep 09, 2015 3:42:54 PM org.elasticsearch.plugins.PluginsService <init>
INFO: [Monsoon] loaded [], sites []
Writing Employee(age=27, firstName=PTR, lastName=Nayan, hobbies=[Tattoos, People Watching, Dagger Collecting, Confusing People]) to Elasticsearch
Reading data from Elasticsearch
_index: organization
_type: employee
_id: 1
_version: 1
_source: {"age":"27","firstName":"PTR","lastName":"Nayan","hobbies":["Tattoos","People Watching","Dagger Collecting","Confusing People"]}
*************************************************
Deleting document from Elasticsearch
_index: organization
_type: employee
_id: 1
_version: 2
found: true
*************************************************
Reading data from Elasticsearch
_index: organization
_type: employee
_id: 1
_version: -1
_source: null






Prevoius                                                 Next                                                 Home

No comments:

Post a Comment