首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在gremlin查询中使用多个联合步骤?

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

Stack Overflow用户
提问于 2021-10-27 17:38:06
回答 2查看 344关注 0票数 0

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

代码语言:javascript
运行
复制
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 *

在上面的密码查询中,源顶点是Group,它有id '123',所以对于传入和输出边缘,我创建了下面的gremlin查询。

代码语言:javascript
运行
复制
g.V().hasLabel('Group').has('id',123)
 union(
   __.inE('Member_Of').values('Name'), 
   __.outE('Member_Of').values('Name'))
.path()  

现在,我必须遍历顶点的传入边缘,在上面的查询中,这个顶点是源顶点的传入顶点,在这里,我混淆了联合语法。

请帮助,谢谢:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-02 16:19:10

下面是用gremlin查询替换密码查询的全部内容。

代码语言:javascript
运行
复制
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())
票数 0
EN

Stack Overflow用户

发布于 2021-10-28 15:32:46

Cypher查询的这一部分

代码语言:javascript
运行
复制
match d=(s:Group{id:123})<-[r:Member_Of*]-(p:Group)<-[r1:Member_Of*]-(c:Person)

在Gremlin中,可以表示为

代码语言:javascript
运行
复制
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值。这使得查询效率大大提高,因为您可以使用以下内容:

代码语言:javascript
运行
复制
g.V('123')

直接找到一个顶点。

如果没有完全解除您的障碍,请在下面添加一个注释。

更新: 2021-10-29

我使用air数据集测试查询模式是否有效,使用以下查询:

代码语言:javascript
运行
复制
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())
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69743181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档