javaspring-bootelasticsearchresthighlevelclient

Elasticsearch exception [type=action_request_validation_exception, reason=Validation Failed: 1: type is missing;


I am trying to do bulk insert using elastic search rest high-level client, getting type missing validation error.

My pom.xml has the following dependency versions

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.9</version>
        <relativePath/>
</parent>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>4.3.5</version>
</dependency>
<dependency>
     <groupId>org.elasticsearch.client</groupId>
     <artifactId>elasticsearch-rest-high-level-client</artifactId>
     <version>7.15.2</version>
</dependency>

code snippet what I am using for bulk request

BulkRequest request = new BulkRequest();
            ObjectMapper objectMapper = new ObjectMapper();
            list.forEach(data -> {
                IndexRequest indexRequest = new IndexRequest("index2", "_doc").source(objectMapper.convertValue(data, Map.class));
                request.add(indexRequest);
            });

            BulkResponse bulkResponse = esClient.bulk(request, RequestOptions.DEFAULT);

I am getting below error

org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=action_request_validation_exception, reason=Validation Failed: 1: type is missing;]
    at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:176) ~[elasticsearch-7.15.2.jar:7.15.2]
    at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:2011) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
    at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1988) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
    at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1745) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
    at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1702) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
    at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1672) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
    at org.elasticsearch.client.RestHighLevelClient.bulk(RestHighLevelClient.java:577) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]

I have done reindexing using the below curl so that all the records in an index will have a common type name as '_doc'. And from the code, I am pointing to index2 which has common type _doc. This is the reason I am passing type as _doc.

curl --location --request POST '{baseServerurl}/_reindex' \
--header 'Content-Type: application/json' \
--header 'Cache-Control: no-cache' \
--data-raw '{
  "source": {
    "index": "index1",
    "type": "user"
  },
  "dest": {
    "index": "index2",
    "type": "_doc"
  }
}' 

Have removed the type declaration from my code mapping as upgrading ES to 7 don't support explicit type declaration at mapping level. ES dependency version that I am using is 7.15.


Solution

  • Talking to a cluster in version 6 you probably need to send the type value that is defined in the mapping for your index instead of "_doc".