2015-06-21 62 views
-1

我正在尝试编写一个简单的JAVA REST客户端,通过它我想要PUT/GET elasticsearch文档信息。 PUT工作正常,我的json数据被添加到索引中。 但问题是GET,响应代码是200,但它没有返回任何数据。ElasticSearch Rest API在JAVA中

任何人都可以请帮忙。

public static String httpGet(String resturl){ 
    String output = null; 
    try { 

      URL url = new URL(resturl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestMethod("GET"); 
      conn.setRequestProperty("Content-Type", "application/json"); 

      if (conn.getResponseCode() != 200) { 
       throw new RuntimeException("Failed : HTTP error code : " 
         + conn.getResponseCode()); 
      } 

      BufferedReader br = new BufferedReader(new InputStreamReader(
       (conn.getInputStream()))); 


      System.out.println("Output from Server .... \n"); 
      while ((output = br.readLine()) != null) { 
       //System.out.println(output); 
      } 

      conn.disconnect(); 

      } catch (MalformedURLException e) { 

      e.printStackTrace(); 

      } catch (IOException e) { 

      e.printStackTrace(); 

      } 

     return output; 
} 

我打电话的

RestClient.httpGet( “http://localhost:9200/gabsindex/employee/_search”)

+0

你为什么要那样做?你知道在java,.net,js,python等中有多个用于elasticsearch的客户端库。 – Roman

回答

0

println呼叫目前注释掉。如果您取消注释它,你会得到从服务器上的一个单行响应,像

{"took":50,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":7056,"max_score":1.0,"hits":[...]}} 

如果你正试图从教育遵循这个方法,这可以是好的。但请记住,有许多库可以完全用于此用途。你应该看看clients page