I want to map dynamically the properties inside a JObject in C# using Nest. The goal is to map every string field of the object as SearchAsYouType. I thought about 3 ways of doing that that didn't work:
- Use AutoMap and declare the attribute in the C# Class directly
public class Forfait
{
public long Id { get; set; }
[SearchAsYouType()]
public string Data { get; set; }
}
public class Act
{
public JObject Entity;
}
Forfait forfait = new Forfait()
{
Data = "data",
Id = 99
};
Act act = new Act()
{
Entity = JObject.FromObject(forfait)
};
await client.Indices.CreateAsync("index", o => o
.Map<Act>(m => m
.AutoMap()
2.Use DynamicTemplates but i can't find SearchAsYouType in the Mapping, it seems that it doesn't exist in Nest 7.4.1 yet
await client.Indices.CreateAsync("index", o => o
.Map<Act>(m => m
.DynamicTemplates(d =>d
.DynamicTemplate("stringassearch",dt => dt
.Match("entity.*")
.MatchMappingType("string")
.Mapping(ma =>ma
.)))));
3.Use a Visitor to force every String to be a SearchAsYouType
public class EveryStringIsASearchAsYouTypePropertyVisitor : NoopPropertyVisitor
{
public override IProperty Visit(PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
{
if (propertyInfo.PropertyType == typeof(String))
return new SearchAsYouTypeProperty();
return null;
}
}
await client.Indices.CreateAsync("index", o => o
.Map<Act>(m => m
.AutoMap(new EveryStringIsASearchAsYouTypePropertyVisitor(),2)
Everything failed
I have a feeling the solution is in the NEST.JsonNetSerializer to somehow make the settings used in the mapping apply inside the JObject but i couldn't find anything useful