2013-05-20 36 views
10

我用於彈性搜尋創建索引下面的代碼, 默認的Java API創建彈性搜索索引通過Java API的

HttpURLConnection con = null; 
    try 
    { 
     String url = "http://localhost:9200/test2"; 

     URL resturl = new URL(url); 
     con = (HttpURLConnection) resturl.openConnection(); 

     con.setDoOutput(true); 
     con.setRequestMethod("PUT"); 
     BufferedReader in = null; 
     try 
     { 
      if (con.getInputStream() != null) 
      { 
       in = new BufferedReader(new InputStreamReader(con.getInputStream())); 
      } 
     } 
     catch (IOException e) 
     { 
      if (con.getErrorStream() != null) 
      { 
       in = new BufferedReader(new InputStreamReader(con.getErrorStream())); 
      } 
     } 
     if (in == null) 
     { 
      throw new Exception("Unable to read response from server"); 
     } 
     StringBuffer decodedString = new StringBuffer(); 
     String line; 
     while ((line = in.readLine()) != null) 
     { 
      decodedString.append(line); 
     } 
     in.close(); 
     System.out.println("4"); 
     Integer responseCode = con.getResponseCode(); 
     System.out.println(responseCode); 
    } 
    catch (Exception ex) 
    { 
     ex.printStackTrace(); 
    } 
    finally 
    { 
     if (con != null) 
     { 
      con.disconnect(); 
     } 
    } 

通過使用REST API,我我能夠創建索引。默認情況下,JAVA API, 我收到以下異常。

org.elasticsearch.client.transport.NoNodeAvailableException: No node available 
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:202) 
at org.elasticsearch.client.transport.support.InternalTransportIndicesAdminClient.execute(InternalTransportIndicesAdminClient.java:85) 
at org.elasticsearch.client.support.AbstractIndicesAdminClient.create(AbstractIndicesAdminClient.java:200) 
at org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder.doExecute(CreateIndexRequestBuilder.java:206) 
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:62) 
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:57) 
at ElasticSearch.createIndex(ElasticSearch.java:121) 
at ElasticSearch.main(ElasticSearch.java:157) 

請指導我在哪裏犯了錯誤。在此先感謝

回答

8

(通過Java API)的端口TransportClient是不是HTTP 不同默認情況下,transportClient端口是9300

+0

謝謝您的回答。我仍然得到同樣的例外。 –

+0

現在工作正常。我在使用9300時犯了一個小錯誤,但在配置中我更新了端口號。 –

6

既然你有你的客戶你不應該是能夠做到這一點,那麼:

CreateIndexResponse createResponse = client.admin().indices().create(createIndexRequest("test1")).actionGet(); 
+2

我只是補充說'createIndexRequest'方法是'Requests.createIndexRequest(「test1」)' –

5

隨着副本和碎片設置:

Settings indexSettings = ImmutableSettings.settingsBuilder() 
       .put("number_of_shards", 1) 
       .put("number_of_replicas", 1) 
       .build(); 
CreateIndexRequest indexRequest = new CreateIndexRequest(index, indexSettings); 
client.admin().indices().create(indexRequest).actionGet();