gpt4 book ai didi

.net - Nest(Geo)Location的经度始终为0?

转载 作者:行者123 更新时间:2023-12-02 22:24:58 30 4
gpt4 key购买 nike

我一直在深入研究将NEST用于将使用ElasticSearch的基于.Net的项目,但是让我感到困惑的是,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。
看来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的文档说:

The filter requires the geo_point type to be set on the relevant field.



这就是为什么我将 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)))
);

希望这可以帮助 :)

关于.net - Nest(Geo)Location的经度始终为0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29332016/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com