C# 如何在RavenDB中的索引上使用GroupBy?
我有这个文件,一个帖子: {内容:blabla,标签:[测试,toto],CreatedOn:2019-05-01:02:01} 我想有一个页面,显示最常用的标签,因为过去30天 到目前为止,我试图创建这样一个索引C# 如何在RavenDB中的索引上使用GroupBy?,c#,ravendb,C#,Ravendb,我有这个文件,一个帖子: {内容:blabla,标签:[测试,toto],CreatedOn:2019-05-01:02:01} 我想有一个页面,显示最常用的标签,因为过去30天 到目前为止,我试图创建这样一个索引 public class Toss_TagPerDay : AbstractIndexCreationTask<TossEntity, TagByDayIndex> { public Toss_TagPerDay() { Map = t
public class Toss_TagPerDay : AbstractIndexCreationTask<TossEntity, TagByDayIndex>
{
public Toss_TagPerDay()
{
Map = tosses => from toss in tosses
from tag in toss.Tags
select new TagByDayIndex()
{
Tag = tag,
CreatedOn = toss.CreatedOn.Date,
Count = 1
};
Reduce = results => from result in results
group result by new { result.Tag, result.CreatedOn }
into g
select new TagByDayIndex()
{
Tag = g.Key.Tag,
CreatedOn = g.Key.CreatedOn,
Count = g.Sum(i => i.Count)
};
}
}
我这样质疑
await _session
.Query<TagByDayIndex, Toss_TagPerDay>()
.Where(i => i.CreatedOn >= firstDay)
.GroupBy(i => i.Tag)
.OrderByDescending(g => g.Sum(i => i.Count))
.Take(50)
.Select(t => new BestTagsResult()
{
CountLastMonth = t.Count(),
Tag = t.Key
})
.ToListAsync()
但这给了我一个错误
消息:System.NotSupportedException:无法理解表达式:来自索引'Toss/TagPerDay'。其中i=>Converti.CreatedOn,DateTimeOffset>=valueToss.Server.Models.Tows.BestTagsQueryHandler+c_uDisplayClass3\u 0.firstDay.GroupByi=>i.Tag.OrderByDescendingg=>g.Sumi=>i.Count.Take50.Selectt=>new BestTagsResult{CountLastMonth=t.Count,Tag=t.Key}
-System.NotSupportedException:GroupBy方法仅在动态map reduce查询中受支持
我可以查询过去30天的所有索引数据,并在内存中执行groupby/order/take,但这可能会使我的应用程序加载大量数据。出现错误的原因是,在对索引进行的查询中不能使用“groupby” 执行“动态查询”时可以使用“GroupBy” i、 e.对集合进行的查询,不指定索引 见:
您创建的map reduce索引的结果将为您提供每天的标记数。您希望拥有过去30天中最流行的标记,因此需要执行以下查询:
var tagCountPerDay = session
.Query<TagByDayIndex, Toss_TagPerDay>()
.Where(i => i.CreatedOn >= DateTime.Now.AddDays(-30))
.ToList();
我知道,但我怎样才能实现我在这里想要做的事情呢?
var mostUsedTags = tagCountPerDay.GroupBy(x => x.Tag)
.Select(t => new BestTagsResult()
{
CountLastMonth = t.Count(),
Tag = t.Key
})
.OrderByDescending(g => g.CountLastMonth)
.ToList();