2013-03-07 69 views
2

如何爲LineString地理數據創建RavenDB空間索引?RavenDB LineString的空間索引

我想爲地理數據的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); 
       } 
      } 
     } 
    } 
} 

回答

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) 
        }; 
    } 
} 

,我沒有測試過這個公平的警告。

+0

感謝您的回覆。但不要認爲索引工作。以下查詢不是過濾數據。 – user2144147 2013-03-07 14:11:57

+0

streets = session.Query () .Customize(x => x.WithinRadiusOf(10,-4.06011,51.70934)) .ToList(); – user2144147 2013-03-07 14:12:15

+1

@ user2144147您沒有在查詢中指定索引。 'session.Query ()...'空間查詢需要一個靜態索引。 – 2013-03-07 20:32:46