Insert data into Elastic Search using Java

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.
For more options, visit https://groups.google.com/groups/opt_out.

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.comwrote:

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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

Further you can refer to this link

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

On Wed, Jun 5, 2013 at 8:07 AM, Tarang Dawer 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.comwrote:

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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

Did you read the ref doc: 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 :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 5 juin 2013 à 00:49, ganesh sainath 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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

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/
Is there anything unclear we should rewrite?

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

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

Le 5 juin 2013 à 00:49, ganesh sainath <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 <javascript:>.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

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'
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("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(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.
For more options, visit https://groups.google.com/groups/opt_out.

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

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

Le 5 juin 2013 à 18:53, ganesh sainath 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'
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("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(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.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

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 :wink:
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 5 juin 2013 à 18:53, ganesh sainath <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'
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("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(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 <javascript:>.
For more options, visit 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.
For more options, visit https://groups.google.com/groups/opt_out.

You could also try the following link

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.
For more options, visit https://groups.google.com/groups/opt_out.