2017-09-14 5 views
0

私たちは、春の起動時に弾性検索アプリケーションを開発しています。弾性検索によって提供されるJava APIまたはJava Rest Client APIは使用できません。代わりに、バネ・レスト・テンプレートを使用して弾性で操作する必要がありますが、弾性は残りのクライアントからの索引要求を受け入れていないようです。「受け入れられない」応答が返されます。誰かが私たちにヒントや情報をくれたら本当にありがたいです。弾性検索のためのSpring Rest Clientの実装方法は?

エラスティックバージョン:5.6

+1

コードを表示してください。 ES 5.6では、[低レベルのRESTクライアント](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low-usage-initialization)を使用することができます。 html)はApacheのHTTPクライアントのラッパーで、他の依存関係はありません。 – Val

+0

ほとんどの場合、HTTPリクエストヘッダに 'Content-Type'ヘッダを送信していません。可能? – Val

答えて

1

これを試してください。それはHttpURLConnectionを使用してHTTP APIを介してドキュメントをインデックス付けする私のために働く。

URL obj = new URL("http://localhost:9200/index/type"); 
String json = "{\n" + 
      " \"user\" : \"kimchy\",\n" + 
      " \"post_date\" : \"2009-11-15T14:12:12\",\n" + 
      " \"message\" : \"trying out Elasticsearch\"\n" + 
      "}"; 
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

con.setRequestMethod("POST"); 
con.setDoInput(true); 
con.setDoOutput(true); 
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 

OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); 
osw.write(json); 
osw.flush(); 
osw.close(); 

System.out.println(con.getResponseCode() + " : " + con.getResponseMessage()); 
if (con != null) 
    con.disconnect(); 

HttpURLConnectionを使用した簡単な検索の実行。

URL obj = new URL("http://localhost:9200/index/type/_search"); 
String json = "{\n" + 
       " \"query\": {\n" + 
       " \"match_all\": {}\n" + 
       " }\n" + 
       "}"; 
HttpURLConnection con = (HttpURLConnection) obj.openConnection(); 

con.setRequestMethod("GET"); 
con.setDoInput(true); 
con.setDoOutput(true); 
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 

OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream()); 
osw.write(json); 
osw.flush(); 
osw.close(); 

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

System.out.println("Response : " + br.readLine()); 

System.out.println(con.getResponseCode() + " : " + con.getResponseMessage()); 

if (con != null) 
    con.disconnect(); 
+0

シンプルなリクエストボディで検索する例を表示できますか? – Vikki

+1

私の答えを更新しました。希望が役立ちます。 –

関連する問題