具有geo_shape字段的文档无法反序列化



我的索引包含一个类型为 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 类型的实例。类型是接口或抽象类,不能实例化。路径 'hits.hits[0]._source.elasticShape.坐标',第 10 行,位置 19。

我希望这是因为我使用的是Nest.GeoShape而不是明确的GeoShape类型(如EnvelopeGeoShape),但就我而言,每个文档将具有不同的形状(5可能是圆形,3个矩形,2个多边形和74个点)。

那么有没有办法进一步控制 Json 反序列化来检查类型并显式映射它以生成特定类型?或者(理想情况下)有没有办法简单地让反序列化自动从类型字段中"弄清楚"?

好的,这是我发现的反序列化解决方案(问题 #2)...

它需要编写一个自定义创建转换器来处理可用于不同 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)。

最新更新