简体   繁体   English

如何编写Gremlin查询以查找具有指定边的父顶点?

[英]How to write Gremlin query to find parent vertices which have a specified edge?

I am a newbie to gremlin queries. 我是gremlin查询的新手。 I have a graph like below, and my source vertex is P3, I want to write a query that will get all the parent\\ancestor vertices (an vertex is parent\\ancetor of P3 if there is an path from that vertex to P3 with edges of type 'contains') of type 'Part' and have an Owner associated to them. 我有一个如下图,我的源顶点是P3,我想编写一个查询,该查询将获取所有父\\祖先顶点(如果该顶点到具有边缘的P3路径,则该顶点是P3的父\\祖先顶点类型为“零件”的“包含”),并具有与其相关联的所有者。 So in this case the query should return P1 and P2 but not P. 因此,在这种情况下,查询应返回P1和P2,但不返回P。

Query to create sample data: 查询以创建样本数据:

    g.addV(id, 'P1').property('label','part').as('p1')
.addV(id, 'P2').property('label','part').as('p2')
.addV(id, 'P3').property('label','part').as('p3')
.addV(id, 'P4').property('label','part').as('p4')
.addV(id, 'owner1').property('label','owner').as('o1')
.addV(id, 'owner2').property('label','owner').as('o2')
.addE('contains').from('p1').to('p2')
.addE('contains').from('p2').to('p3')
.addE('contains').from('p4').to('p3')
.addE('owns').from('o1').to('p1')
.addE('owns').from('o2').to('p2')

在此处输入图片说明

This is the query that I came up with, but the traversal stops once it finds a part vertex that has an owner vertex associated to it. 这是我提出的查询,但是一旦找到与之关联的所有者顶点的零件顶点,遍历就会停止。 How to update it to return both P1 and P2. 如何更新它以同时返回P1和P2。

g.V('P3')
           .union(
                            inE().hasLabel('owns').inV(),
                repeat(inE().hasLabel('contains')
                                            .outV().hasLabel('part'))
                                            .until(inE().hasLabel('owns'))
                ).dedup()

I also tried using a sideEffect step to collect part vertices but didn't get required result. 我也尝试使用sideEffect步骤来收集零件顶点,但是没有得到所需的结果。

g.V('P3').union(
inE().hasLabel('owns').inV(),
repeat(inE().sideEffect(hasLabel('owns').outV().as('parts'))
        .hasLabel('contains')
        .outV().hasLabel('part'))
)
.select('parts').dedup()

I revised your sample data code a bit as the syntax was wrong: 由于语法错误,我对示例数据代码进行了一些修改:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('part').property(id, 'P1').as('p1').
......1>   addV('part').property(id, 'P2').as('p2').
......2>   addV('part').property(id, 'P3').as('p3').
......3>   addV('part').property(id, 'P4').as('p4').
......4>   addV('owner').property(id, 'owner1').as('o1').
......5>   addV('owner').property(id, 'owner2').as('o2').
......6>   addE('contains').from('p1').to('p2').
......7>   addE('contains').from('p2').to('p3').
......8>   addE('contains').from('p4').to('p3').
......9>   addE('owns').from('o1').to('p1').
.....10>   addE('owns').from('o2').to('p2').iterate()

I think you can simplify your traversal to just be a simple repeat() : 我认为您可以将遍历简化为一个简单的repeat()

gremlin> g.V('P3').emit(inE('owns')).repeat(__.in('contains'))
==>v[P2]
==>v[P1]

Note the placement of the emit() step which controls the vertices that are output from the loop. 请注意emit()步骤的位置,该步骤控制从循环输出的顶点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个 gremlin 查询来查找其他顶点之间的公共顶点并按重叠计数排序返回? - How to write a gremlin query that finds common vertices between other vertices and return sorted by the count of overlap? Gremlin 查找具有给定值的“任何”属性的所有顶点 - Gremlin find all vertices that have “any” property with a given value Gremlin:在单个gremlin查询中添加多个顶点? - Gremlin : Add multiple vertices in single gremlin query? Gremlin查询展平嵌套顶点 - Gremlin query flatten nested vertices 如何在Gremlin / Tinkerpop 3中查询多个顶点及其关系计数? - How to query for multiple vertices and counts of their relationships in Gremlin/Tinkerpop 3? 如何在一个 Gremlin 查询中获取两个或多个顶点的属性? - How to get the properties of two or more vertices in one Gremlin query? 如何在两个有边的顶点之间插入一个顶点。 Azure Cosmos DB [GREMLIN API] - how to insert a vertex in between 2 vertices with edge. Azure Cosmos DB [GREMLIN API] 如何在Gremlin上“连接”两个顶点? - how to “join” two vertices on Gremlin? Gremlin - 如何确保一个顶点只有一个入站边? - Gremlin - how to ensure one vertex only have one inbound edge? Gremlin 找到顶点之间最轻的路径 - Gremlin find lightest path between vertices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM