Odd error creating index via Rest API (ES 6.2.2)

I'm trying to use the ES High-level Rest Client to integrate to ES (6.2.2) from my Java code. Here's the test class I wrote to re-create the problem. The code works as written, but the commented out code doesn't work. But I patterned it faithfully on the manual: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.2/java-rest-high-create-index.html

Any advice/help would be greatly appreciated!

@Test
public void DoCreateJobIndex () throws IOException {
    RestHighLevelClient client = new RestHighLevelClient (
            RestClient.builder (
                    new HttpHost (esHost, 0, "https")));
    String index = "my_job_profiles";
    CreateIndexRequest request = new CreateIndexRequest (index);
    request.source("{\n" +
            "  \"settings\":{\"number_of_shards\":1},\n" +
            "  \"mappings\":{\n" +
            "    \"JobProfileDTO\":{\n" +
            "      \"properties\":{\n" +
            "        \"jobDetails\":{\n" +
            "          \"type\":\"text\",\n" +
            "          \"analyzer\":\"english\"\n" +
            "        }\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}", XContentType.JSON);

// request.settings (Settings.builder ()
// .put ("index.number_of_shards", 1)
// .put ("index.number_of_replicas", 1));
// request.mapping (index,
// "{\n" +
// " "JobProfileDTO":{\n" +
// " "properties":{\n" +
// " "jobDetails":{\n" +
// " "type":"text",\n" +
// " "analyzer":"english"\n" +
// " }\n" +
// " }\n" +
// " }\n" +
// "}",
// XContentType.JSON
// );

    request.timeout (TimeValue.timeValueMinutes (1));
    CreateIndexResponse response = client.indices ().create (request);
    logger.info (response.toString ());
    client.close ();
}

The above code works (looked up the index via postman):

{
"my_job_profiles": {
"aliases": {},
"mappings": {
"JobProfileDTO": {
"properties": {
"jobDetails": {
"type": "text",
"analyzer": "english"
}
}
}
},
"settings": {
"index": {
"creation_date": "1526843609577",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "xr2LC2vxQpGbHw3hBbgm8w",
"version": {
"created": "6020299"
},
"provided_name": "my_job_profiles"
}
}
}
}

But the commented-out code gets the following exception:

ElasticsearchStatusException[Elasticsearch exception [type=mapper_parsing_exception, reason=Failed to parse mapping [my_job_profiles]: Root mapping definition has unsupported parameters: [JobProfileDTO : {properties={jobDetails={analyzer=english, type=text}}}]]
]; nested: ElasticsearchException[Elasticsearch exception [type=mapper_parsing_exception, reason=Root mapping definition has unsupported parameters: [JobProfileDTO : {properties={jobDetails={analyzer=english, type=text}}}]]];

The first argument of the request.mapping() function takes the mapping type, not the index (see javadocs). So this would be:

request.mapping("JobProfileDTO", 
      "{ \"properties\": {\"jobDetails\":{ \"type\":\"text\", \"analyzer\":\"english\"}}}",
      XContentType.JSON
    );

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