

Table of Contents
1. Overview
Right now in the market, there are two most popular search engines available one is Apache Solr and the second one is Elastic Search. Both these search engines are based on Apache Lucene.
In this article, we will discuss how to index documents in Elastic Search using Elastic Search JAVA Transport Client
with an example.
2. Development Environment
Elastic Search: 6.2.4
Java: 1.8.0_65
IDE: IntelliJ Idea
Build Tool: Maven
3. Steps to Index Document using Transport Client
Now we will discuss how to index document using Elastic Search Transport client with details explanations.
Step 1: Create Maven Project
Step 2: Add elastic-search-transport-client dependency in a project.
Elastic Search team provides client APIs to communicate with the elastic search for Java, C# .NET, Python etc… In this article, we will discuss Java client of Elastic Search. Add below dependency in your project.
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.2.4</version> </dependency>
Step 3: Initialize transport client
TransportClient class is used to communicate with Elastic Search cluster. It connects ES cluster using transport module. Here is the sample code to the initialize client.
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new TransportAddress(InetAddress.getByName("{HostName_1}"), {PORT})); //1st ES Node host and port .addTransportAddress(new TransportAddress(InetAddress.getByName("{HostName_2"), {PORT})); //2nd ES Node host and port
Step 4: create an index and Index type and prepare JSON
The next step is to create Index and Index type in Elastic Search. Transport Client automatically index and index type, if it does not exists when you submit any document for indexing.
Step 5: Prepare JSON Document
As per the Elastic Search Index API Documentations, there are several ways to generate JSON document, out of these options in our example we have used JSON Builder
to construct the document. Refer below syntax to construct JSON Document.
XContentBuilder builder = jsonBuilder() .startObject() .field("{FIELD_NAME_1}", "{FIELD_VALUE_1}") .field("{FIELD_NAME_2}", "{FIELD_VALUE_2}") .field("{FIELD_NAME_3}", "{FIELD_VALUE_3}") .endObject()
4. Example
Here is the complete example of index single document using Elastic Search Transport Client.
Project Structure

Elastic Search Index Document Project Structure
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>ElasticSearchIndexDocumentExample</groupId> <artifactId>ElasticSearchIndexDocumentExample</artifactId> <version>1.0-SNAPSHOT</version> <description>Elastic Search Index Document Example</description> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.2.4</version> </dependency> </dependencies> </project>
Document Pojo
package javadeveloperzone.pojo; import java.util.Date; /** * Created by JavaDeveloperZone on 5/13/2018. */ public class Document { int docId; String docTitle; int page; String docType; Date modifiedDate; public int getDocId() { return docId; } public void setDocId(int docId) { this.docId = docId; } public String getDocTitle() { return docTitle; } public void setDocTitle(String docTitle) { this.docTitle = docTitle; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public String getDocType() { return docType; } public void setDocType(String docType) { this.docType = docType; } public Date getModifiedDate() { return modifiedDate; } public void setModifiedDate(Date modifiedDate) { this.modifiedDate = modifiedDate; } }
ESDocumentIndexing
package javadeveloperzone; import javadeveloperzone.pojo.Document; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.transport.client.PreBuiltTransportClient; import java.net.InetAddress; import java.util.Date; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; /** * Created by JavaDeveloperZone on 6/16/2018. */ public class ESDocumentIndexingMain { TransportClient client = null; public static void main(String[] args) { ESDocumentIndexingMain esExample = new ESDocumentIndexingMain(); try { esExample.initEStransportClinet(); //init transport client esExample.indexDocument(); //index one document esExample.refreshIndices(); //refresh indices esExample.search(); //search indexed document }catch (Exception e){ e.printStackTrace(); }finally { esExample.closeTransportClient(); //close transport client } } /* Method used to init Elastic Search Transprt client, Return true if it is succesfully intialized otherwise false */ public boolean initEStransportClinet() { try { // un-command this, if you have multiple node client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9300)); return true; } catch (Exception ex) { //log.error("Exception occurred while getting Client : " + ex, ex); ex.printStackTrace(); return false; } } private boolean indexDocument(Document document){ try { IndexResponse response = client.prepareIndex("document", "document", String.valueOf(document.getDocId())) .setSource(jsonBuilder() .startObject() .field("docTitle", document.getDocTitle()) .field("docPage", document.getPage()) .field("docType", document.getDocType()) .field("docModifiedDate",document.getModifiedDate()) .endObject() ) .get(); if(response!=null){ System.out.println(response.toString()); }else{ return false; } return true; }catch (Exception e){ return false; } } public void indexDocument(){ Document document = new Document(); document.setDocId(1); document.setDocTitle("Elastic Search Indexing Example"); document.setDocType("PDF"); document.setModifiedDate(new Date()); document.setPage(2); boolean isIndexed = this.indexDocument(document); if(isIndexed){ //further actions like change index status in DB, // send notification etc.. } } public void refreshIndices(){ client.admin().indices() .prepareRefresh("document") .get(); //Refresh before search, so you will get latest indices result } public void search(){ SearchResponse response = client.prepareSearch("document") .setTypes("document") .get(); //MatchAllDocQuery System.out.println("Total Hits : "+response.getHits().getTotalHits()); System.out.println(response); } public void closeTransportClient(){ if(client!=null){ client.close(); } } }
Output
IndexResponse[index=document,type=document,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={"total":2,"successful":1,"failed":0}] Total Hits : 1 { "took":609, "timed_out":false, "_shards":{ "total":5, "successful":5, "skipped":0, "failed":0 }, "_clusters":{ "total":0, "successful":0, "skipped":0 }, "hits":{ "total":1, "max_score":1.0, "hits":[ { "_index":"document", "_type":"document", "_id":"1", "_score":1.0, "_source":{ "docTitle":"Elastic Search Indexing Example", "docPage":2, "docType":"PDF", "docModifiedDate":"2018-06-16T14:27:51.979Z" } } ] } }
5. Conclusion
In this article, we have discussed how to configure Elastic Search Transport Client in our project and how to index single document using JSON Builder
, refresh Elastic Search indices and get documents count using Match All Doc
query.
6. References
Refer below links for more details:
7. Source Code
Elastic Search Index Document Example
You can also download the source code of Elastic Search Index Document Example and other useful examples from our git repository.