2

我一直在深入研究将 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'?!
        }
    }
}
4

1 回答 1

2

1. 看起来GeoLocation类型已经过时了。甚至NEST 测试也使用一个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_point 类型。

这就是为什么我将Locationtype 设置为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

索引的 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)))
    );

希望这可以帮助 :)

于 2015-03-29T18:43:32.437 回答