elastic/elasticsearch-java

Cannot parse DynamicTemplate mapping with unknown field

ahermes opened this issue · 5 comments

Java API client version

7.17.21

Java version

17

Elasticsearch Version

7.16.2

Problem description

When i try to put a mapping to an already created indice with these settings

{
  "max_ngram_diff": 50,
  "analysis": {
	"tokenizer": {
	  "ngram_tokenizer_3-20": {
		"type": "ngram",
		"min_gram": 3,
		"max_gram": 20,
		"token_chars": [
		  "letter",
		  "digit",
		  "punctuation",
		  "symbol"
		]
	  }
	},
	"analyzer": {
	  "designation_analyser": {
		"type": "custom",
		"tokenizer": "ngram_tokenizer_3-20",
		"filter": [
		  "lowercase",
		  "asciifolding"
		]
	  }
	}
  }
}

I use a dynamic_template with an analyzer

{
  "properties": {
	"id": {
	  "type": "keyword"
	},
	"code": {
	  "type": "keyword"
	},
	"forbidden": {
	  "type": "boolean"
	},
	"designations": {
	  "type": "object",
	  "dynamic": true
	},
	"keyWords": {
	  "type": "nested",
	  "properties": {
		"id": {
		  "type": "keyword"
		},
		"label": {
		  "type": "keyword"
		}
	  }
	}
  },
  "dynamic_templates": [
	{
	  "designations": {
		"match_mapping_type": "string",
		"match": "([a-z]{2}_[A-Z]{2})",
		"mapping": {
		  "type": "keyword",
		  "index": "analyzed",
		  "analyzer": "designation_analyser",
		  "path_match": "designations.*"
		}
	  }
	}
  ]
}

which result in a JsonpMappingException for fields "analyzer" and "path_match" in the "mapping" section
Error deserializing co.elastic.clients.elasticsearch._types.mapping.KeywordProperty: Unknown field 'analyzer' (JSON path: dynamic_templates[0].designations.mapping.analyzer)

it is demonstrated in the documentation that it is usable that way. (indices created without any problem with this json with the java-high-level-api)

The way i send the request :

final RestClient httpClient = RestClient.builder(esHostList.stream()
				.map(esHost -> new HttpHost(esHost, esPort, httpProtocol))
				.toArray(HttpHost[]::new))
				.build();
final ElasticsearchTransport transport = new RestClientTransport(httpClient, jacksonMapper);
final ElasticsearchClient client = new ElasticsearchClient(transport);

final PutMappingRequest.Builder request = new PutMappingRequest.Builder()
				.index(indexName)
				.withJson(new ByteArrayInputStream(mappingAsJson.getBytes(StandardCharsets.UTF_8)));
client.indices().putMapping(request.build());

Hello, I think you're using the wrong mapping type here. The documentation shows that analyzer is used with a text type mapping, while you're using keyword which doesn't support analyzer.

Hello, thanks for your answer, but I'm unsure this is the case as it worked previously and the indice was created using this same mapping with the old API. Could you please send the source regarding the keyword not supporting analyzer ?