如何在 gremlin 查询中使用多个联合步骤?

How to use multiple union step in gremlin query?

我想在 gremlin 中转换以下密码查询,但在使用联合时遇到问题。

match d=(s:Group{id:123})<-[r:Member_Of*]-(p:Person) with d,
RELATIONSHIPS(d) as rels
WHERE NONE(rel in rels WHERE EXISTS(rel.`Ceased On`))
return *

UNION

match d=(s:Group{id:123})<-[r:Member_Of*]-(p:Group)-[r1:
Member_Of*]->(c:Group) with d,
RELATIONSHIPS(d) as rels
WHERE NONE(rel in rels WHERE EXISTS(rel.`Ceased On`))
return *

UNION

match d=(c:Group{id:123})-[r:Member_Of*]->(p:Group) with d,
RELATIONSHIPS(d) as rels
WHERE NONE(rel in rels WHERE EXISTS(rel.`Ceased On`))
return *

UNION

match d=(s:Group{id:123})<-[r:Member_Of*]-(p:Group)<-[r1:
Member_Of*]-(c:Person) with d,
RELATIONSHIPS(d) as rels
where NONE(rel in rels WHERE EXISTS(rel.`Ceased On`))
return *

在上面的密码查询中,源顶点是组,其 ID 为“123”,因此对于传入和传出边,我创建了以下 gremlin 查询。

g.V().hasLabel('Group').has('id',123)
 union(
   __.inE('Member_Of').values('Name'), 
   __.outE('Member_Of').values('Name'))
.path()  

现在我必须遍历顶点的传入边,这是上面查询中源顶点的传入顶点,我对联合语法感到困惑。

请帮忙,谢谢:)

这部分Cypher查询

match d=(s:Group{id:123})<-[r:Member_Of*]-(p:Group)<-[r1:Member_Of*]-(c:Person)

在Gremlin中,可以表示为

g.V().has('Group','id',123).
      repeat(inE('Member_Of').outV()).until(hasLabel('G')).
      repeat(inE('Member_Of').outV()).until(hasLabel('Person')).
      path().
        by(elementMap())

如果实际上没有涉及多跳(即在 Cypher 中,如果您真的不需要'*'),您可以删除 repeat 结构并保留 inEoutV 步骤。无论如何,这一点 Gremlin 将为您提供节点和边(关系)及其所有属性等,因为路径将包含它们的 elementMap.

请注意,从 Cypher 到 Gremlin 的直接端口可能无法充分利用目标数据库。例如,许多支持 Gremlin 的商店允许用户提供(真实的)ID 值。这使得查询效率更高,因为您可以使用这样的东西:

g.V('123')

直接找顶点。

如果这不能完全解除阻止,请在下面添加评论。

更新时间:2021-10-29

我使用航线数据集来测试查询模式是否有效,使用此查询:

g.V().has('airport','code','AUS').
      repeat(inE('contains').outV()).until(hasLabel('country')).limit(1).
      repeat(outE('contains').inV()).until(hasLabel('airport')).limit(3).
      path().
        by(elementMap())

以下是用 gremlin 查询完全替换密码查询。

g.V().has('Group','id',123). //source vertex 
      union(
           //Following represent incoming Member_Of edge towards source vertex from Person Node, untill the last node in the chain
          repeat(inE('Member_Of').outV()).until(hasLabel('Person')),
          //Following represent incoming Member_Of edge from Group to source vertex and edge outwards from same Group, untill the last node in the chain
          repeat(inE('Member_Of').outV().hasLabel('Group').simplePath()).until(inE().count().is(0)).
              repeat(outE('Member_Of').inV().hasLabel('Group').simplePath()).until(inE().count().is(0)),
          //Following represent outgoing Member_Of edge from source vertex to another Group node, untill the last node in the chain
          repeat(outE('Member_Of').inV().hasLabel('Group').simplePath()).until(outE().count().is(0)),
          //Following represent incoming Member_Of edge from Group to source vertex and incoming edge from person to Group untill the last node in the chain
          repeat(inE('Member_Of').outV().hasLabel('Group').simplePath()).until(inE().count().is(0)).
              repeat(inE('Member_Of').outV().hasLabel('Person').simplePath()).until(inE().count().is(0))
      ).
      path().by(elementMap())