Sunday, December 27, 2015

Elastic Search 2.x sample CRUD code

What is ElasticSearch?

Elasticsearch is an open-source, restful, distributed, search engine built on top of apache-lucene, Lucene is arguably the most advanced, high-performance, and fully featured search engine library in existence today—both open source and proprietary.
Elasticsearch is also written in Java and uses Lucene internally for all of its indexing and searching, but it aims to make full-text search easy by hiding the complexities of Lucene behind a simple, coherent, RESTful API.

Basic Concept and terminologies:

1.Near Realtime (NRT) Elasticsearch is a near real time search platform. What this means is there is a slight latency (normally one second) from the time you index a document until the time it becomes searchable.
2.Cluster A cluster is a collection of one or more nodes (servers) that together holds your entire data and provides federated indexing and search capabilities across all nodes. default cluster name will be "elasticsearch".
3.Node A node is a single server that is part of your cluster, stores your data, and participates in the cluster’s indexing and search capabilities.
4.Index An index is a collection of documents that have somewhat similar characteristics i.e like database.
5.Type Within an index, you can define one or more types. A type is a logical category/partition of your index and defined for documents that have a set of common fields. i.e. like table in relational database. a type
6.Document A document is a basic unit of information that can be indexed. For example

Below image will show how we can co-relate the relational database with elastic index which will make easy to understand the elastic terms and api.
In Elasticsearch, a document belongs to a type, and those types live inside an index. You can draw some (rough) parallels to a traditional relational database:

Relational DB ⇒ Databases ⇒ Tables ⇒ Rows ⇒ Columns Elasticsearch ⇒ Indices ⇒ Types ⇒ Documents ⇒ Fields

Development: Maven library dependency:

Client: using java client we can performe operations on elastic search cluster/node.
1.Perform standard index, get, delete and search operations on an existing cluster
2.Perform administrative tasks on a running cluster
3.Start full nodes when you want to run Elasticsearch embedded in your own application or when you want to launch unit or integration tests

Two types of client to get the client connection with cluster to perform the operations.
1. Node Client.
2. TransportClient.
Node Client: Instantiating a node based client is the simplest way to get a Client that can execute operations against elasticsearch. TransportClient: The TransportClient connects remotely to an Elasticsearch cluster using the transport module. It does not join the cluster, but simply gets one or more initial transport addresses and communicates with them. sample elastic search crud sample code: Node Client:
Node node  = NodeBuilder.nodeBuilder().clusterName("yourclustername").node();
Client client = node.client();
Settings settings = Settings.settingsBuilder()
                    .put(ElasticConstants.CLUSTER_NAME, cluster).build();
TransportClient transportClient = TransportClient.builder().settings(settings).build().
                    addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
Creat Index: We can create the IndexRequest or using XContentBuilder we can populate the request to store in the index.
 XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
 Map<String, Object> data = new HashMap<String, Object>();
 data.put("FirstName", "Uttesh");
 data.put("LastName", "Kumar T.H.");;
public IndexResponse createIndex(String index, String type, String id, XContentBuilder jsonData) {
    IndexResponse response = null;
    try {
        response = ElasticSearchUtil.getClient().prepareIndex(index, type, id)
        return response;
    } catch (Exception e) {
        logger.error("createIndex", e);
    return null;
Find Document By Index:
public void findDocumentByIndex() {
        GetResponse response = findDocumentByIndex("users", "user", "1");
        Map<String, Object> source = response.getSource();
        System.out.println("Index: " + response.getIndex());
        System.out.println("Type: " + response.getType());
        System.out.println("Id: " + response.getId());
        System.out.println("Version: " + response.getVersion());
        System.out.println("getFields: " + response.getFields());

public GetResponse findDocumentByIndex(String index, String type, String id) {
        try {
            GetResponse getResponse = ElasticSearchUtil.getClient().prepareGet(index, type, id).get();
            return getResponse;
        } catch (Exception e) {
            logger.error("", e);
        return null;

Find Document By Value
public void findDocumentByValue() {
        SearchResponse response = findDocument("users", "user", "LastName", "Kumar T.H.");
        SearchHit[] results = response.getHits().getHits();
        System.out.println("Current results: " + results.length);
        for (SearchHit hit : results) {
            System.out.println("Index: " + hit.getIndex());
            System.out.println("Type: " + hit.getType());
            System.out.println("Id: " + hit.getId());
            System.out.println("Version: " + hit.getVersion());
            Map<String, Object> result = hit.getSource();
        Assert.assertSame(response.getHits().totalHits() > 0, true);

    public SearchResponse findDocument(String index, String type, String field, String value) {
        try {
            QueryBuilder queryBuilder = new MatchQueryBuilder(field, value);
            SearchResponse response = ElasticSearchUtil.getClient().prepareSearch(index)
            SearchHit[] results = response.getHits().getHits();
            return response;
        } catch (Exception e) {
            logger.error("", e);
        return null;
Update Index
public void UpdateDocument() throws IOException {
    XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("FirstName", "Uttesh Kumar");
    data.put("LastName", "TEST");;
    UpdateResponse updateResponse = updateIndex("users", "user", "1", jsonBuilder);

public UpdateResponse updateIndex(String index, String type, String id, XContentBuilder jsonData) {
    UpdateResponse response = null;
    try {
        System.out.println("updateIndex ");
        response = ElasticSearchUtil.getClient().prepareUpdate(index, type, id)
        System.out.println("response " + response);
        return response;
    } catch (Exception e) {
        logger.error("UpdateIndex", e);
    return null;
Remove Index:
public void RemoveDocument() throws IOException {
    DeleteResponse deleteResponse = elastiSearchService.removeDocument("users", "user", "1");

public DeleteResponse removeDocument(String index, String type, String id) {
        DeleteResponse response = null;
        try {
            response = ElasticSearchUtil.getClient().prepareDelete(index, type, id).execute().actionGet();
            return response;
        } catch (Exception e) {
            logger.error("RemoveIndex", e);
        return null;
Full sample code is available at guthub Download full code

1 comment:

  1. ElasticSearch + Kibana database

    Elasticsearch is a free, open-source search and analytics engine based on the Apache Lucene library. It’s the most popular search engine and has been available since 2010. It’s developed in Java, supporting clients in many different languages, such as PHP, Python, C#, and Ruby.

    Kibana is an free and open frontend application that sits on top of the Elastic Stack, providing search and data visualization capabilities for data indexed in Elasticsearch. Commonly known as the charting tool for the Elastic Stack (previously referred to as the ELK Stack after Elasticsearch, Logstash, and Kibana), Kibana also acts as the user interface for monitoring, managing, and securing an Elastic Stack cluster — as well as the centralized hub for built-in solutions developed on the Elastic Stack. Developed in 2013 from within the Elasticsearch community, Kibana has grown to become the window into the Elastic Stack itself, offering a portal for users and companies.ElasticSearch + Kibana database

    our ElasticSearch + Kibana database expert skills & 24/7 dedicated support for stable clusters and achieve unparalleled performance and cost reduction along with stable data health. Experience our enterprise-class, worldwide support for Kibana integrated Elasticsearch & other stack.With years of direct, hands-on experience managing large Elasticsearch deployments, Genex efficiently supports data-analytics in real time. Take advantage of market-leading functionalities and Kibana visualizations on large data sets, with features including high available clusters, TLS, and RBAC