Skip to main content
 首页 » 编程设计

C#嵌套 : How to index array of geo-poinst

2024年02月06日13telwanggs

你好,我是 elastic 和 nest 的新手。 我使用了一个如何索引地理点位置的示例,效果很好,因为我可以在 kibana map 可视化中看到地理点。

这是我的数据结构:

public class LocationArray 
{ 
    public string ArrayNameArtical { get; set; } 
    [ElasticProperty(Type = FieldType.GeoPoint)] 
    public IEnumerable<Location> Locations { get; set; } 
} 
public class Location 
{ 
    public string Name { get; set; } 
 
    [ElasticProperty(Type = FieldType.GeoPoint)] 
    public Coordinate Coordinate { get; set; } 
} 
 
 
public class Coordinate 
{ 
    public double Lat { get; set; } 
    public double Lon { get; set; } 
} 

我的索引代码如下:

var response = client.CreateIndex(indexName, s => s 
      .AddMapping<Location>(f => f 
        .MapFromAttributes()  
        .Properties(p => p 
          .GeoPoint(g => g.Name(n => n.Coordinate).IndexGeoHash().IndexLatLon()) 
        ) 
      ) 
    ); 
    client.IndexMany(new[]{ 
        new Location 
        { 
            Name = "Amsterdam", 
            Coordinate = new Coordinate { Lat =  52.3740300, Lon = 4.8896900} 
        }, 
        new Location 
        { 
            Name = "Rotterdam", 
            Coordinate = new Coordinate { Lat = 51.9225000, Lon = 4.4791700} 
        }, 
        new Location 
        { 
            Name = "Utrecht", 
            Coordinate = new Coordinate { Lat =  52.0908300,  Lon = 5.1222200} 
        },new Location 
        { 
            Name = "Den Haag", 
            Coordinate = new Coordinate { Lat =  52.3740300, Lon = 4.8896900} 
        } 
    }); 

现在我想索引 LocationArray 类,似乎需要更改我的映射,但我不知道该怎么做。无论如何我可以在 kibana 中看到数组数据,但无法通过 map 查看它。 geo-point 索引数组有问题吗?

请您参考如下方法:

好的,经过几个小时的挖掘,找到了映射这个地理点阵列的方法。 希望有一天它能帮助别人 :)

client.Map<LocationArray>(m => m 
                        .MapFromAttributes().Properties(p=>p 
                            .NestedObject<Location>(no => no 
                            .Name(pl => pl.Locations.First()) 
                            .Dynamic() 
                            .Enabled() 
                            .IncludeInAll() 
                            .IncludeInParent() 
                            .IncludeInRoot() 
                            .MapFromAttributes() 
                            .Path("full") 
                            .Properties(pprops => pprops 
                                .GeoPoint(ps => ps 
                                    .Name(pg => pg.Coordinate) 
                                    .IndexGeoHash().IndexLatLon() 
                                ) 
                            ) 
                        )) 
                    );