如何正确构建RavenDB空间索引,同时还检查是否有任何List <objects>匹配?

Cha*_*eus 6 c# linq ravendb

按照RevenDB网站上的示例,我想出了这个:

public class Place_ByLocationsAndCategoryId : AbstractIndexCreationTask<Place> {
    public Place_ByLocationsAndCategoryId() {
        Map = places => from p in places
                                select new { p.Categories, _ = Raven.Database.Indexing.SpatialIndex.Generate(p.Location.Lat, p.Location.Lng) };
    }
}
Run Code Online (Sandbox Code Playgroud)

地方看起来像这样:

Place {
    string Id;
    List<Category> Categories;
    ...
}
Run Code Online (Sandbox Code Playgroud)

类别看起来像这样:

Category {
    string Id;
    string Name;
    ...
}
Run Code Online (Sandbox Code Playgroud)

如何构建索引以便我可以在lat/lng的给定半径内的位置进行查询,并包含给定的类别?

我试图像这样查询上面的索引:

 var placesFromDb = RavenSession.Advanced.LuceneQuery<Place>("Place/ByLocationsAndCategoryId").WhereIn("Id", new []{cat.Id}).WithinRadiusOf(radius: 5, latitude: Lat, longitude: Lng).ToList<Place>();
Run Code Online (Sandbox Code Playgroud)

查询进程,但没有返回任何结果(当我知道有结果要返回.我的错误可能在.WhereIn()语句中,但当我尝试使用时,.Where(x => x.Categories.Any(c => c.Id == id))我得到一个构建错误,说明它已经过时了.


--Update--

我把我的索引切换到了这个(正如Ayende推荐的那样)

public Place_ByLocationsAndCategoryId() {
    Map = places => from p in places
                    select new { Categories_Id = p.Categories.Select(x => x.Id), _ = Raven.Database.Indexing.SpatialIndex.Generate(p.Location.Lat, p.Location.Lng) };
}
Run Code Online (Sandbox Code Playgroud)

我在文档存储上创建索引,如下所示:

IndexCreation.CreateIndexes(typeof(Place_ByLocationsAndCategoryId).Assembly, Store);
Run Code Online (Sandbox Code Playgroud)

我这样查询:

var placesFromDb = RavenSession.Advanced.LuceneQuery<Place>("Place/ByLocationsAndCategoryId").WhereEquals("Categories_Id ", cat.Id).WithinRadiusOf(radius: 15, latitude: Lat, longitude: Lng).ToList<Place>();
Run Code Online (Sandbox Code Playgroud)

这就是RavenDB本身的索引:

docs.Places
    .Select(p => new {Categories_Id = p.Categories
    .Select(x => x.Id), _ = SpatialIndex.Generate(((System.Double)(p.Location.Lat)), ((System.Double)(p.Location.Lng)))})
Run Code Online (Sandbox Code Playgroud)

查询似乎运行,但返回0结果(我知道有找到的地方).我注意到的一件事是我的模型中的Lat和Lng数据类型是浮点数,而看起来索引设置为Double.

试图像这样在索引中转换坐标,也不起作用:

public Place_ByLocationsAndCategoryId() {
    Map = places => from p in places
                    select new { Categories_Id = p.Categories.Select(x => x.Id), _ = Raven.Database.Indexing.SpatialIndex.Generate((float)p.Location.Lat, (float)p.Location.Lng) };
}
Run Code Online (Sandbox Code Playgroud)

所以我改变模型使用双打; 仍然返回0结果.


- 更新2--

如果我删除索引的类别部分,并仅查询空间部分,则返回位置.似乎类别部分未按计划运行.

如果它有帮助,这里是存储文档看起来的部分方式(JSON视图):

{
  "Id": "4dca6d56d22da18f4e626f54",
  "Name": "??",
  "Categories": [
    {
      "PlaceCategories": null,
      "Name": "Korean Restaurant",
      "Icon": "korean.png",
      "Id": "4bf58dd8d48988d113941735",
      "IsPrimary": true
    }
  ],
  "Location": {
    "Lat": "35.6709824",
    "Lng": "139.374588"
  },
  ...
}
Run Code Online (Sandbox Code Playgroud)

(注意:类别是List,类别可以有子类别,在他们自己的名为PlaceCategories的List属性中)


- 更新3--

这是管理工作室的索引错误:

Places/ByLocationsAndCategoryId 
Cannot convert type 'string' to 'float' 
6/27/2012 
places/1025
... and repeats 50 times
Run Code Online (Sandbox Code Playgroud)

我已经将我的模型从float更改为double,因为它看起来像空间生成器需要双倍.很有可能日志中有一些错误(看不到过去的50),说明"无法将类型'字符串'转换为'浮动'

我很好奇,在我以前的模型中,我的Lat/Lng是漂浮物.字符串来自哪里?

Cha*_*eus 0

原来这是我的模型。为了将来的参考,如果你想在 Raven 中使用空间索引,你必须将你的坐标存储为 double (我的最初是 float )。我确实更改为双精度,但必须编写一个脚本来遍历数据库中的所有文档并将坐标转换为双精度。

在那之后,他的工作就像冠军一样。