给定这样的模型:
public class Location
{
public int Id { get; set; }
public string Name { get; set; }
public Coordinates Geo { get; set; }
public IList<Review> Reviews { get; set; }
}
public class Review
{
public string Comment { get; set; }
public int Rating { get; set; }
}
如何编写一个查询来返回按评级降序排序的匹配位置?
我有一个索引,可让我查询地理点周围一定半径内的位置:
public class Locations_ByCoordinates : AbstractIndexCreationTask<Location>
{
public Locations_ByCoordinates()
{
Map = locations => from l in locations
select new {_ = SpatialIndex.Generate(l.Geo.Lat, l.Geo.Lon)};
}
}
这似乎有效,我可以像这样对结果进行排序:
RavenQueryStatistics stats;
var query = session.Advanced.LuceneQuery<Location>(Names.Indexes.LocationsByCoordinates)
.Statistics(out stats)
.WithinRadiusOf(radius, coordinates.Lat, coordinates.Lon);
switch (sort)
{
case Sort.Distance:
query = query.SortByDistance();
break;
case Sort.Rating:
query = query.AddOrder(l => l.Reviews.Average(r => r.Rating), true); // TODO: This throws an exception, perhaps a modified index with an aggregate is required? Maybe Map/Reduce?
break;
case Sort.Alphabetical:
query = query.OrderBy(l => l.Name);
break;
case Sort.Number_of_ratings:
query = query.AddOrder(l => l.Reviews.Count, true);
break;
case Sort.Relevance: // This is the default ordering in RavenDB.
break;
default:
throw new ArgumentOutOfRangeException("sort");
}
除了按平均评分排序之外,每个选项都可以正常工作,这会引发有关未知字段的异常。我觉得这必须是 Map/Reduce 索引而不是 Map 索引。但我不知道怎么写。
我正在从 MongoDB 迁移此代码,但我曾经在客户端通过评级对文档进行排序,我想我现在会在服务器端执行此操作。