Ank*_*mik 7 java elasticsearch
我一直在尝试java使用以下代码将数据放入弹性搜索中:
String url = "http://localhost:9200/testindex2/test/2";
HttpClient client = new DefaultHttpClient();
HttpPut put = new HttpPut(url);
JSONObject json = new JSONObject();
json.put("email", "abc@abof.com");
json.put("first_name", "abc");
StringEntity se = new StringEntity("JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"Text"));
put.setEntity(se);
HttpResponse response = client.execute(put);
System.out.println("\nSending 'PUT' request to URL : " + url);
System.out.println("Put parameters : " + put.getEntity());
System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
向URL发送'PUT'请求:http:// localhost:9200/testindex2/test/2 放置参数:[Content-Type:text/plain; charset = ISO-8859-1,Content- Encoding:Text,Content-Length:52,Chunked:false]响应代码:400 {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason ":"无法解析"}],"类型":"mapper_parsing_exception","reason":"无法解析","caused_by":{"type":"not_x_content_exception","reason":"压缩器检测只能在某些xcontent字节或压缩的xcontent字节"}}上调用,"status":400}
此外,当我从其他客户端尝试相同的代码时,它运行得很好,不知道为什么会出现这个问题.
已更换
StringEntity se = new StringEntity("JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"Text"));
Run Code Online (Sandbox Code Playgroud)
有了这个:
StringEntity se = new StringEntity(json.toString(),ContentType.APPLICATION_JSON);
Run Code Online (Sandbox Code Playgroud)
现在正在工作
| 归档时间: |
|
| 查看次数: |
8699 次 |
| 最近记录: |