Sunday, November 30, 2014

Elasticsearch Java example (Elasticsearch Java API Basic example)

Simple Code to get started with Elasticsearch Java APIs.



You may need to import following two jar files to run this example. You may find these jars inside your elastic search folder (installation dir) or you may download it from here 

Java Elasticsearch Example-



import java.util.LinkedHashMap;
import java.util.Map;

import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

public class ElasticSearchExample {

private static Client client;
private String indexName;
static String documentType = "json";
TransportClient transportClient;

public static void main(String[] str) throws Exception {

ElasticSearchExample elasticSearchService;

// create client
elasticSearchService = new ElasticSearchExample("localhost", 9300);

// create index
elasticSearchService.createIndex("test");

// add elements
LinkedHashMap jsonMap = new LinkedHashMap();
// add 1st element
jsonMap = new LinkedHashMap();
jsonMap.put("Content-Type", "application/octet-stream");
jsonMap.put("resourceName", "GFED3_aromatics_1997-2010_8683.nc");
jsonMap.put("user", "user_1");
elasticSearchService.addElement("1", jsonMap);
// add 2nd element
jsonMap = new LinkedHashMap();
jsonMap.put("user", "user_2");
jsonMap.put("Content-Type", "application/octet-stream");
jsonMap.put("resourceName", "GFED3_monoterpenes_1997-2010_8683.nc");
elasticSearchService.addElement("2", jsonMap);

// get element by document ID
elasticSearchService.getElement("1");

}

public ElasticSearchExample(String server, int port) {
transportClient = new TransportClient();
client = transportClient
.addTransportAddress(new InetSocketTransportAddress(server,
port));
}

public void createIndex(String indexName) {
// Create Index and set settings and mappings
this.indexName = indexName;
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin()
.indices().prepareCreate(indexName);
createIndexRequestBuilder.execute().actionGet();
}

public void addElement(String documentId,
LinkedHashMap jsonMap) throws Exception {
IndexRequestBuilder indexRequestBuilder = client.prepareIndex(
indexName, documentType, documentId);
indexRequestBuilder.setSource(jsonMap);
IndexResponse response = indexRequestBuilder.execute().actionGet();
System.out.println("Element added-" + jsonMap);
}

public void getElement(String documentId) {
GetRequestBuilder getRequestBuilder = client.prepareGet(indexName,
documentType, documentId);
GetResponse response = getRequestBuilder.execute().actionGet();
Map map = response.getSourceAsMap();
System.out.print("getElementById -");
for (String key : map.keySet()) {
Object object = map.get(key);
System.out.print("{id:" + documentId + ", name:" + key + ", value:"
+ object + "}, ");
}
}

@Override
protected void finalize() throws Throwable {
transportClient.close();
super.finalize();
}


}



Source Code and jar files - here 


1 comment:

Unknown said...

Thanks Rupesh Really helpful this