Reusing internal implementation to transform JSON mapping to ES mapping

Hello all, I have a use case where I have to do some validation on the JSON mapping passed into the PUT request body. There are several keywords (like dynamic, include_in_parent, etc.) and I cannot account for them all when I do my validations and it would be preferable to use the ES defined type Map<String, Property>. Is it possible to re-use the internal serialization that ES uses to make this data structure? If not, could someone please point me to the file it occurs in the github repo?

Update: This is in reference to the new Java client.

Thank you kindly,
Vlad

Not sure if my ask was somewhat unclear. Is serializing the mappings object provided in the PUT _mapping API body using the existing ES serializer possible? I've searched for it in the github repo but I couldn't find the point at which the JSON object mappings is turned into an Elasticsearch specific type (namely Map<String, Property>).
I require this because I want to validate that changes to the mapping have a specific format.

My alternative, and the way I've currently implemented this is to collapse the mappings object in dot-notation and remove certain Elasticsearch-specific keywords; like properties., .type, etc. This solution doesn't scale because there is a chance I didn't account for every keyword.
However collapsing the Map<String, Property> used by Elasticsearch internally (and returned in the java client from getMappingRequest) relies on Elasticsearch correctly parsing the mappings object.

Thanks

If anyone has a similar question in the future; withJson is a general deserializer than will convert a JSON to internal ES objects without having to make a request to the cluster itself.

  public static Map<String, Property> jsonToMapping(String json) {
    TypeMapping typeMapping = withJson(new TypeMapping.Builder(), new StringReader(json), new JacksonJsonpMapper()).build();
    return typeMapping.properties();
  }

  private static <T, B extends ObjectBuilder<T>> B withJson(
      B builder, Reader json, JsonpMapper mapper) {
    // Find which deserializer is needed
    JsonpDeserializer<?> classDeserializer =
        JsonpMapperBase.findDeserializer(builder.getClass().getEnclosingClass());

    @SuppressWarnings("unchecked")
    ObjectDeserializer<B> builderDeserializer =
        (ObjectDeserializer<B>) DelegatingDeserializer.unwrap(classDeserializer);

    JsonParser parser = mapper.jsonProvider().createParser(json);
    builderDeserializer.deserialize(builder, parser, mapper, parser.next());
    return builder;
  }

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