Indexing 返回一个用户以及分页的关注者集合
我仍在学习,所以可能我的模型目前是错误的,但这是我目前所掌握的:Indexing 返回一个用户以及分页的关注者集合,indexing,ravendb,Indexing,Ravendb,我仍在学习,所以可能我的模型目前是错误的,但这是我目前所掌握的: Account { string Id, string ArtistName, List<FollowerAccount> Followers } FollowerAccount { AccountId, DateBeganFollowing } 帐户 { 字符串Id, 字符串艺术家姓名, 列出追随者 } 跟随计数 { 帐户ID, 日期如下 } 因此,我的帐户文档包含一个非规范化引用列表,该列表列出了跟
Account
{
string Id,
string ArtistName,
List<FollowerAccount> Followers
}
FollowerAccount
{
AccountId,
DateBeganFollowing
}
帐户
{
字符串Id,
字符串艺术家姓名,
列出追随者
}
跟随计数
{
帐户ID,
日期如下
}
因此,我的帐户文档包含一个非规范化引用列表,该列表列出了跟随它们的所有帐户
我现在想从“accounts/1”Followers列表中返回一个帐户列表,但对它们进行分页,我知道我可以作为2个查询来执行此操作,但我希望我可以确定这2个1个查询
这是一个我一直在玩的索引,但是我无法让它工作
public class TestIndex : AbstractMultiMapIndexCreationTask<TestIndex.ReduceResult>
{
public class ReduceResult
{
public string AccountId { get; set; }
public DateTimeOffset? DateBecameFollower { get; set; }
public string ParentAccountId { get; set; }
public string ArtistName { get; set; }
}
public TestIndex()
{
AddMap<Account>(followers => from follower in followers
from sub in follower.FollowersAccounts
select new
{
ParentAccountId = follower.Id,
AccountId = sub.AccountId,
DateBecameFollower = sub.DataBecameFollower,
ArtistName = (string)null
});
AddMap<Account>(accounts => from account in accounts
select new
{
ParentAccountId = (string)null,
AccountId = account.Id,
DateBecameFollower = DateTimeOffset.MinValue,
ArtistName = account.ArtistName,
});
Reduce = results => from result in results
group result by result.AccountId
into g
select new
{
ParentAccountId = g.Select(x => x.ParentAccountId).Where(x => x != null).First(),
AccountId = g.Key,
DateBecameFollower = g.Select(x => x.DateBecameFollower).Where(x => x != DateTimeOffset.MinValue).First(),
ArtistName = g.Select(x => x.ArtistName).Where(x => x != null).First()
};
}
}
公共类TestIndex:AbstractMultiMapIndexCreationTask
{
公共类ReduceResult
{
公共字符串AccountId{get;set;}
公共DateTimeOffset?DateBecameFollower{get;set;}
公共字符串ParentAccountId{get;set;}
公共字符串ArtistName{get;set;}
}
公共测试索引()
{
AddMap(追随者=>来自追随者中的追随者
从followers.followers帐户中的子帐户
选择新的
{
ParentAccountId=follower.Id,
AccountId=子AccountId,
DateBecameFollower=sub.DataBecameFollower,
ArtistName=(字符串)null
});
AddMap(accounts=>来自accounts中的account
选择新的
{
ParentAccountId=(字符串)null,
AccountId=account.Id,
DateBecameFollower=DateTimeOffset.MinValue,
艺人名称=account.ArtistName,
});
Reduce=results=>from result in results
按result.AccountId对结果进行分组
进入g
选择新的
{
ParentAccountId=g.Select(x=>x.ParentAccountId)。其中(x=>x!=null)。First(),
AccountId=g.键,
DateBecameFollower=g.Select(x=>x.DateBecameFollower)。其中(x=>x!=DateTimeOffset.MinValue)。First(),
ArtistName=g.Select(x=>x.ArtistName)。其中(x=>x!=null)。First()
};
}
}
RavenDB无法在单个文档的项目内翻页,因为它将文档视为聚合。因此,您可以分页完成文档(使用Skip/Take),但不能在单个帐户文档中分页跟随者
因此,您必须求助于2个数据库调用,一个用于获取帐户,一个用于查询追随者如何跟踪该帐户
但是,如果使用RavenDB的延迟请求功能,则可以节省网络往返时间。有关更多信息,请参阅和
var lazyUser = session.Advanced.Lazily.Load<User>("users/ayende");
var lazyPosts = session.Query<Posts>().Take(30).Lazily();
var lazyUser=session.Advanced.lazyly.Load(“users/ayende”);
var lazyPosts=session.Query().Take(30.lazyly();
另一件事要记住,你希望一个帐户有多少追随者?如果数量很大,在RavenDB中存储如此大的文档时可能会遇到性能问题。反序列化非常大的文档的开销可能会成为一个问题。经过思考后,我确实改变了主意,将关注者移动到一个单独的文档中,现在我可以非常轻松地执行分页,而且它的扩展性也应该很好。