0
votes

I am new to elasticsearch but I am attempting to implement the basic java Get API from the elasticsearch documentation. https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.x/get.html

I am using the TransportClient to connect to elasticsearch. From what I understand the TransportClient uses port 9300. Elasticsearch is on port 9200. When I call the get request with my specified cluster, index, type, and id, I just get back an empty list of fields. I have already created the index in elasticsearch with the sense plugin and verified it through the head plugin. However, when I try to retrieve these values via my java implementation, I get nothing back. Here is a portion of my code below.

public String transportClientToElasticSearch()
{
   Settings settings = ImmutableSettings.settingsBuilder()
           .put("client.transport.sniff", "true")
           .put("cluster.name", "elasticsearch").build();

    Client client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
            //.addTransportAddress(new InetSocketTransportAddress("localhost", 9200));

    client.admin().indices().prepareRefresh("index").execute();

   GetResponse response = client.prepareGet("index", "type", "id")
           .execute()
           .actionGet();

   String elasticResponse = response.getFields().toString();
   return elasticResponse;
}

My initial thought is that this is due to a difference in port numbers for the TransportClient and ES. Any thoughts? Thank you.

1
Your ports are fine, 9200 is the default port for HTTP traffic and 9300 is the default port for TCP traffic (i.e. via TransportClient). Can you show the relevant log from your ES server? - Val
Hi @Val, thank you for your reply. Looking in the logs folder, only elasticsearch.log has been updated and only on starting elasticsearch.bat. I can't seem to find any log statements or files in reference to the above. - mil06
Have you tried to retrieve response.getSource() instead, which should yield a map of your document fields? - Val
Awesome! That worked. response.getSource() returned the fields for the Id specified. This is what I would have expected to be returned from response.getFields(). - mil06

1 Answers

0
votes

You should simply retrieve the fields via response.getSource(), which will yield a Map<String, Object>