对搜索结果进行分组 - RavenDB

时间:2013-11-10 07:39:53

标签: c# full-text-search ravendb faceted-search

我有一个显示文本搜索结果的索引。结果始终只显示一个月。

我需要显示每个月有多少结果。示例位于底部。

我知道分面搜索应该可以完成这项任务,但是手动创建/生成范围是不可能的,并且会产生大量的范围。 我尝试了官方文档中的所有内容,甚至是不正确的ResultTransformers。所以我希望我一定忽视了一些事情。

Map = transactions => from transaction in transactions
select new
    {
        Description = new object[] { transaction.Description, transaction.Items.Select(i => i.Name), transaction.Documents.Select(i => i.Name) },
        Account_UserName = transaction.Account.UserName,
        Time = transaction.Time
    };

结果我期望是这样的:

 [{
    Year: 2013,
    Month: 12,
    Count: 3
 },
 {
    Year: 2013,
    Month: 11,
    Count: 10
 }]

2 个答案:

答案 0 :(得分:0)

您希望使用Month值标记每个索引记录并从那里开始:

Map = transactions => from transaction in transactions
select new
    {
        Description = new object[] { transaction.Description, transaction.Items.Select(i => i.Name), transaction.Documents.Select(i => i.Name) },
        Account_UserName = transaction.Account.UserName,
        Month = transaction.Time.Month + "/" + transaction.Time.Year, // To be able to group on Months
        Time = transaction.Time
    };

从那里你可以在Month字段中使用faceting,或者使用Reduce函数将Months与总数一起编入索引以使此计算可搜索

答案 1 :(得分:0)

经过一番挖掘后,我发现Dynamic aggregation将完成这项工作。我添加了字段月份,因为匿名对象不起作用。

Map = transactions => from transaction in transactions
select new
    {
        Value = transaction.Value,
        Description = new object[] { transaction.Description, transaction.Items.Select(i => i.Name) },
        Account_UserName = transaction.Account.UserName,
        Time = transaction.Time,
        Month = new DateTime(transaction.Time.Year, transaction.Time.Month, 1) // new field
    };

然后按.AggregateBy(x => x.Month).CountOn(x => x.Month);

扩展查询
var result = _session.Query<Transaction, Transaction_Search>()
    .AggregateBy(x => x.Month)
    .CountOn(x => x.Month);

经过一些转变,这会产生预期的结果。