# Elasticsearch java code sample for query

**URL:** <https://discuss.elastic.co/t/elasticsearch-java-code-sample-for-query/7828>\
**Category:** Elasticsearch\
**Created:** [May 23, 2012, 8:32pm UTC](https://discuss.elastic.co/t/elasticsearch-java-code-sample-for-query/7828 "2012-05-23T20:32:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kara](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@Kara](https://discuss.elastic.co/u/Kara)\
**Post date:** [May 23, 2012, 8:32pm UTC](https://discuss.elastic.co/t/elasticsearch-java-code-sample-for-query/7828/1 "2012-05-23T20:32:00Z")

</div>

Where can I find some java code or documentation where it queries the  
ES? Just a code snippet where it makes a connection to a ES server and  
queries for simple fields.

Thanks,  
Karine

---

<div class="post-metadata">

**Author:** ![Martin\_Czygan](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Martin\_Czygan](https://discuss.elastic.co/u/Martin_Czygan)\
**Post date:** [May 23, 2012, 10:33pm UTC](https://discuss.elastic.co/t/elasticsearch-java-code-sample-for-query/7828/2 "2012-05-23T22:33:32Z")

</div>

Your entry point would be:

```
* http://www.elasticsearch.org/guide/reference/java-api/

```

and especially:

1. connect to a client (if your ES server is already running, use a  
Transport Client):

2. then search

Hope this helps.

On Wednesday, 23 May 2012 22:32:00 UTC+2, Kara wrote:

> Where can I find some java code or documentation where it queries the  
> ES? Just a code snippet where it makes a connection to a ES server and  
> queries for simple fields.
> 
> Thanks,  
> Karine

---

<div class="post-metadata">

**Author:** ![Kara](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@Kara](https://discuss.elastic.co/u/Kara)\
**Post date:** [May 23, 2012, 10:39pm UTC](https://discuss.elastic.co/t/elasticsearch-java-code-sample-for-query/7828/3 "2012-05-23T22:39:33Z")

</div>

Thanks for your help. Yes - the ES is running already on the remote  
host. I got the following code but I am getting an "Exception in  
thread "main"  
org.elasticsearch.client.transport.NoNodeAvailableException: No node  
available"

public static void main(String args) {

```
	Settings settings =

```

ImmutableSettings.settingsBuilder().put("client.transport.sniff",  
true).build();  
TransportClient client = new  
TransportClient(settings).addTransportAddress(new  
InetSocketTransportAddress("hostname", 9300));  
SearchResponse response = client.prepareSearch("mongoindex")  
.setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
.setQuery(QueryBuilders.termQuery("firstName", "John"))  
.setFrom(0).setSize(60).setExplain(true)  
.execute()  
.actionGet();

```
	SearchHit[] docs = response.getHits().getHits();
	System.out.println("size is " + docs.length);
	client.close();
}

```

On May 23, 3:33 pm, Martin Czygan [martin.czy...@googlemail.com](mailto:martin.czy...@googlemail.com)  
wrote:

> Your entry point would be:
> 
> ```
> *http://www.elasticsearch.org/guide/reference/java-api/
> 
> ```
> 
> and especially:
> 
> 1. connect to a client (if your ES server is already running, use a  
> Transport Client):
> 
> 2. then search
> 
> Hope this helps.
> 
> On Wednesday, 23 May 2012 22:32:00 UTC+2, Kara wrote:
> 
> > Where can I find some java code or documentation where it queries the  
> > ES? Just a code snippet where it makes a connection to a ES server and  
> > queries for simple fields.
> 
> > Thanks,  
> > Karine

---

<div class="post-metadata">

**Author:** ![Martin\_Czygan](https://avatars.discourse-cdn.com/v4/letter/m/a6a055/32.png) [@Martin\_Czygan](https://discuss.elastic.co/u/Martin_Czygan)\
**Post date:** [May 23, 2012, 10:55pm UTC](https://discuss.elastic.co/t/elasticsearch-java-code-sample-for-query/7828/4 "2012-05-23T22:55:13Z")

</div>

Ok, without knowing your detailed setup, let me just point to two things:

1. In my app I used something along

Maybe you need to set this as well - so that the server's "cluster.name"  
and your transport client's match.  
Also your "client.transport.sniff" may not be necessary for a first simple  
setup.

1. You added

"hostname" maybe that's just obfuscation - or maybe a typo?

Martin

On Thursday, 24 May 2012 00:39:33 UTC+2, Kara wrote:

> Thanks for your help. Yes - the ES is running already on the remote  
> host. I got the following code but I am getting an "Exception in  
> thread "main"  
> org.elasticsearch.client.transport.NoNodeAvailableException: No node  
> available"
> 
> public static void main(String args) {
> 
> ```
> Settings settings = 
> 
> ```
> 
> ImmutableSettings.settingsBuilder().put("client.transport.sniff",  
> true).build();  
> TransportClient client = new  
> TransportClient(settings).addTransportAddress(new  
> InetSocketTransportAddress("hostname", 9300));  
> SearchResponse response =  
> client.prepareSearch("mongoindex")  
> .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> .setQuery(QueryBuilders.termQuery("firstName", "John"))  
> .setFrom(0).setSize(60).setExplain(true)  
> .execute()  
> .actionGet();
> 
> ```
> SearchHit[] docs = response.getHits().getHits(); 
> System.out.println("size is " + docs.length); 
> client.close(); 
> } 
> 
> ```
> 
> On May 23, 3:33 pm, Martin Czygan [martin.czy...@googlemail.com](mailto:martin.czy...@googlemail.com)  
> wrote:
> 
> > Your entry point would be:
> > 
> > ```
> > *http://www.elasticsearch.org/guide/reference/java-api/ 
> > 
> > ```
> > 
> > and especially:
> > 
> > 1. connect to a client (if your ES server is already running, use a  
> > Transport Client):
> > 
> > 2. then search
> > 
> > Hope this helps.
> > 
> > On Wednesday, 23 May 2012 22:32:00 UTC+2, Kara wrote:
> > 
> > > Where can I find some java code or documentation where it queries the  
> > > ES? Just a code snippet where it makes a connection to a ES server and  
> > > queries for simple fields.
> > 
> > > Thanks,  
> > > Karine

---

<div class="post-metadata">

**Author:** ![Kara](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@Kara](https://discuss.elastic.co/u/Kara)\
**Post date:** [May 23, 2012, 11:12pm UTC](https://discuss.elastic.co/t/elasticsearch-java-code-sample-for-query/7828/5 "2012-05-23T23:12:58Z")

</div>

I added your steps into my code and it still gave me the node not  
available error. I tried it on my localhost and it is working. The  
remote host that I have is DHCP cloud instance. Do I have to do  
special network config for it? Or ES server should run on static IP  
for java client. The "curl" command line query works. Just the java  
client that does not work for the remote ES.

Thanks again!  
Kara

On May 23, 3:55 pm, Martin Czygan [martin.czy...@googlemail.com](mailto:martin.czy...@googlemail.com)  
wrote:

> Ok, without knowing your detailed setup, let me just point to two things:
> 
> 1. In my app I used something along
> 
> Maybe you need to set this as well - so that the server's "cluster.name"  
> and your transport client's match.  
> Also your "client.transport.sniff" may not be necessary for a first simple  
> setup.
> 
> 1. You added
> 
> "hostname" maybe that's just obfuscation - or maybe a typo?
> 
> Martin
> 
> On Thursday, 24 May 2012 00:39:33 UTC+2, Kara wrote:
> 
> > Thanks for your help. Yes - the ES is running already on the remote  
> > host. I got the following code but I am getting an "Exception in  
> > thread "main"  
> > org.elasticsearch.client.transport.NoNodeAvailableException: No node  
> > available"
> 
> > public static void main(String args) {
> 
> > ```
> > Settings settings =
> > 
> > ```
> > 
> > ImmutableSettings.settingsBuilder().put("client.transport.sniff",  
> > true).build();  
> > TransportClient client = new  
> > TransportClient(settings).addTransportAddress(new  
> > InetSocketTransportAddress("hostname", 9300));  
> > SearchResponse response =  
> > client.prepareSearch("mongoindex")  
> > .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)  
> > .setQuery(QueryBuilders.termQuery("firstName", "John"))  
> > .setFrom(0).setSize(60).setExplain(true)  
> > .execute()  
> > .actionGet();
> 
> > ```
> > SearchHit[] docs = response.getHits().getHits();
> > System.out.println("size is " + docs.length);
> > client.close();
> > }
> > 
> > ```
> 
> > On May 23, 3:33 pm, Martin Czygan [martin.czy...@googlemail.com](mailto:martin.czy...@googlemail.com)  
> > wrote:
> > 
> > > Your entry point would be:
> 
> > > ```
> > > *http://www.elasticsearch.org/guide/reference/java-api/
> > > 
> > > ```
> 
> > > and especially:
> 
> > > 1. connect to a client (if your ES server is already running, use a  
> > > Transport Client):
> 
> > > ```
> > > *http://www.elasticsearch.org/guide/reference/java-api/client.html
> > > 
> > > ```
> 
> > > 1. then search
> 
> > > ```
> > > *http://www.elasticsearch.org/guide/reference/java-api/search.html
> > > 
> > > ```
> 
> > > Hope this helps.
> 
> > > On Wednesday, 23 May 2012 22:32:00 UTC+2, Kara wrote:
> 
> > > > Where can I find some java code or documentation where it queries the  
> > > > ES? Just a code snippet where it makes a connection to a ES server and  
> > > > queries for simple fields.
> 
> > > > Thanks,  
> > > > Karine

---

<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, 3:27am UTC](https://discuss.elastic.co/t/elasticsearch-java-code-sample-for-query/7828/6 "2017-07-06T03:27:22Z")

</div>


