问题描述
帮助理解 RavenDB 查询,我使用以下模型来存储和索引空间数据。
public class GeoRecord {
public string SearchId {get; set;}
public string Tag {get; set;}
public double Latitude {get; set;}
public double Longitude {get; set;}
}
public class GeoRecord_GeoIndex : AbstractIndexCreationTask<GeoRecord>
{
public GeoRecord_GeoIndex()
{
Map = records => from @record in records
select new
{
IndexName = "geoIndex",record.SearchId,Coordinates = CreateSpatialField(@record.Latitude,@record.Longitude)
};
}
}
我可以使用如下所示的空间查询过滤所有 GeoRecord:
await session
.Query<GeoRecord,GeoRecord_GeoIndex>()
.Spatial("Coordinates",factory => factory.Within(shapeWkt: wkt))
.ToListAsync();
但是我想通过 SearchId 和 Coordinates 进行过滤,我得到了这个解决方案,但是我想了解它是否使用 GeoRecord_GeoIndex 而不是过滤来自 GeoRecord 的结果。
await session.Advanced.AsyncDocumentQuery<GeoRecord,GeoRecord_GeoIndex>()
.WhereIn("SearchId",activeSearchIds)
.Intersect()
.Spatial("Coordinates",criteria => criteria.Within(shapeWkt:wkt))
.ToListAsync();
解决方法
您正在查询索引 GeoRecord_GeoIndex
,它是用于过滤的索引。
静态索引包含:
- Map 函数中指定的每个索引字段的索引词列表,
(您的索引字段是:IndexName、SearchId 和 Coordinates) - 到相关文档的映射
在查询时,索引词会根据您的查询进行过滤,并从数据库中提取相关文档
一些演示链接:
https://demo.ravendb.net/demos/csharp/static-indexes/map-index#step-3