Geo_Point属性未按预期编制索引

时间:2014-02-04 12:37:06

标签: c# geolocation elasticsearch nest geopoints

我正在使用NEST .net客户端进行弹性搜索。我有一个带有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"
                    }
                }
            }
        }
    }
}

我在这里遗漏了什么吗?

3 个答案:

答案 0 :(得分:2)

请使用MapFluent()方法指定映射。基于属性的映射在它可以表达的内容方面受到很大限制。在下一个版本中,MapFluent将重命名为Map,它将是指定映射的唯一方法。

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

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

答案 1 :(得分:2)

以下是如何使用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代码。

答案 2 :(得分:0)

创建弹性客户端

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))));