mil06
mil06

Reputation: 29

Connecting to Elasticsearch with java transport client. Get request returns nothing

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.

Upvotes: 0

Views: 1277

Answers (1)

Val
Val

Reputation: 217274

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

Upvotes: 0

Related Questions