Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch Java API 7.0 Bulk Insert trying to basic example, throwing errors

Java 1.8, Elasticsearch Low and High Level Rest Clients 7.0.0

I am trying the simple example from the docs found here: 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:

{"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

so what type is missing? I have tried adding mappings, add opType("create") to the IndexRequest that is created inside the request.add()?

Yeah this is probably some simple oversight on my part but I have been wrestling this for a while guys and would appreciate some help.

like image 437
Justin Avatar asked Mar 04 '26 12:03

Justin


1 Answers

"missing type" arises because of missing "_doc" in your request. Changing new IndexRequest("posts") to new IndexRequest("posts","_doc") might work. This will give a 'deprecated' warning since _types are removed in Elastic 7.x.x.

like image 71
pranav karthik Avatar answered Mar 07 '26 03:03

pranav karthik