2015-04-19 5 views
0

Я хочу добавить запись в Elasticsearch с использованием Apache HttpClient в Java.Ошибка Elasticsearch: MapperParsingException: не удалось разобрать

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: failed to parse

Caused by: org.elasticsearch.ElasticsearchParseException: Failed to derive xcontent from (offset=0, length=33): [...]

Кажется, что мой запрос является недействительным, но я понятия не имею, почему.

Вот полный след от 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}" 
+1

Проблема заключается в том, что вы не создали документ JSON, не так ли? – dadoonet

+0

Я бы порекомендовал вам использовать API Java Elasticsearch для написания документов. См. Http://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index_.html – Zouzias

+0

То, что я использую сейчас. Я получил его работу с HttpClient, но он намного надежнее и удобнее с API Elasticsearch Java – fwind

ответ

1

Ok Я нашел ошибку.

Мое лицо не обрабатывается как 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); 
Смежные вопросы