I'm trying to use the Elasticsearch Java SDK (7.6.0) to validate some index template JSON that I know works when sent to Elasticsearch's _template
endpoint through Kibana/direct to the Elasticsearch instance.
Example template JSON:
{
"order": 1,
"index_patterns": [
"organisations"
],
"mappings": {
"properties": {
"organisation_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
Example code:
public class IndexTemplatesTest extends ESSingleNodeTestCase {
public void testTemplateIsLoaded() throws IOException {
// clean all templates setup by the framework.
client().admin().indices().prepareDeleteTemplate("*").get();
// setup Template request for the JSON file
final PutIndexTemplateRequest template = new PutIndexTemplateRequest(templateDetails.getName());
final InputStream jsonIs = FileSystemUtils.openFileURLStream(
Objects.requireNonNull(getClass().getClassLoader().getResource("my-template.json"))
);
final byte[] bytes = new byte[jsonIs.available()];
template.source(bytes, XContentType.JSON);
// FIXME: the template creation fails because Elasticsearch thinks the "mappings" are invalid, although the same content works fine via Kibana?
final ActionFuture<AcknowledgedResponse> templateResponse = client().admin().indices().putTemplate(template);
assertTrue(templateResponse.actionGet().isAcknowledged());
}
}
Exception received:
MapperParsingException[Failed to parse mapping [properties]: Root mapping definition has unsupported parameters: [organisation_name : {type=text, fields={keyword={ignore_above=256, type=keyword}}}] ]; nested: MapperParsingException[Root mapping definition has unsupported parameters: [organisation_name : {type=text, fields={keyword={ignore_above=256, type=keyword}}}]];
Which is thrown by:
at org.elasticsearch.index.mapper.DocumentMapperParser.checkNoRemainingFields(DocumentMapperParser.java:148)
What am I doing wrong? Should I be able to test my template JSON using this kind of approach or is this completely wrong? (Basically I'm looking for a way to ensure anyone updating the template JSON at least does so in such a way that it's still valid JSON and would be accepted by Elasticsearch, without necessarily having to push it to a running Elasticsearch instance/cluster)