如何为LineString地理数据创建RavenDB空间索引?
我正在尝试为地理数据的LINESTRING创建空间索引,但搜索查询不会返回任何数据。
请使用以下测试用例作为参考,因为我是RavenDb的新用户我不确定我的搜索查询是否正确或RavenDB上的错误
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Raven.Abstractions.Indexing;
using Raven.Client;
using Raven.Client.Embedded;
using Raven.Client.Indexes;
namespace GeoDataLoading.Test
{
[TestFixture]
public class SpatialTest
{
public class GeoDocument
{
public string WKT { get; set; }
}
public class GeoIndex : AbstractIndexCreationTask<GeoDocument>
{
public GeoIndex()
{
Map = docs => from doc in docs
select new {_ = SpatialGenerate("WKT", doc.WKT, SpatialSearchStrategy.GeohashPrefixTree)};
}
}
[Test]
public void LineStringsShouldNearest()
{
using (var store = new EmbeddableDocumentStore {RunInMemory = true})
{
store.Initialize();
store.ExecuteIndex(new GeoIndex());
using (IDocumentSession session = store.OpenSession())
{
session.Store(new GeoDocument
{
WKT =
"LINESTRING (-0.20854 51.80315, -0.20811 51.80395, -0.20811 51.80402, -0.20814 51.80407, -0.20823 51.80419, -0.20888 51.80435, -0.20978 51.80455, -0.21033 51.80463, -0.21088 51.80467, -0.2116 51.80463, -0.21199 51.80457, -0.21246 51.80453, -0.2131 51.80448, -0.21351 51.80442, -0.2143 51.80433, -0.21436 51.80372, -0.21454 51.80321, -0.21468 51.80295)"
});
session.SaveChanges();
}
using (IDocumentSession session = store.OpenSession())
{
List<GeoDocument> result = session.Advanced.LuceneQuery<GeoDocument>("GeoIndex")
.WaitForNonStaleResults()
.WithinRadiusOf(1.2, -0.20854f, 51.80315f)
.SortByDistance()
.ToList();
Assert.IsTrue(result.Count > 0);
}
}
}
}
}
答案 0 :(得分:1)
public class YourDocumentType_SpatialIndex : AbstractIndexCreationTask<YourDocumentType>
{
public SpatialIndex()
{
Map = documents => from document in documents
select new
{
document.LinkId,
_ = SpatialGenerate(fieldName: "Geometry", shapeWKT: document.Geometry, strategy: SpatialSearchStrategy.GeohashPrefixTree, maxTreeLevel: 12)
};
}
}
公平警告,我没有测试过这个。