0

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

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

我知道分面搜索应该可以完成这项工作,但是手动创建/生成范围是不可能的,它会导致大量范围。我尝试了官方文档中的所有内容,甚至 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
 }]
4

2 回答 2

0

经过一番挖掘,我发现动态聚合可以完成这项工作。我添加了字段月份,因为匿名对象不起作用。

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);

经过一些转换后,这会产生所需的结果。

于 2013-11-22T08:31:13.530 回答
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 与总计索引以使该计算可搜索

于 2013-11-10T09:14:55.630 回答