如何在Gremlin控制台写查询返回含平行边的顶点对及Cypher转Gremlin
问题1:Gremlin查询存在平行边的顶点对
平行边指两个顶点之间存在2条及以上符合指定规则的边,通用查询语句如下,可根据需求调整规则:
// 按「出点ID+入点ID+边标签」分组,返回存在同方向同类型平行边的顶点对 g.E().groupCount(). by(union(outV().id(), inV().id(), label()).fold()). unfold(). filter(select(values).is(gt(1))). select(keys). project('outV_id', 'inV_id', 'edge_label'). by(select(keys).limit(local, 1)). by(select(keys).range(local, 1, 2)). by(select(keys).tail(local))
如果不需要区分边方向,可将分组规则里的outV().id(), inV().id()替换为union(outV().id(), inV().id()).order().by(id).fold(),这样a→b和b→a的边会被判定为同一组;如果不需要区分边类型,删除by参数里的label()即可。
问题2:Cypher语句转Gremlin
你给出的Cypher逻辑是查找互为好友的两个Person标签顶点,对应的Gremlin语句如下:
g.V().hasLabel('Person').as('n'). out('friend').hasLabel('Person').as('t'). out('friend').where(eq('n')). select('n', 't'). dedup()
末尾的dedup()用于避免返回重复顶点对(如「张三、李四」和「李四、张三」会被合并为一条结果),如果需要保留双向匹配结果可删除该步骤。
内容的提问来源于stack exchange,提问作者Doul Jack
相关产品推荐
相关产品推荐

