【问题标题】:RavenDB Spatial index for LineStringLineString 的 RavenDB 空间索引
【发布时间】:2013-03-07 12:13:49
【问题描述】:

如何为 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);
                }
            }
        }
    }
}

【问题讨论】:

    标签: ravendb spatial-index


    【解决方案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)
                         };
        }
    }
    

    公平警告,我尚未对此进行测试。

    【讨论】:

    • 感谢您的回复。但不要认为索引有效。以下查询未过滤数据。
    • streets = session.Query() .Customize(x => x.WithinRadiusOf(10, -4.06011, 51.70934)) .ToList();
    • @user2144147 您没有在查询中指定索引。 session.Query&lt;YourObject, YourIndex&gt;()... 空间查询始终需要静态索引。
    猜你喜欢
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多