Elasticsearch Java API 7.0批量插入尝试基本示例,引发错误

时间:2019-05-03 15:47:51

标签: java elasticsearch elasticsearch-java-api elasticsearch-bulk-api

Java 1.8,Elasticsearch低级和高级Rest客户端7.0.0

我正在尝试以下文档中的简单示例:Bulk API

BulkRequest bulkRequest = new BulkRequest();
request.add(new IndexRequest("posts").id("1")  
    .source(XContentType.JSON,"field", "valueString"));
 // not working


Map<String, Object> doc1 = new HashMap<>();
doc1.put("property", "value");
request.add(new IndexRequest("posts").id("1").source(doc1);
 // this is easy to add as a single IndexRequest, but not working here

 BulkResponse bulkResponse = this.getClient().bulk(request, RequestOptions.DEFAULT);
 // this is the line throwing the error, straight from the docs

错误:

{"error":{"root_cause":[{"type":"action_request_validation_exception","reason":"Validation Failed: 1: type is missing;"}],"type":"action_request_validation_exception","reason":"Validation Failed: 1: type is missing;"},"status":400}
        at org.elasticsearch.client.RestClient.convertResponse(RestClient.java:260)
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:238)
        at org.elasticsearch.client.RestClient.performRequest(RestClient.java:212)
        at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1433)
        ... 6 more

那么缺少什么类型?我尝试添加映射,将opType(“ create”)添加到在request.add()内部创建的IndexRequest中?

是的,对我来说,这可能只是一些简单的疏忽,但是我已经努力了一段时间,希望得到一些帮助。

1 个答案:

答案 0 :(得分:0)

“缺失类型”是由于您的请求中缺少“ _doc”而引起的。可以将新的IndexRequest(“ posts”)更改为新的IndexRequest(“ posts”,“ _ doc”)。由于_types在Elastic 7.x.x中已删除,因此这将给出“已弃用”警告。