GremlinQ:如何根据多个条件过滤顶点

问题描述 投票:0回答:1

我需要过滤一个

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 寻求答案,但没有任何运气。

gremlin gremlinnet
1个回答
0
投票

没关系,我找到了答案。我必须为每个条件单独调用

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();
© www.soinside.com 2019 - 2024. All rights reserved.