Nest(Geo)位置的经度总是0?

时间:2015-03-29 16:55:35

标签: .net elasticsearch nest elasticsearch-net

我一直在深入研究使用NEST进行基于.Net的项目,该项目将使用ElasticSearch,但令我困惑的是GeoDistance查询从未返回任何结果。

当调试简单的“*”查询的响应并查看搜索结果的.Documents时,所有文档实例的经度值都为0.0 - 纬度是正确的。

这是一个简单的ES服务器,它是新的(下载和运行),没有(重新)配置..与FacetFlow托管的一样。

对于版本,它们对于Elasticsearch.Net和NEST都是1.4.3,ElasticSearch本身是版本1.4.4。

我在这里或更准确地说是否有任何遗漏 - 我在这里缺少什么?

示例代码如下所示(下面使用的 GeoLocation 类是 Nest.GeoLocation 一个):

using System;
using System.Linq;
using Nest;

namespace NestPlayground
{
    public class Post
    {
        public Guid Id { get; set; }
        public string User { get; set; }
        public DateTime CreatedAt { get; set; }
        public string Message { get; set; }
        public GeoLocation Location { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var indexName = "sampleindex";

            var uri = new Uri("<elasticsearch url>");
            var settings = new ConnectionSettings(uri).SetDefaultIndex(indexName);
            var client = new ElasticClient(settings);

            client.DeleteIndex(indexName);

            var post = new Post
            {
                Id = Guid.NewGuid(),
                User = "Some User",
                CreatedAt = DateTime.UtcNow,
                Message = "Some Sample Message",
                Location = new GeoLocation(37.809860, -122.476995)
            };

            client.Index(post);
            client.Refresh();

            // Execute a search using the connection from above.

            var result = client.Search<Post>(s => s
                               .Index(indexName)
                               .Query(queryDescriptor => queryDescriptor.QueryString(queryStringQueryDescriptor => queryStringQueryDescriptor.Query("*")))
                               //.Filter(filterDescriptor => filterDescriptor.GeoDistance(post1 => post1.Location, geoDistanceFilterDescriptor => geoDistanceFilterDescriptor
                               //    .Distance(50, GeoUnit.Kilometers)
                               //    .Location(Lat: 37.802774, Lon: -122.4478561)
                               //    .Optimize(GeoOptimizeBBox.Indexed)))
                               );

            // this DOES return the just created/indexed document, but its .Longitude / result.Documents.First().Location.Longtitude property is always '0'?!
        }
    }
}

1 个答案:

答案 0 :(得分:2)

1。 看起来GeoLocation类型已过期。即使NEST tests使用CustomGeoLocation类。

所以你的Post课应该是这样的:

public class Post
{
    public Guid Id { get; set; }
    public string User { get; set; }
    public DateTime CreatedAt { get; set; }
    public string Message { get; set; }
    [ElasticProperty(Type = FieldType.GeoPoint)]
    public Location Location { get; set; }
}

public class Location
{
    public Location(double lat, double lon)
    {
        Lat = lat;
        Lon = lon;
    }

    public double Lat { get; set; }
    public double Lon { get; set; }
}

2。 Geo Distance Filter的文档说:

  

过滤器需要在相关设置上设置geo_point类型   字段。

这就是我将Location类型设置为FieldType.GeoPoint的原因。

请记住为索引创建映射。

client.CreateIndex(
    descriptor =>
        descriptor.Index(indexName)
            .AddMapping<Post>(
                m => m.Properties(p => p
                    .GeoPoint(mappingDescriptor => mappingDescriptor.Name(f => f.Location).IndexLatLon()))));

我开启了lat_lon,因为您想在GeoDistanceFilter中使用GeoOptimizeBBox.Indexed

索引的ES映射:

{
    "sampleindex" : {
        "mappings" : {
            "post" : {
                "properties" : {
                    "createdAt" : {
                        "type" : "date",
                        "format" : "dateOptionalTime"
                    },
                    "id" : {
                        "type" : "string"
                    },
                    "location" : {
                        "type" : "geo_point",
                        "lat_lon" : true
                    },
                    "message" : {
                        "type" : "string"
                    },
                    "user" : {
                        "type" : "string"
                    }
                }
            }
        }
    }
}

3。 现在这个查询终于可以了

var result = client.Search<Post>(s => s
    .Index(indexName)
    .Query(
        queryDescriptor => queryDescriptor.QueryString(queryStringQueryDescriptor => queryStringQueryDescriptor.Query("*")))
    .Filter(
        filterDescriptor =>
            filterDescriptor.GeoDistance(post1 => post1.Location, geoDistanceFilterDescriptor => geoDistanceFilterDescriptor
                .Distance(500, GeoUnit.Kilometers)
                .Location(37.802774, -122.4478561)
                .Optimize(GeoOptimizeBBox.Indexed)))
    );

希望这会有所帮助:)