2016-09-30 5 views
0

JavaクライアントのElasticSearchのドキュメントに従っています。私はElasticSearchを開始しました。残りのAPIを使ってElasticSearchとやりとりすることができます。私は、Javaクライアントを使用すると、これまで私はこのようなメインを持っている:JavaトランスポートクライアントでElasticSearchに接続する方法は?

public class TestElastic { 

public static void main(String[] args) { 
    try{ 
     TransportClient client = TransportClient.builder().build() 
     .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); 
     JSONObject place = new JSONObject(); 
     place.put("name", "AAAAA"); 
     IndexResponse response = client.prepareIndex("my_database", "places", "1") 
       .setSource(place) 
       .get(); 
     System.out.println(response.toString()); 
     // Index name 
     String _index = response.getIndex(); 
     System.out.println(_index); 
     // Type name 
     String _type = response.getType(); 
     System.out.println(_type); 
     // Document ID (generated or not) 
     String _id = response.getId(); 
     System.out.println(_id); 
     // Version (if it's the first time you index this document, you will get: 1) 
     long _version = response.getVersion(); 
     System.out.println(_version); 
     // isCreated() is true if the document is a new one, false if it has been updated 
     boolean created = response.isCreated(); 
     System.out.println(created); 
     client.close(); 
    }catch (Exception ex){ 
     ex.printStackTrace(); 
    } 
} 

} Javaのログで

、私は127.0.0.1:9300との接続があることがわかります。しかし、 "準備インデックス"コマンドの後に私は何もエラーが表示されず、何も印刷されません(私はいくつかのシステムアウトコマンドを持っています)。 ElasticSearchのログには、相​​対的なものもありません。私は、Rest APIを使ってインデックスを作成すると、これをログに見ることができます。

+1

catchブロックに 'ex.printStackTrace();'を追加すると、例外があるかどうかを確認できますか?あなたはそれを静かに飲み込んでいます;-) – Val

+0

あなたは正しいです。どのように私はこれを逃すことができる? –

答えて

0

[OK]をクリックすると、エラーが表示されるのを忘れていました。問題は、JSONObjectがElasticSearchが望む形式ではないということでした。 MapとHashMapは受け入れられます。

関連する問題