# Getting started with ElasticSearch with JAVA API

**URL:** <https://discuss.elastic.co/t/getting-started-with-elasticsearch-with-java-api/9054>\
**Category:** Elasticsearch\
**Created:** [September 18, 2012, 4:43pm UTC](https://discuss.elastic.co/t/getting-started-with-elasticsearch-with-java-api/9054 "2012-09-18T16:43:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali\_ilyas](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@ali\_ilyas](https://discuss.elastic.co/u/ali_ilyas)\
**Post date:** [September 18, 2012, 4:43pm UTC](https://discuss.elastic.co/t/getting-started-with-elasticsearch-with-java-api/9054/1 "2012-09-18T16:43:01Z")

</div>

Hi,  
I am new to elastic search and having problem in connecting the node with  
Java API.  
Settings settings = ImmutableSettings.settingsBuilder()  
.put("cluster.name", "elasticsearch").build();  
Client client = new  
TransportClient(settings).addTransportAddress(new  
InetSocketTransportAddress("localhost", 9200));  
I am trying to get the node and create the index on it with this code. And  
I am getting Exception on nettyLayer..  
Where when it using httpput , I am able to get the index created.  
HttpPut httpPut = new HttpPut("[http://localhost:9200/data/big/1](http://localhost:9200/data/big/1)");  
httpPut.addHeader("Accept", "application/json");  
httpPut.addHeader("Content-Type",  
"application/json");  
JSONObject json = new JSONObject();  
json.put("name", "testing");  
json.put("message", "adding new index");  
StringEntity entity = new  
StringEntity(json.toString(), "UTF-8");  
entity.setContentType("application/json");

```
                        httpPut.setEntity(entity);
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpResponse response = httpClient 

```

.execute(httpPut);  
Please help me out in this regard.

--

---

<div class="post-metadata">

**Author:** ![Robin\_Verlangen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/robin_verlangen/32/1542_2.png) [@Robin\_Verlangen](https://discuss.elastic.co/u/Robin_Verlangen)\
**Post date:** [September 18, 2012, 4:47pm UTC](https://discuss.elastic.co/t/getting-started-with-elasticsearch-with-java-api/9054/2 "2012-09-18T16:47:27Z")

</div>

Why don't you use the real Java API instead of the REST API?

Take a look at:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Best regards,

Robin Verlangen  
_Software engineer_  
\*  
\*  
W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
E robin@us2.nl

Disclaimer: The information contained in this message and attachments is  
intended solely for the attention and use of the named addressee and may be  
confidential. If you are not the intended recipient, you are reminded that  
the information remains the property of the sender. You must not use,  
disclose, distribute, copy, print or rely on this e-mail. If you have  
received this message in error, please contact the sender immediately and  
irrevocably delete this message and any copies.

2012/9/18 ali ilyas [mmaipucit@gmail.com](mailto:mmaipucit@gmail.com)

> Hi,  
> I am new to Elasticsearch and having problem in connecting the node with  
> Java API.  
> Settings settings = ImmutableSettings.settingsBuilder()  
> .put("cluster.name", "elasticsearch").build();  
> Client client = new  
> TransportClient(settings).addTransportAddress(new  
> InetSocketTransportAddress("localhost", 9200));  
> I am trying to get the node and create the index on it with this code.  
> And I am getting Exception on nettyLayer..  
> Where when it using httpput , I am able to get the index created.  
> HttpPut httpPut = new HttpPut("[http://localhost:9200/data/big/1](http://localhost:9200/data/big/1)");  
> httpPut.addHeader("Accept",  
> "application/json");  
> httpPut.addHeader("Content-Type",  
> "application/json");  
> JSONObject json = new JSONObject();  
> json.put("name", "testing");  
> json.put("message", "adding new index");  
> StringEntity entity = new  
> StringEntity(json.toString(), "UTF-8");  
> entity.setContentType("application/json");
> 
> ```
> httpPut.setEntity(entity);
> HttpClient httpClient = new
> 
> ```
> 
> DefaultHttpClient();  
> HttpResponse response = httpClient  
> .execute(httpPut);  
> Please help me out in this regard.
> 
> --

--

---

<div class="post-metadata">

**Author:** ![ali\_ilyas](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@ali\_ilyas](https://discuss.elastic.co/u/ali_ilyas)\
**Post date:** [September 18, 2012, 4:57pm UTC](https://discuss.elastic.co/t/getting-started-with-elasticsearch-with-java-api/9054/3 "2012-09-18T16:57:37Z")

</div>

I am using JAVA API and getting the exception , where if I use rest  
framework, it works fine.

On Tuesday, September 18, 2012 6:47:30 PM UTC+2, Robin Verlangen wrote:

> Why don't you use the real Java API instead of the REST API?
> 
> Take a look at:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/)
> 
> Best regards,
> 
> Robin Verlangen  
> _Software engineer_  
> \*  
> \*  
> W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> E ro...@us2.nl \<javascript:\>
> 
> Disclaimer: The information contained in this message and attachments is  
> intended solely for the attention and use of the named addressee and may be  
> confidential. If you are not the intended recipient, you are reminded that  
> the information remains the property of the sender. You must not use,  
> disclose, distribute, copy, print or rely on this e-mail. If you have  
> received this message in error, please contact the sender immediately and  
> irrevocably delete this message and any copies.
> 
> 2012/9/18 ali ilyas \<[mmai...@gmail.com](mailto:mmai...@gmail.com) \<javascript:\>\>
> 
> > Hi,  
> > I am new to Elasticsearch and having problem in connecting the node with  
> > Java API.  
> > Settings settings = ImmutableSettings.settingsBuilder()  
> > .put("cluster.name", "elasticsearch").build();  
> > Client client = new  
> > TransportClient(settings).addTransportAddress(new  
> > InetSocketTransportAddress("localhost", 9200));  
> > I am trying to get the node and create the index on it with this code.  
> > And I am getting Exception on nettyLayer..  
> > Where when it using httpput , I am able to get the index created.  
> > HttpPut httpPut = new HttpPut("[http://localhost:9200/data/big/1](http://localhost:9200/data/big/1)");  
> > httpPut.addHeader("Accept",  
> > "application/json");  
> > httpPut.addHeader("Content-Type",  
> > "application/json");  
> > JSONObject json = new JSONObject();  
> > json.put("name", "testing");  
> > json.put("message", "adding new index");  
> > StringEntity entity = new  
> > StringEntity(json.toString(), "UTF-8");  
> > entity.setContentType("application/json");
> > 
> > ```
> > httpPut.setEntity(entity);
> > HttpClient httpClient = new 
> > 
> > ```
> > 
> > DefaultHttpClient();  
> > HttpResponse response = httpClient  
> > .execute(httpPut);  
> > Please help me out in this regard.
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [September 18, 2012, 5:05pm UTC](https://discuss.elastic.co/t/getting-started-with-elasticsearch-with-java-api/9054/4 "2012-09-18T17:05:26Z")

</div>

Hi Ali,

What Robin means to say is that you should be using the Elasticsearch  
Java API, and not use other REST/web libraries, even if they are in  
Java.

There is no need for HttpPut, HttpClient, etc... Simple use the  
methods defined in org.elasticsearch.client.Client. The guide could do  
a better job at providing a front to back usage of the client object,  
but the basics are there. Take a look at creating an index using the  
Java API:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

The mailing list has tons of examples, and while it might be hard to  
search for code, you should still find something. Hopefully this can  
be a start.

Cheers,

Ivan

On Tue, Sep 18, 2012 at 9:57 AM, ali ilyas [mmaipucit@gmail.com](mailto:mmaipucit@gmail.com) wrote:

> I am using JAVA API and getting the exception , where if I use rest  
> framework, it works fine.
> 
> On Tuesday, September 18, 2012 6:47:30 PM UTC+2, Robin Verlangen wrote:
> 
> > Why don't you use the real Java API instead of the REST API?
> > 
> > Take a look at:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/)
> > 
> > Best regards,
> > 
> > Robin Verlangen  
> > Software engineer
> > 
> > W [http://www.robinverlangen.nl](http://www.robinverlangen.nl)  
> > E ro...@us2.nl
> > 
> > Disclaimer: The information contained in this message and attachments is  
> > intended solely for the attention and use of the named addressee and may be  
> > confidential. If you are not the intended recipient, you are reminded that  
> > the information remains the property of the sender. You must not use,  
> > disclose, distribute, copy, print or rely on this e-mail. If you have  
> > received this message in error, please contact the sender immediately and  
> > irrevocably delete this message and any copies.
> > 
> > 2012/9/18 ali ilyas [mmai...@gmail.com](mailto:mmai...@gmail.com)
> > 
> > > Hi,  
> > > I am new to Elasticsearch and having problem in connecting the node with  
> > > Java API.  
> > > Settings settings = ImmutableSettings.settingsBuilder()  
> > > .put("cluster.name", "elasticsearch").build();  
> > > Client client = new  
> > > TransportClient(settings).addTransportAddress(new  
> > > InetSocketTransportAddress("localhost", 9200));  
> > > I am trying to get the node and create the index on it with this code.  
> > > And I am getting Exception on nettyLayer..  
> > > Where when it using httpput , I am able to get the index created.  
> > > HttpPut httpPut = new HttpPut("[http://localhost:9200/data/big/1](http://localhost:9200/data/big/1)");  
> > > httpPut.addHeader("Accept",  
> > > "application/json");  
> > > httpPut.addHeader("Content-Type",  
> > > "application/json");  
> > > JSONObject json = new JSONObject();  
> > > json.put("name", "testing");  
> > > json.put("message", "adding new index");  
> > > StringEntity entity = new  
> > > StringEntity(json.toString(), "UTF-8");  
> > > entity.setContentType("application/json");
> > > 
> > > ```
> > > httpPut.setEntity(entity);
> > > HttpClient httpClient = new
> > > 
> > > ```
> > > 
> > > DefaultHttpClient();  
> > > HttpResponse response = httpClient  
> > > .execute(httpPut);  
> > > Please help me out in this regard.
> > > 
> > > --
> 
> --

--

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [September 18, 2012, 9:38pm UTC](https://discuss.elastic.co/t/getting-started-with-elasticsearch-with-java-api/9054/5 "2012-09-18T21:38:48Z")

</div>

The Java TransportClient Service is using port 9300, not 9200.

For a quick example, see one of kimchy's  
gists [gist:2513321 · GitHub](https://gist.github.com/2513321)

Hope this helps,

Jörg

On Tuesday, September 18, 2012 6:43:01 PM UTC+2, ali ilyas wrote:

> Hi,  
> I am new to Elasticsearch and having problem in connecting the node with  
> Java API.  
> Settings settings = ImmutableSettings.settingsBuilder()  
> .put("cluster.name", "elasticsearch").build();  
> Client client = new  
> TransportClient(settings).addTransportAddress(new  
> InetSocketTransportAddress("localhost", 9200));  
> I am trying to get the node and create the index on it with this code.  
> And I am getting Exception on nettyLayer..  
> Where when it using httpput , I am able to get the index created.  
> HttpPut httpPut = new HttpPut("[http://localhost:9200/data/big/1](http://localhost:9200/data/big/1)");  
> httpPut.addHeader("Accept",  
> "application/json");  
> httpPut.addHeader("Content-Type",  
> "application/json");  
> JSONObject json = new JSONObject();  
> json.put("name", "testing");  
> json.put("message", "adding new index");  
> StringEntity entity = new  
> StringEntity(json.toString(), "UTF-8");  
> entity.setContentType("application/json");
> 
> ```
> httpPut.setEntity(entity);
> HttpClient httpClient = new 
> 
> ```
> 
> DefaultHttpClient();  
> HttpResponse response = httpClient  
> .execute(httpPut);  
> Please help me out in this regard.

--

---

<div class="post-metadata">

**Author:** ![peterhawkins](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peterhawkins/32/2558_2.png) [@peterhawkins](https://discuss.elastic.co/u/peterhawkins)\
**Post date:** [December 28, 2012, 4:42pm UTC](https://discuss.elastic.co/t/getting-started-with-elasticsearch-with-java-api/9054/6 "2012-12-28T16:42:43Z")

</div>

use [JEST](https://github.com/searchbox-io/Jest) if you really want to some of the functionality of Apache HttpClient. In a project, i had a need to send custom headers, JEST had that capability plus the query building of the regular ES client API.

---

<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:58am UTC](https://discuss.elastic.co/t/getting-started-with-elasticsearch-with-java-api/9054/7 "2017-07-06T02:58:17Z")

</div>


