Gremlin图遍历:如何基于block边过滤发布者屏蔽用户的评论
正确Gremlin遍历实现
你可以使用如下可正常运行的遍历语句实现需求:
g.V().hasLabel("comment") .as("comment_node") // 获取当前评论的发布者(评论者) .in("comment").hasLabel("user").as("commenter") // 回到评论节点,关联到对应帖子的作者 .select("comment_node") .out("comment").hasLabel("post") .in("post").hasLabel("user").as("post_publisher") // 排除帖子作者屏蔽了评论者的情况 .not(__.out("block").where(eq("commenter"))) // 返回符合要求的评论节点,去重避免重复结果 .select("comment_node") .dedup()
原代码问题说明
你之前的代码无法运行主要有两个核心问题:
- 代码中引用了未声明的别名
commentOwner,遍历时找不到对应节点直接报错 hasId(__.select("commentOwner").id())的写法不符合Gremlin语法规范,节点匹配应该使用where(eq())语法实现
简化版实现
如果不需要保留中间别名,也可以用更简洁的写法:
g.V().hasLabel("comment") .not( __.as("c") .in("comment").id().as("commenter_id") .select("c").out("comment").in("post") .out("block").id().where(eq("commenter_id")) )
内容的提问来源于stack exchange,提问作者Fook
相关产品推荐
相关产品推荐

