Elasticsearch java code sample for query


(Kara) #1

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


(Martin Czygan) #2

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


(Kara) #3

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
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

  2. 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


(Martin Czygan) #4

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

  1. In my app I used something along

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

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

    new InetSocketTransportAddress("hostname", 9300)

"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
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

  2. 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


(Kara) #5

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
wrote:

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

  1. In my app I used something along

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

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

    new InetSocketTransportAddress("hostname", 9300)

"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
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


(system) #6