# Insert data into Elastic Search using Java

**URL:** <https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264>\
**Category:** Elasticsearch\
**Created:** [June 4, 2013, 10:49pm UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264 "2013-06-04T22:49:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ganesh\_sainath](https://avatars.discourse-cdn.com/v4/letter/g/ea666f/32.png) [@ganesh\_sainath](https://discuss.elastic.co/u/ganesh_sainath)\
**Post date:** [June 4, 2013, 10:49pm UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/1 "2013-06-04T22:49:29Z")

</div>

Hi All,

I am new to Elastic Search.

What I need is to insert data in to Elastic search using Java. I am not  
able to find any sample program to have as base that would help me connect  
to Elastic search. It would be great if any of you can guide me to  
appropriate links or post some sample code for the same.

Thanks,  
Ganesh Sainath

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![tarang\_dawer](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@tarang\_dawer](https://discuss.elastic.co/u/tarang_dawer)\
**Post date:** [June 5, 2013, 2:37am UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/2 "2013-06-05T02:37:37Z")

</div>

Hi ganesh

First create a ES client in your java application.

```
                      private org.elasticsearch.client.Client client;

```

Initialize it, (google Elasticsearch Node client, and Transport Client)

```
                             Settings settings = ImmutableSettings
                            .settingsBuilder()
                            .put("cluster.name","elasticsearchcluster")
                            .build();

                          try{
                                 client=
                                       new TransportClient(settings)
                                      .addTransportAddress(new

```

InetSocketTransportAddress("192.168.145.144",9300));  
}catch(Exception e)  
{  
e.printStackTrace();  
}

Create a indexRequest, with IndexName , Document Type ( + ID-optional ) ,  
set it's source as the data that you want to index. Source can be either a  
map , or a string etc.  
IndexRequest indexRequest=new  
IndexRequest("indexName","documentType");

then execute the request by  
client.index(INDEXREQUEST).actionGet();

By exxecuting this request , if your index , "indexName" is not present ,  
elasticsearch will create it automatically , with some default mapping for  
the document "documentType" you have sent to it to be indexed.

You can batch multiple Reuests, using the Bulk Api , you can create/specify  
custom mappings for your document and control how individual fields are to  
be indexed , are they going to be stored or not etc, but the above part is  
just the basic level gist of getting you started.

Hoping that this helps.

Thanks  
Tarang Dawer

On Wed, Jun 5, 2013 at 4:19 AM, ganesh sainath [ganesh10888@gmail.com](mailto:ganesh10888@gmail.com)wrote:

> Hi All,
> 
> I am new to Elastic Search.
> 
> What I need is to insert data in to Elastic search using Java. I am not  
> able to find any sample program to have as base that would help me connect  
> to Elastic search. It would be great if any of you can guide me to  
> appropriate links or post some sample code for the same.
> 
> Thanks,  
> Ganesh Sainath
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![tarang\_dawer](https://avatars.discourse-cdn.com/v4/letter/t/c0e974/32.png) [@tarang\_dawer](https://discuss.elastic.co/u/tarang_dawer)\
**Post date:** [June 5, 2013, 2:42am UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/3 "2013-06-05T02:42:23Z")

</div>

Further you can refer to this link

[https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/00Tlhfs6kfc](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/00Tlhfs6kfc)

On Wed, Jun 5, 2013 at 8:07 AM, Tarang Dawer [tarang.dawer@gmail.com](mailto:tarang.dawer@gmail.com) wrote:

> Hi ganesh
> 
> First create a ES client in your java application.
> 
> ```
> private org.elasticsearch.client.Client client;
> 
> ```
> 
> Initialize it, (google Elasticsearch Node client, and Transport Client)
> 
> ```
> Settings settings = ImmutableSettings
> .settingsBuilder()
> .put("cluster.name
> 
> ```
> 
> ","elasticsearchcluster")  
> .build();
> 
> ```
> try{
> client=
> new TransportClient(settings)
> .addTransportAddress(new
> 
> ```
> 
> InetSocketTransportAddress("192.168.145.144",9300));  
> }catch(Exception e)  
> {  
> e.printStackTrace();  
> }
> 
> Create a indexRequest, with IndexName , Document Type ( + ID-optional ) ,  
> set it's source as the data that you want to index. Source can be either a  
> map , or a string etc.  
> IndexRequest indexRequest=new  
> IndexRequest("indexName","documentType");
> 
> then execute the request by  
> client.index(INDEXREQUEST).actionGet();
> 
> By exxecuting this request , if your index , "indexName" is not present ,  
> elasticsearch will create it automatically , with some default mapping for  
> the document "documentType" you have sent to it to be indexed.
> 
> You can batch multiple Reuests, using the Bulk Api , you can  
> create/specify custom mappings for your document and control how individual  
> fields are to be indexed , are they going to be stored or not etc, but the  
> above part is just the basic level gist of getting you started.
> 
> Hoping that this helps.
> 
> Thanks  
> Tarang Dawer
> 
> On Wed, Jun 5, 2013 at 4:19 AM, ganesh sainath [ganesh10888@gmail.com](mailto:ganesh10888@gmail.com)wrote:
> 
> > Hi All,
> > 
> > I am new to Elastic Search.
> > 
> > What I need is to insert data in to Elastic search using Java. I am not  
> > able to find any sample program to have as base that would help me connect  
> > to Elastic search. It would be great if any of you can guide me to  
> > appropriate links or post some sample code for the same.
> > 
> > Thanks,  
> > Ganesh Sainath
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 5, 2013, 6:15am UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/4 "2013-06-05T06:15:00Z")

</div>

Did you read the ref doc: [http://www.elasticsearch.org/guide/reference/java-api/](http://www.elasticsearch.org/guide/reference/java-api/)  
Is there anything unclear we should rewrite?

BTW, we will add in the near future a Java tutorial.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 5 juin 2013 à 00:49, ganesh sainath [ganesh10888@gmail.com](mailto:ganesh10888@gmail.com) a écrit :

Hi All,

I am new to Elastic Search.

What I need is to insert data in to Elastic search using Java. I am not able to find any sample program to have as base that would help me connect to Elastic search. It would be great if any of you can guide me to appropriate links or post some sample code for the same.

## Thanks, Ganesh Sainath

You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![ganesh\_sainath](https://avatars.discourse-cdn.com/v4/letter/g/ea666f/32.png) [@ganesh\_sainath](https://discuss.elastic.co/u/ganesh_sainath)\
**Post date:** [June 5, 2013, 4:39pm UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/5 "2013-06-05T16:39:44Z")

</div>

Thanks David for your reply.

I felt that a sample java program (In complete) should prove really helpful  
along with the code snippets posted in the link.

On Tuesday, 4 June 2013 23:15:00 UTC-7, David Pilato wrote:

> Did you read the ref doc:  
> [http://www.elasticsearch.org/guide/reference/java-api/](http://www.elasticsearch.org/guide/reference/java-api/)  
> Is there anything unclear we should rewrite?
> 
> BTW, we will add in the near future a Java tutorial.
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 5 juin 2013 à 00:49, ganesh sainath \<[ganes...@gmail.com](mailto:ganes...@gmail.com) \<javascript:\>\>  
> a écrit :
> 
> Hi All,
> 
> I am new to Elastic Search.
> 
> What I need is to insert data in to Elastic search using Java. I am not  
> able to find any sample program to have as base that would help me connect  
> to Elastic search. It would be great if any of you can guide me to  
> appropriate links or post some sample code for the same.
> 
> Thanks,  
> Ganesh Sainath
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![ganesh\_sainath](https://avatars.discourse-cdn.com/v4/letter/g/ea666f/32.png) [@ganesh\_sainath](https://discuss.elastic.co/u/ganesh_sainath)\
**Post date:** [June 5, 2013, 4:53pm UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/6 "2013-06-05T16:53:39Z")

</div>

Hi David, Tarang,

I am stuck at this point. I think you provide some head up on this. I am  
getting NoNodeAvailableException. I have attached the screenshot of the  
same.

What I inferred from the forums is that the cluster name can be entered  
wrongly because of which this exception can occur. I double checked by  
running the following command curl -XGET  
'[http://localhost:9200/\_cluster/health?pretty=true](http://localhost:9200/_cluster/health?pretty=true)'  
The java code is referring to the same as well. I am not sure where I am  
going wrong. I have attached the screenshot of the same.

import org.elasticsearch.common.settings.\*;  
//all necessary imports

class Test  
{  
public static void main(String args)  
{  
[//System.out.println](https://System.out.println)("Hello World");  
Client client = null;  
// = new TransportClient();  
Settings settings = ImmutableSettings  
.settingsBuilder()  
.put("cluster.name","elasticsearch")  
.build();  
try{  
client = new TransportClient(settings)  
.addTransportAddress(new  
InetSocketTransportAddress("localhost",9200));  
}  
catch(Exception e)  
{  
e.printStackTrace();  
}  
//InetSocketAddress inst = new  
InetSocketAddress(InetAddress.getByName("127.0.0.1"),9200);  
[//client.addTransportAddress](https://client.addTransportAddress)(inst);  
try{  
IndexResponse response = client.prepareIndex("twitter",  
"tweet", "1")  
.setSource(jsonBuilder()  
.startObject()  
.field("user", "kimchy")  
.field("postDate", new Date())  
.field("message", "trying out Elastic  
Search")  
.endObject()  
)  
.execute()  
.actionGet();  
}  
catch(Exception e)  
{  
e.printStackTrace();  
}  
}

On Tuesday, 4 June 2013 15:49:29 UTC-7, ganesh sainath wrote:

> Hi All,
> 
> I am new to Elastic Search.
> 
> What I need is to insert data in to Elastic search using Java. I am not  
> able to find any sample program to have as base that would help me connect  
> to Elastic search. It would be great if any of you can guide me to  
> appropriate links or post some sample code for the same.
> 
> Thanks,  
> Ganesh Sainath

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 5, 2013, 6:13pm UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/7 "2013-06-05T18:13:50Z")

</div>

Transport layer uses port 9300 to 9399.  
9200-9299 are REST ports.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 5 juin 2013 à 18:53, ganesh sainath [ganesh10888@gmail.com](mailto:ganesh10888@gmail.com) a écrit :

> Hi David, Tarang,
> 
> I am stuck at this point. I think you provide some head up on this. I am getting NoNodeAvailableException. I have attached the screenshot of the same.
> 
> What I inferred from the forums is that the cluster name can be entered wrongly because of which this exception can occur. I double checked by running the following command curl -XGET '[http://localhost:9200/\_cluster/health?pretty=true](http://localhost:9200/_cluster/health?pretty=true)'  
> The java code is referring to the same as well. I am not sure where I am going wrong. I have attached the screenshot of the same.
> 
> import org.elasticsearch.common.settings.\*;  
> //all necessary imports
> 
> class Test  
> {  
> public static void main(String args)  
> {  
> [//System.out.println](https://System.out.println)("Hello World");  
> Client client = null;  
> // = new TransportClient();  
> Settings settings = ImmutableSettings  
> .settingsBuilder()  
> .put("cluster.name","elasticsearch")  
> .build();  
> try{  
> client = new TransportClient(settings)  
> .addTransportAddress(new InetSocketTransportAddress("localhost",9200));  
> }  
> catch(Exception e)  
> {  
> e.printStackTrace();  
> }  
> //InetSocketAddress inst = new InetSocketAddress(InetAddress.getByName("127.0.0.1"),9200);  
> [//client.addTransportAddress](https://client.addTransportAddress)(inst);  
> try{  
> IndexResponse response = client.prepareIndex("twitter", "tweet", "1")  
> .setSource(jsonBuilder()  
> .startObject()  
> .field("user", "kimchy")  
> .field("postDate", new Date())  
> .field("message", "trying out Elastic Search")  
> .endObject()  
> )  
> .execute()  
> .actionGet();  
> }  
> catch(Exception e)  
> {  
> e.printStackTrace();  
> }  
> }
> 
> On Tuesday, 4 June 2013 15:49:29 UTC-7, ganesh sainath wrote:
> 
> > Hi All,
> > 
> > I am new to Elastic Search.
> > 
> > What I need is to insert data in to Elastic search using Java. I am not able to find any sample program to have as base that would help me connect to Elastic search. It would be great if any of you can guide me to appropriate links or post some sample code for the same.
> > 
> > Thanks,  
> > Ganesh Sainath
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![ganesh\_sainath](https://avatars.discourse-cdn.com/v4/letter/g/ea666f/32.png) [@ganesh\_sainath](https://discuss.elastic.co/u/ganesh_sainath)\
**Post date:** [June 6, 2013, 4:39pm UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/8 "2013-06-06T16:39:16Z")

</div>

Hi David,

Thanks for the information. It worked like a charm.

On Wednesday, 5 June 2013 11:13:50 UTC-7, David Pilato wrote:

> Transport layer uses port 9300 to 9399.  
> 9200-9299 are REST ports.
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 5 juin 2013 à 18:53, ganesh sainath \<[ganes...@gmail.com](mailto:ganes...@gmail.com) \<javascript:\>\>  
> a écrit :
> 
> Hi David, Tarang,
> 
> I am stuck at this point. I think you provide some head up on this. I am  
> getting NoNodeAvailableException. I have attached the screenshot of the  
> same.
> 
> What I inferred from the forums is that the cluster name can be entered  
> wrongly because of which this exception can occur. I double checked by  
> running the following command curl -XGET '  
> [http://localhost:9200/\_cluster/health?pretty=true](http://localhost:9200/_cluster/health?pretty=true)'  
> The java code is referring to the same as well. I am not sure where I am  
> going wrong. I have attached the screenshot of the same.
> 
> import org.elasticsearch.common.settings.\*;  
> //all necessary imports
> 
> class Test  
> {  
> public static void main(String args)  
> {  
> [//System.out.println](https://System.out.println)("Hello World");  
> Client client = null;  
> // = new TransportClient();  
> Settings settings = ImmutableSettings  
> .settingsBuilder()  
> .put("cluster.name","elasticsearch")  
> .build();  
> try{  
> client = new TransportClient(settings)  
> .addTransportAddress(new  
> InetSocketTransportAddress("localhost",9200));  
> }  
> catch(Exception e)  
> {  
> e.printStackTrace();  
> }  
> //InetSocketAddress inst = new  
> InetSocketAddress(InetAddress.getByName("127.0.0.1"),9200);  
> [//client.addTransportAddress](https://client.addTransportAddress)(inst);  
> try{  
> IndexResponse response = client.prepareIndex("twitter",  
> "tweet", "1")  
> .setSource(jsonBuilder()  
> .startObject()  
> .field("user", "kimchy")  
> .field("postDate", new Date())  
> .field("message", "trying out Elastic  
> Search")  
> .endObject()  
> )  
> .execute()  
> .actionGet();  
> }  
> catch(Exception e)  
> {  
> e.printStackTrace();  
> }  
> }
> 
> On Tuesday, 4 June 2013 15:49:29 UTC-7, ganesh sainath wrote:
> 
> > Hi All,
> > 
> > I am new to Elastic Search.
> > 
> > What I need is to insert data in to Elastic search using Java. I am not  
> > able to find any sample program to have as base that would help me connect  
> > to Elastic search. It would be great if any of you can guide me to  
> > appropriate links or post some sample code for the same.
> > 
> > Thanks,  
> > Ganesh Sainath
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Karan\_Sood](https://avatars.discourse-cdn.com/v4/letter/k/90ced4/32.png) [@Karan\_Sood](https://discuss.elastic.co/u/Karan_Sood)\
**Post date:** [June 8, 2013, 1:37pm UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/9 "2013-06-08T13:37:35Z")

</div>

You could also try the following link

[http://www.efroot.org](http://www.efroot.org)

It provides simple step by step tutorials to help get things rolling

On Wednesday, 5 June 2013 04:19:29 UTC+5:30, ganesh sainath wrote:

> Hi All,
> 
> I am new to Elastic Search.
> 
> What I need is to insert data in to Elastic search using Java. I am not  
> able to find any sample program to have as base that would help me connect  
> to Elastic search. It would be great if any of you can guide me to  
> appropriate links or post some sample code for the same.
> 
> Thanks,  
> Ganesh Sainath

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:32am UTC](https://discuss.elastic.co/t/insert-data-into-elastic-search-using-java/12264/10 "2017-07-06T02:32:11Z")

</div>


