NEST library. Mapping

I use NEST functionality to build mapping for my index. I do it in the following way:

		TypeMappingDescriptor<object> typeMappingDescriptor= new TypeMappingDescriptor<object>();

		typeMappingDescriptor.Map<object>(ms => ms
			.Properties(ps => ps
				.Text(txt => txt .Name("text").IncludeInAll())
				.Number(n=> n.Name("number").Type(NumberType.Byte).IncludeInAll())
			)
			.DynamicTemplates(x => x
				.DynamicTemplate("customanalyzer_for_strings", y => y
					.PathMatch("path")
					.MatchMappingType("string")
					.Mapping(t => t
						.Text(r => r
							.Fields(fg => fg
								.Keyword(k=> k.Name("raw"))))))));

And I want convert typeMappingDescriptor to string representation or Json like this:

  "mappings": {
     "filesetdata": {
        "properties": {
          ......
        }
     }
  }

How can I do it?

You can use the serializer used by the client

var client = new ElasticClient();
var jsonString = client.Serializer.SerializeToString(typeMappingDescriptor);

Thank you for your help
But can I use Serializer.SerializeToString without creating ElasticClient?

You can use

var serializer = new JsonNetSerializer(new ConnectionSettings(new Uri("http://localhost:9200")));
serializer.SerializeToString(typeMappingDescriptor);

but this is not really much different from just using an instance of the client. I'd recommend also using a singleton instance of the client if you need to serialize multiple objects as many caches are used internally.

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