我有一个场景,我想分批存储小文档,因为插入性能。但我当然希望能够搜索各个项目。
public class Batch
{
public Guid Id { get; set; }
public Item[] Items { get; set; }
}
public class Item
{
public int Id { get; set; }
public string Description { get; set; }
}
假设我想按描述搜索,并且我想要返回ID。我创建了以下非常简单的map / reduce索引,我希望这样做:
Map = batches =>
from batch in batches
from item in batch.Items
select new Result
{
Description = item.Description,
ItemIds = new[] {item.Id}
};
Reduce = results =>
from result in results
group result by result.Description into g
select new Result
{
Description = g.Key,
ItemIds = g.SelectMany(x => x.ItemIds).ToArray()
};
使用此索引的查询不会失败,它只会返回任何结果。
如果索引被修改了一点,使用string.Join而不是SelectMany来获取ID,一切正常。
Map = batches =>
from batch in batches
from item in batch.Items
select new Result
{
Description = item.Description,
ItemIdsAsString = item.Id.ToString(),
};
Reduce = results =>
from result in results
group result by result.Description into g
select new Result
{
Description = g.Key,
ItemIdsAsString = string.Join("|", g.Select(x => x.ItemIdsAsString)),
};
行为完全相同,只是ID现在存储为“2 | 5 | 7”而不是这些整数的数组。但这一次,索引产生了正确的结果。
为什么SelectMany在Reduce表达式中不起作用?