Gremlin TinkerPop:用于组合和过滤多个遍历的通用查询

Gremlin TinkerPop:用于组合和过滤多个遍历的通用查询,gremlin,tinkerpop,tinkerpop3,gremlin-server,Gremlin,Tinkerpop,Tinkerpop3,Gremlin Server,样本数据: 条件: vadas是否在2跳内连接到lop vadas是否在3跳内连接到peter 是否连接到的vadas在1个跃点中不存在(不会给出任何结果的搜索) 具有预期结果的虚拟搜索 条件1和2 =>[vadas marko lop,vadas marko lop peter] 条件1或3 =>[vadas marko lop] 我能得到的 条件1和2 条件1或2 那么,如何实现这一点呢?我有两种不同的查询格式,有没有一种方法可以编写一种既可以执行这两种操作又可以执行这两种操作的查询格式

样本数据:

条件:

  • vadas
    是否在
    2跳内连接到
    lop
  • vadas
    是否在
    3跳内连接到
    peter
  • 是否连接到
    vadas
    1个跃点中不存在
    (不会给出任何结果的搜索)
  • 具有预期结果的虚拟搜索

  • 条件
    1
    2
    =>[vadas marko lop,vadas marko lop peter]

  • 条件
    1
    3
    =>[vadas marko lop]


  • 我能得到的

  • 条件
    1
    2
  • 条件
    1
    2
  • 那么,如何实现这一点呢?我有两种不同的查询格式,有没有一种方法可以编写一种既可以执行这两种操作又可以执行这两种操作的查询格式


    这不起作用,这里有什么问题吗?不返回已遍历的节点

    g.V().has("person", "name", "vadas").as("nodes")
    .or(
    repeat(both().as("nodes")).times(2).emit().has("software", "name", "lop"), 
    repeat(both().as("nodes")).times(3).emit().has("person", "name", "peter")
    )
    .project("a").by(select(all, "nodes").unfold().values("name").fold())
    ==>[a:[vadas]]
    // Expect paths to be printed here vadas..lop, vadas...peter

    我不知道我是否理解您的意图,但如果您只需要查询模板之类的东西,那么这可能会有所帮助:

    gremlin> conditions = [
    ......1>   [filter: {has("software", "name", "lop")},  distance: 2],
    ......2>   [filter: {has("person", "name", "peter")},  distance: 3],
    ......3>   [filter: {has("x", "y", "does-not-exist")}, distance: 1]]
    ==>[filter:groovysh_evaluate$_run_closure1@378bd86d,distance:2]
    ==>[filter:groovysh_evaluate$_run_closure2@2189e7a7,distance:3]
    ==>[filter:groovysh_evaluate$_run_closure3@69b2f8e5,distance:1]
    
    gremlin> g = TinkerFactory.createModern().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    gremlin> g.V().has("person", "name", "vadas").
    ......1>   union(repeat(both().simplePath()).
    ......2>           times(conditions[0].distance).
    ......3>           emit().
    ......4>         filter(conditions[0].filter()).store("x"),
    ......5>         repeat(both().simplePath()).
    ......6>           times(conditions[1].distance).
    ......7>           emit().
    ......8>         filter(conditions[1].filter()).store("x")).
    ......9>   barrier().
    .....10>   filter(select("x").
    .....11>          and(unfold().filter(conditions[0].filter()),
    .....12>              unfold().filter(conditions[1].filter()))).
    .....13>   path().
    .....14>     by("name")
    ==>[vadas,marko,lop]
    ==>[vadas,marko,lop,peter]
    
    gremlin> g.V().has("person", "name", "vadas").
    ......1>   union(repeat(both().simplePath()).
    ......2>           times(conditions[0].distance).
    ......3>           emit().
    ......4>         filter(conditions[0].filter()).store("x"),
    ......5>         repeat(both().simplePath()).
    ......6>           times(conditions[2].distance).
    ......7>           emit().
    ......8>         filter(conditions[2].filter()).store("x")).
    ......9>   barrier().
    .....10>   filter(select("x").
    .....11>          or(unfold().filter(conditions[0].filter()),
    .....12>             unfold().filter(conditions[2].filter()))).
    .....13>   path().
    .....14>     by("name")
    ==>[vadas,marko,lop]
    
    再抽象一点应该会更清楚,这两个查询只在一个步骤中不同(
    vs
    ):

    apply={条件->
    重复(两个().simplePath())。
    时间(条件、距离)。
    emit()。
    过滤器(condition.filter()).store(“x”)
    }
    验证={条件->
    展开().filter(条件.filter())
    }
    //条件1和2
    g、 V()有(“人”、“姓名”、“vadas”)。
    联合(适用(条件[0]),
    适用(条件[1])。
    屏障()。
    过滤器(选择(“x”)。
    和(验证(条件[0]),
    验证(条件[1]))。
    路径()。
    通过(“姓名”)
    //条件1或3
    g、 V()有(“人”、“姓名”、“vadas”)。
    联合(适用(条件[0]),
    适用(条件[2])。
    屏障()。
    过滤器(选择(“x”)。
    或(验证(条件[0]),
    验证(条件[2]))。
    路径()。
    通过(“姓名”)
    
    我不能说我完全理解了这个问题。你说的“通用”是什么意思?这似乎是你问题的重点,但我不清楚这对我意味着什么。我可以用两种不同的方法实现
    逻辑,有没有更好的方法将这两种方法结合起来?我仍然不明白。Gremlin有条件逻辑的
    和()?你是说你想用另一种方法来编写上面的遍历,而不使用这两个步骤吗?我会更新帖子来解释更好我编辑了帖子,现在更好了?非常感谢,所以
    filter
    必须做两次,而且似乎
    或(…as(“x”),…as(“y”))
    不会将值存储到
    x
    y
    中。我说的对吗?只针对当前路径,这对您的用例没有帮助,因为您的过滤条件取决于最终结果。
    g.V().has("person", "name", "vadas").as("nodes")
    .or(
    repeat(both().as("nodes")).times(2).emit().has("software", "name", "lop"), 
    repeat(both().as("nodes")).times(3).emit().has("person", "name", "peter")
    )
    .project("a").by(select(all, "nodes").unfold().values("name").fold())
    ==>[a:[vadas]]
    // Expect paths to be printed here vadas..lop, vadas...peter
    gremlin> conditions = [
    ......1>   [filter: {has("software", "name", "lop")},  distance: 2],
    ......2>   [filter: {has("person", "name", "peter")},  distance: 3],
    ......3>   [filter: {has("x", "y", "does-not-exist")}, distance: 1]]
    ==>[filter:groovysh_evaluate$_run_closure1@378bd86d,distance:2]
    ==>[filter:groovysh_evaluate$_run_closure2@2189e7a7,distance:3]
    ==>[filter:groovysh_evaluate$_run_closure3@69b2f8e5,distance:1]
    
    gremlin> g = TinkerFactory.createModern().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
    gremlin> g.V().has("person", "name", "vadas").
    ......1>   union(repeat(both().simplePath()).
    ......2>           times(conditions[0].distance).
    ......3>           emit().
    ......4>         filter(conditions[0].filter()).store("x"),
    ......5>         repeat(both().simplePath()).
    ......6>           times(conditions[1].distance).
    ......7>           emit().
    ......8>         filter(conditions[1].filter()).store("x")).
    ......9>   barrier().
    .....10>   filter(select("x").
    .....11>          and(unfold().filter(conditions[0].filter()),
    .....12>              unfold().filter(conditions[1].filter()))).
    .....13>   path().
    .....14>     by("name")
    ==>[vadas,marko,lop]
    ==>[vadas,marko,lop,peter]
    
    gremlin> g.V().has("person", "name", "vadas").
    ......1>   union(repeat(both().simplePath()).
    ......2>           times(conditions[0].distance).
    ......3>           emit().
    ......4>         filter(conditions[0].filter()).store("x"),
    ......5>         repeat(both().simplePath()).
    ......6>           times(conditions[2].distance).
    ......7>           emit().
    ......8>         filter(conditions[2].filter()).store("x")).
    ......9>   barrier().
    .....10>   filter(select("x").
    .....11>          or(unfold().filter(conditions[0].filter()),
    .....12>             unfold().filter(conditions[2].filter()))).
    .....13>   path().
    .....14>     by("name")
    ==>[vadas,marko,lop]