1

我正在使用 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 Attribute geo_point 类型修饰了 Location 属性。我能够为这种类型建立索引。但是当我尝试为此获取映射时,

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

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

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

我在这里错过了什么吗?

4

3 回答 3

3

下面是一个完整示例,说明如何使用 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 代码。

于 2015-04-15T10:33:22.090 回答
2

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

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

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

于 2014-02-04T13:06:15.927 回答
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))));
于 2015-08-10T06:26:56.323 回答