我需要过滤一个
Conversation
顶点,其中 senderId
和 receiverId
都必须与传出边 BelongsTo
匹配。我想象的查询应该是这样的:
public async Task<ConversationMessage?> SendMessageToUser(string senderId, string receiverId, string messageContentValue)
{
var conversation = gremlinQuerySource.V<Conversation>()
.Where(conversation => conversation.OutE<BelongsTo>()
.InV<Account>()
.Where(account => account.Id == senderId) &&
conversation.OutE<BelongsTo>()
.InV<Account>()
.Where(account => account.Id == receiverId))
.Debug();
}
我不确定如何使用 GremlinQ 来实现这一点。我已经在互联网上搜索了几个小时并向 ChatGPT 寻求答案,但没有任何运气。
没关系,我找到了答案。我必须为每个条件单独调用
Where
子句才能使遍历有效。如果我将条件分组在单个 Where
和 And
子句中,它会强制所有条件在同一遍历路径上匹配,这是不可能的,因为 senderId
指向一个帐户,而 receiverId
指向另一个帐户。编译后的原始代码如下所示:
conversation = await gremlinQuerySource
.V<Account>()
.Where(account => account.Id == senderId)
.OutE<Has>()
.InV<Conversation>()
.And(
query => query.Where(conversation => conversation.IsDirectMessage),
query => query.OutE<BelongsTo>().InV<Account>().Where(account => account.Id == senderId),
query => query.OutE<BelongsTo>().InV<Account>().Where(account => account.Id == receiverId))
.FirstAsync();
修改后的代码如下所示:
conversation = await gremlinQuerySource
.V<Account>()
.Where(account => account.Id == senderId)
.OutE<Has>()
.InV<Conversation>()
.Where(conversation => conversation.IsDirectMessage)
.Where(conversation => conversation.OutE<BelongsTo>().InV<Account>().Where(a => a.Id == senderId))
.Where(conversation => conversation.OutE<BelongsTo>().InV<Account>().Where(a => a.Id == receiverId))
.FirstAsync();