我的索引包含一个Nest.GeoShape类型的字段。
问题#1 - Kibana将该字段显示为“indexed = false”,即使它已经像这样定义(在索引创建期间使用.MapFromAttributes())...
[ElasticProperty(Index = FieldIndexOption.NotAnalyzed, Store = true, IncludeInAll = false)]
public Nest.GeoShape ElasticShape { get; set; }
这是索引创建,如果是问题......
client.CreateIndex(c => c
.Index(indexName)
.InitializeUsing(set)
.AddMapping<ItemSearchable>(m => m
.MapFromAttributes()
.Properties(props => props
.GeoShape(x => x
.Name(n => n.ElasticShape)
.Tree(GeoTree.Geohash)
.TreeLevels(9)
.DistanceErrorPercentage(0.025))))
问题#2 - 当我进行查询时,返回的结果无法反序列化。
{“无法创建Nest.GeoShape类型的实例.Type是一个接口或抽象类,无法实例化.Path'hits.hits [0] ._ source.elasticShape.coordinates',第10行,第19位。 “}
我期待它是因为我使用的是Nest.GeoShape而不是明确的GeoShape类型(如EnvelopeGeoShape),但在我的情况下,每个文档都有不同的形状(5个可能是圆形,3个矩形,2个多边形,以及74分)。
那么有没有办法可以进一步控制Json反序列化以检查类型并显式映射它以生成特定类型?或者(理想情况下)有没有办法简单地让反序列化自动从类型字段中“弄明白”?
答案 0 :(得分:2)
好的,这是我发现的反序列化解决方案(问题#2)...
它需要编写CustomCreationConverter来处理可用于不同GeoShape类型的特定字段。以下是积分示例:
public class CustomNestGeoShapeConverter : CustomCreationConverter<Nest.GeoShape>
{
public override Nest.GeoShape Create(Type objectType)
{
return null;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JToken token = JToken.Load(reader);
if(token == null) return null;
switch (token["type"].ToString())
{
case "point":
{
var coords = new List<double>();
coords.Add(Double.Parse(token["coordinates"][0].ToString()));
coords.Add(Double.Parse(token["coordinates"][1].ToString()));
return new Nest.PointGeoShape() { Coordinates = coords };
}
}
return null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
然后,为了使用这个配置,我在课堂上设置了一个装饰器......
[JsonConverter(typeof(CustomNestGeoShapeConverter)), ElasticProperty(Index = FieldIndexOption.NotAnalyzed, Store = true, IncludeInAll = false)]
public Nest.GeoShape ElasticShape { get; set; }
这对我来说很有用,但是我仍然需要测试我是否可以搜索形状,即使Kibana认为该字段实际上没有被索引(问题#1)。