Elasticsearch 7.17 Java RestHighLevelClient to Elasticsearch 8.10.0 server communication issue

Hi,

Since we have a large Java Client codebase, our migration to 8.x Java Client would take time and as intermediary solution, we are constrained to use Elasticsearch 7.17.x client in combination with 8.10.0 server

I am trying to get Elasticsearch 7.17 Java RestHighLevelClient to talk to Elasticsearch 8.10.0 Server. As recommended by the document, I am using the setApiCompatibility(true) - https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-compatibility.html

Below is the code which I tried testing using

Client Libraries version 7.17.0
Server Version 8.10.0

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestHighLevelClientBuilder;
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;


public class TestESPutMapping {

    public static void main(String[] args) {

        RestClient client1 = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
        RestHighLevelClient client = new RestHighLevelClientBuilder(client1).setApiCompatibilityMode(true).build();
        try  {
            String indexName = "elasticsearch-sql_test_index_account";

            // Create mapping JSON
            String dataMapping = "{\n" +
                    " \"properties\": {\n" +
                    "          \"gender\": {\n" +
                    "            \"type\": \"text\"\n" +
                    "          }," +
                    "          \"address\": {\n" +
                    "            \"type\": \"text\"\n" +
                    "          }," +
                    "          \"state\": {\n" +
                    "            \"type\": \"text\"\n" +
                    "          }" +
                    "       }"+
                    "   }";

            // Create put mapping request
            PutMappingRequest putMappingRequest = new PutMappingRequest(indexName)
                    .source(dataMapping, XContentType.JSON).type("_doc");
            // Update mapping
            AcknowledgedResponse putMappingResponse = client.indices().putMapping(putMappingRequest, RequestOptions.DEFAULT);
            boolean acknowledged = putMappingResponse.isAcknowledged();
            System.out.println("Mapping update acknowledged: " + acknowledged);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

With above code,

  1. if I remove .type("_doc") in PutMappingRequest, I get below exception
Exception in thread "main" org.elasticsearch.action.ActionRequestValidationException: Validation Failed: 1: mapping type is missing;
	at org.elasticsearch.action.ValidateActions.addValidationError(ValidateActions.java:15)
	at org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest.validate(PutMappingRequest.java:118)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:2133)
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:2105)
	at org.elasticsearch.client.IndicesClient.putMapping(IndicesClient.java:490)
	at org.nlpcn.es4sql.TestESPutMapping.main(TestESPutMapping.java:43)
  1. With same code if I retain .type("_doc") , I get below exception
Exception in thread "main" ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping: Root mapping definition has unsupported parameters:  [address : {type=text}] [gender : {type=text}] [state : {type=text}]]]; nested: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Root mapping definition has unsupported parameters:  [address : {type=text}] [gender : {type=text}] [state : {type=text}]]];
	at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:178)
	at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:2484)
	at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:2461)
	at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:2184)
	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:2137)
	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:2105)
	at org.elasticsearch.client.IndicesClient.putMapping(IndicesClient.java:490)
	at org.nlpcn.es4sql.TestESPutMapping.main(TestESPutMapping.java:43)
	Suppressed: org.elasticsearch.client.ResponseException: method [PUT], host [http://localhost:9200], URI [/elasticsearch-sql_test_index_account/_mapping/_doc?master_timeout=30s&include_type_name=true&timeout=30s], status line [HTTP/1.1 400 Bad Request]
Warnings: [[types removal] Specifying types in put mapping request is deprecated. Use typeless api instead]

Clarifications :

  1. Is .type("_doc") mandatory in above code
  2. What is wrong with above simple mapping ? (formatted json as below to declutter escape chars)
{
  "properties": {
    "gender": {
      "type": "text"
    },
    "address": {
      "type": "text"
    },
    "state": {
      "type": "text"
    }
  }
}

Kindly let me know if I am missing something grossly.

Regards
Muthu

I believe you should be using org.elasticsearch.client.indices.PutMappingRequest

Thanks a lot @TimV , that fixed the issue ! Will mark as solution.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.