使用类型间接连接顶点

Joining vertices with type indirection

我想使用 gremlin 在两个顶点类型之间进行连接

select * from type1 inner join type2 in type2.id = type1.type2_id

使用 type1 和 type2 作为顶点标签时,以下工作:

g.V()
  .hasLabel("type2").as("t2")
  .inE("hasJoin")
  .hasLabel("type1").as("t1")
  .select("t1", "t2")

但是,我的图表没有使用顶点标签来表示类型,而是使用通过 "hasType" 边连接的另一个顶点。

g.V()//
    .addV("instance1").as("instance1")//
    .addV("instance2").as("instance2")//
    .addV("type1").as("type1")//
    .addV("type2").as("type2")//
    .addE("hasType").from("instance1").to("type1")//
    .addE("hasType").from("instance2").to("type2")//
    .addE("hasJoin").from("instance1").to("instance2")//
    .iterate();

我需要做一些类似替换的事情

hasLabel("type2").as("t2")

hasLabel("type2").inE("hasType").outV().as("t2"):

这将导致

g.V()
  .hasLabel("type2").inE("hasType").outV().as("t2")
  .inE("hasJoin")
  .hasLabel("type1").inE("hasType").outV().as("t1")
  .select("t1", "t2")

这适用于 "t2",但不适用于 "t1",因为 .inE("hasJoin").hasLabel("type1") 是错误的。加入"t1"和"t2"需要用什么函数?

您只需要一个检查相邻类型顶点的过滤器。这是您的示例图(您的脚本不太有效):

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV("instance1").property("name","instance1").as("instance1").
......1>   addV("instance2").property("name","instance2").as("instance2").
......2>   addV("type1").as("type1").
......3>   addV("type2").as("type2").
......4>   addE("hasType").from("instance1").to("type1").
......5>   addE("hasType").from("instance2").to("type2").
......6>   addE("hasJoin").from("instance1").to("instance2").
......7>   iterate()

您要查找的查询应该是这样的:

gremlin> g.V().hasLabel("type2").in("hasType").as("t2").
           both("hasJoin").
           filter(out("hasType").hasLabel("type1")).as("t1").
           select("t1", "t2").
             by("name")
==>[t1:instance1,t2:instance2]