Skip to main content
 首页 » 编程设计

c#之Geo_Point 属性未按预期编制索引

2024年02月13日16落叶无声

我正在使用 NEST .net 客户端进行 Elastic Search。我有一个带有 Location 属性的 Address 类。

public class Address 
{ 
public string AddressLine1 {get;set;} 
public string AddressLine2 {get;set;} 
public string City {get;set;} 
public string State {get;set;} 
public string ZipCode {get;set;} 
[ElasticProperty(Type = FieldType.geo_point)] 
public GeoLocation Location {get;set;} 
} 
 
 
public class GeoLocation 
{ 
public float Lat {get;set;} 
public float Lon {get;set;} 
} 

我用 ElasticProperty 属性 geo_point 类型修饰了 Location 属性。我能够为这种类型建立索引。但是当我尝试为此获取映射时,

http://localhost:9200/mysite/Address/_mapping 

我希望 Location 属性是“geo_point”类型,而不是它显示类似这样的内容。

{ 
    "Address": { 
        "properties": {                 
            "location": { 
                "properties": { 
                    "lat": { 
                        "type": "double" 
                    }, 
                    "lon": { 
                        "type": "double" 
                    } 
                } 
            } 
        } 
    } 
} 

我在这里遗漏了什么吗?

请您参考如下方法:

这是一个完整的示例,说明如何使用 Nest 映射 GeoPoint 以及设置其他属性。希望对 future 的人有所帮助。

public class Location 
{ 
    public decimal Lat { get; set; } 
    public decimal Lon { get; set; } 
} 
 
public class Building : BaseEntity 
{ 
    public IEnumerable<Location> Location { get; set; } 
} 
 
public void CreateBuildingMapping() 
{ 
    var nodes = new List<Uri>() { ... }; 
    var connectionPool = new Elasticsearch.Net.ConnectionPool.SniffingConnectionPool(nodes); 
    var connectionSettings = new Nest.ConnectionSettings(connectionPool, "myIndexName"); 
    var elasticsearchClient = new Nest.ElasticClient(connectionSettings); 
 
    var putMappingDescriptor = new Nest.PutMappingDescriptor<Building>(connectionSettings); 
    putMappingDescriptor.DateDetection(false); 
    putMappingDescriptor.Dynamic(false); 
    putMappingDescriptor.IncludeInAll(true); 
    putMappingDescriptor.IgnoreConflicts(false); 
    putMappingDescriptor.Index("myIndexName"); 
    putMappingDescriptor.MapFromAttributes(); 
    putMappingDescriptor 
        .MapFromAttributes() 
            .Properties(p => 
                p.GeoPoint(s => 
                    s.Name(n => n.Location).IndexGeoHash().IndexLatLon().GeoHashPrecision(12) 
                ) 
            ); 
    putMappingDescriptor.NumericDetection(false); 
 
    elasticsearchClient.Map(putMappingDescriptor); 
} 

我使用了他评论中的 Vinoths 代码。