Rahul
Rahul

Reputation: 447

When Trying to access Elastic Search get using java getting Empty Response

Following the Docs of Elastic Search Was able to Set up the Java Environment for its API's. But when Trying to access from an index called twitter, under a type called tweet, with id valued 1 the response is coming null with no error in console.

Code for the Above

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

        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        GetResponse response = client.prepareGet("twitter", "tweet", "2").get();
        System.out.println(response.getFields());

        client.close();

Kibana Console data for the Same

{
        "_index": "twitter",
        "_type": "tweet",
        "_id": "2",
        "_score": 1,
        "_source": {
          "user": "Rahul",
          "post_date": "2009-11-15T14:12:12",
          "message": "trying out Elasticsearch"
        }
      },
      {
        "_index": "twitter",
        "_type": "tweet",
        "_id": "1",
        "_score": 1,
        "_source": {
          "user": "kimchy",
          "post_date": "2009-11-15T14:12:12",
          "message": "trying out Elasticsearch"
        }
      }

Upvotes: 0

Views: 1064

Answers (2)

monojohnny
monojohnny

Reputation: 6181

Make sure you are using HTTP , not HTTPs - the server may be hanging-up on you if you make a simple plain-text HTTP request?

Upvotes: 0

Rahul Singh
Rahul Singh

Reputation: 19622

Please Try some thing like this hope it helps

RestClient restClient = RestClient.builder(
                new HttpHost("localhost", 9200, "http")).build();

        Response response = restClient.performRequest("GET", "/twitter/_search",
               Collections.singletonMap("pretty", "true"));


/*
        JsonObject user = new JsonObject();
        user.addProperty("user","Yash");
        user.addProperty("post_date","2009-11-15T14:12:12");
        user.addProperty("message","Checking json");

        HttpEntity entity = new NStringEntity(user.toString());

        Response indexResponse = restClient.performRequest(
                "PUT",
                "/twitter/tweet/3",
                Collections.<String, String>emptyMap(),
                entity);
*/





        System.out.println(EntityUtils.toString(response.getEntity()));

        restClient.close();

Upvotes: 1

Related Questions