【问题标题】:Geo_Point Property not indexing as expectedGeo_Point 属性未按预期编制索引
【发布时间】:2014-02-04 12:37:05
【问题描述】:

我正在使用 NEST .net 客户端进行弹性搜索。我有一个带有 Location 属性的地址类。

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"
                    }
                }
            }
        }
    }
}

我有什么遗漏吗?

【问题讨论】:

    标签: c# geolocation elasticsearch nest geopoints


    【解决方案1】:

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

    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 代码。

    【讨论】:

    • IndexGeoHash() 属性不再存在。我错过了什么吗?
    【解决方案2】:

    请使用MapFluent() 方法指定映射。基于属性的映射在它可以表达的方面非常有限。在下一个版本中,MapFluent 将重命名为 Map,这将是指定映射的唯一方法。

    请参阅此示例以映射geo_point 类型:

    https://github.com/elasticsearch/elasticsearch-net/blob/master/src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs#L225

    【讨论】:

    • client.MapFluent
      (m => m.MapFromAttributes().Properties(p => p.GeoPoint(s => s.Name(n => n.Location).IndexGeoHash( ).IndexLatLon().GeoHashPrecision(12))));我这样做了,但仍然得到相同的结果。你能告诉我这是否正确吗?
    • 嗨 Martijn,我刚知道你是 NEST 的所有者。你能帮我解决这个问题吗?我在这个问题上停留了 1 天。我用的是Nest0.12.0版本
    • @Martijn Laarman 能否请您帮忙指出并更新示例。链接似乎已关闭。
    【解决方案3】:

    创建弹性客户端

    var uri = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(uri).SetDefaultIndex("my_indexone");
    var client = new ElasticClient(settings);
    client.CreateIndex("my_indexone", s => s.AddMapping<VehicleRecords>(f => f.MapFromAttributes().Properties(p => p.GeoPoint(g => g.Name(n => n.Coordinate ).IndexLatLon()))));
    

    创建下面的类

                  public class VehicleRecords
                    {
    
                        public string name { get; set; }
                        public Coordinate Coordinate { get; set; }
                        public double Distance { get; set; }
                    }
    
                    public class Coordinate
                    {
                        public double Lat { get; set; }
                        public double Lon { get; set; }
                    }
    
              Step 2 :Insert some record using above class
              Step 3 :Using below query to search....
    
    
     Nest.ISearchResponse<VehicleRecords> Response = client.Search<VehicleRecords>(s => s.Sort(sort => sort.OnField(f => f.Year).Ascending()).From(0).Size(10).Filter(fil => fil.GeoDistance(n => n.Coordinate, d => d.Distance(Convert.ToDouble(100), GeoUnit.Miles).Location(73.1233252, 36.2566525))));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 2021-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多