我想使用 Nest 在 C# 中动态映射 JObject 中的属性。目标是将对象的每个字符串字段映射为 SearchAsYouType。我想到了 3 种没有用的方法:
- 使用 AutoMap 并直接在 C# 类中声明属性
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.使用DynamicTemplates但我在映射中找不到SearchAsYouType,它似乎在Nest 7.4.1中不存在
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.使用Visitor强制每个String都是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)
一切都失败了
我有一种感觉,解决方案是在 NEST.JsonNetSerializer 中以某种方式使映射中使用的设置适用于 JObject 但我找不到任何有用的东西