2017-08-13 65 views
0

我對彈性搜索概念非常陌生。我正在嘗試使用彈性搜索來構建一個簡單的應用程序。在彈性搜索中沒有可用的節點例外

public class App 
{ 
public static void main(String[] args) 
{ 
    try { 
     TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) 
       .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); 

     ElasticOperations.addDocToIndex(client, ElasticUtility.getJsonMap("Sandesha", "male", "[email protected]", "Hassan")); 
     System.out.println("Doc added"); 
     client.close(); 
    } catch (UnknownHostException e) { 

     e.printStackTrace(); 
    } 

} 
} 

但是,當試圖運行這個程序時,我得到NoNodeAvailableException

異常在線程 「主」 NoNodeAvailableException [配置的節點都不是可用的:[{#運輸#-1} {w1bSLAN-TFWUHE1PA3qDeQ} {本地主機} {127.0.0.1:9300}]] 在有機.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:347) at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:245) at org.elasticsearch.client.transport.TransportProxyClient.execute (TransportProxyClient.java:59) at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:363) at org.elasticsearch.client.support.AbstractClient.execute(Abst ractClient.java:408) at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:80) at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:54) at com.sandesha.elasticsearch。 Elastic.ElasticOperations.addDocToIndex(ElasticOperations.java:11)​​ 在com.sandesha.elasticsearch.Elastic.App.main(App.java:27)

請幫我解決這個問題。 我ElasticOperations類,

public class ElasticOperations { 

public static void addDocToIndex(TransportClient client, Map<String,Object> jsonMap) 
{ 
    client.prepareIndex("customers", "personal").setSource(jsonMap).execute().actionGet(); 
} 

} 

而且我ElasticUtility類是什麼樣子,

public class ElasticUtility { 

public static Map<String, Object> getJsonMap(String name, String gender, String email, String city) 
{ 
    Map<String,Object> jsonMap = new HashMap<String, Object>(); 
    jsonMap.put("name", name); 
    jsonMap.put("gender", gender); 
    jsonMap.put("email", email); 
    jsonMap.put("city", city); 

    return jsonMap; 
} 
} 

謝謝。

+0

您可以從終端連接到彈性還是使用感應? – sourabh1024

+0

我可以通過終端連接。它運行在9200端口上 –

回答

0

我認爲你的集羣名稱不匹配。如果您的羣集名稱不是「elasticsearch」,那很可能是問題所在。

在瀏覽器中轉到http://localhost:9200並獲取您的羣集名稱。然後,嘗試將集羣名稱屬性添加到TransportClient,如下所示:

Settings settings = Settings.builder() 
          .put("cluster.name", "elasticsearch_foo").build(); 
TransportClient client = new PreBuiltTransportClient(settings) 
         .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));