gpt4 book ai didi

java - elastic search not x 内容异常

转载 作者:搜寻专家 更新时间:2023-10-31 20:18:01 24 4
gpt4 key购买 nike

我一直在尝试使用以下代码通过 java 将数据放入 Elasticsearch 中:

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());

我收到以下错误:

Sending 'PUT' request to URL : http://localhost:9200/testindex2/test/2 Put parameters : [Content-Type: text/plain; charset=ISO-8859-1,Content- Encoding: Text,Content-Length: 52,Chunked: false] Response Code : 400 {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}},"status":400}

此外,当我从休息客户端尝试相同的代码时,它运行得很好,不知道为什么会发生这个问题。

最佳答案

已替换

 StringEntity se = new StringEntity("JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"Text"));

用这个:

StringEntity se = new StringEntity(json.toString(),ContentType.APPLICATION_JSON);

及其现在的工作

关于java - elastic search not x 内容异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35740531/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com