我想在Java中使用Apache HttpClient向Elasticsearch添加一个条目。
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://localhost:9200/index/entries/");
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("title", "Title123"));
params.add(new BasicNameValuePair("content", "Content123"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
发送请求后,Elasticsearch会响应以下错误:
org.elasticsearch.index.mapper.MapperParsingException:无法解析
引起:org.elasticsearch.ElasticsearchParseException:无法从(offset = 0,length = 33)派生xcontent:[...]
我的请求似乎无效,但我不明白为什么。
以下是Elasticsearch的完整跟踪:
[index][0], node[LomlwFXNQl2w_hBrHGKU0Q], [P], s[STARTED]: Failed to execute [index {[index][entries][AUzTUAj4AmLRyNLK73dO], source[_na_]}]
org.elasticsearch.index.mapper.MapperParsingException: failed to parse
at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:565)
at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:493)
at org.elasticsearch.index.shard.IndexShard.prepareCreate(IndexShard.java:453)
at org.elasticsearch.action.index.TransportIndexAction.shardOperationOnPrimary(TransportIndexAction.java:201)
at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction.performOnPrimary(TransportShardReplicationOperationAction.java:515)
at org.elasticsearch.action.support.replication.TransportShardReplicationOperationAction$AsyncShardOperationAction$1.run(TransportShardReplicationOperationAction.java:422)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.elasticsearch.ElasticsearchParseException: Failed to derive xcontent from (offset=0, length=33): [116, 105, 116, 108, 101, 61, 84, 105, 116, 108, 101, 49, 50, 51, 38, 99, 111, 110, 116, 101, 110, 116, 61, 67, 111, 110, 116, 101, 110, 116, 49, 50, 51]
at org.elasticsearch.common.xcontent.XContentFactory.xContent(XContentFactory.java:195)
at org.elasticsearch.common.xcontent.XContentHelper.createParser(XContentHelper.java:73)
at org.elasticsearch.common.xcontent.XContentHelper.createParser(XContentHelper.java:51)
at org.elasticsearch.index.mapper.DocumentMapper.parse(DocumentMapper.java:507)
... 8 more
Apache日志:
2015/04/19 22:12:59:959 CEST [DEBUG] wire - http-outgoing-0 >> "POST /index/entries/ HTTP/1.1[\r][\n]"
2015/04/19 22:12:59:962 CEST [DEBUG] wire - http-outgoing-0 >> "Content-Length: 33[\r][\n]"
2015/04/19 22:12:59:962 CEST [DEBUG] wire - http-outgoing-0 >> "Content-Type: application/x-www-form-urlencoded; charset=UTF-8[\r][\n]"
2015/04/19 22:12:59:962 CEST [DEBUG] wire - http-outgoing-0 >> "Host: localhost:9200[\r][\n]"
2015/04/19 22:12:59:963 CEST [DEBUG] wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
2015/04/19 22:12:59:963 CEST [DEBUG] wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.4.1 (Java/1.8.0_40)[\r][\n]"
2015/04/19 22:12:59:963 CEST [DEBUG] wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
2015/04/19 22:12:59:963 CEST [DEBUG] wire - http-outgoing-0 >> "[\r][\n]"
2015/04/19 22:12:59:963 CEST [DEBUG] wire - http-outgoing-0 >> "title=Title123&content=Content123"
2015/04/19 22:13:00:051 CEST [DEBUG] wire - http-outgoing-0 << "HTTP/1.1 400 Bad Request[\r][\n]"
2015/04/19 22:13:00:051 CEST [DEBUG] wire - http-outgoing-0 << "Content-Type: application/json; charset=UTF-8[\r][\n]"
2015/04/19 22:13:00:051 CEST [DEBUG] wire - http-outgoing-0 << "Content-Length: 312[\r][\n]"
2015/04/19 22:13:00:052 CEST [DEBUG] wire - http-outgoing-0 << "[\r][\n]"
2015/04/19 22:13:00:052 CEST [DEBUG] wire - http-outgoing-0 << "{"error":"MapperParsingException[failed to parse]; nested: ElasticsearchParseException[Failed to derive xcontent from (offset=0, length=33): [116, 105, 116, 108, 101, 61, 84, 105, 116, 108, 101, 49, 50, 51, 38, 99, 111, 110, 116, 101, 110, 116, 61, 67, 111, 110, 116, 101, 110, 116, 49, 50, 51]]; ","status":400}"
答案 0 :(得分:1)
好的,我发现了我的错误。
我的实体未被处理为JSON。使用StringEntity工作:
StringEntity params = new StringEntity("{your JSON String}");
params.setContentType("application/json");
httppost.setEntity(params);
httppost.setHeader("Content-type", "application/json");
HttpResponse response = httpclient.execute(httppost);