【问题标题】:Elasticsearch Error: MapperParsingException: failed to parseElasticsearch 错误:MapperParsingException:解析失败
【发布时间】:2015-04-20 21:36:06
【问题描述】:

我想在 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}"

【问题讨论】:

标签: java elasticsearch apache-httpclient-4.x


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 2014-09-03
    • 2018-10-28
    • 1970-01-01
    • 2019-02-12
    • 2020-03-15
    相关资源
    最近更新 更多