【问题标题】:Tinkerpop Select neighbours grouped by the vertex they are neighbour with range stepTinkerpop 选择按顶点分组的邻居,它们是具有范围步长的邻居
【发布时间】:2020-08-25 10:13:06
【问题描述】:

我想选择所有 l 标记的顶点以及它们的 t 标记的顶点,这些顶点按它们的邻居分组。我还想对邻居的长度施加限制。 对于 ex for neighbor limit = 2,应该输出如下内容。

[
{"l1",[t1,t2]},
{"l2",[t3]},
{"l3",[]}
]

对于 ex for neighbor limit = 1,应该输出类似下面的内容。

[
{"l1",[t1]},
{"l2",[t3]},
{"l3",[]}
]

grelify 链接https://gremlify.com/xun4v83y54/1

g.addV('Vertex').as('1').property(single, 'name', 'l1').property(single, 'label', 'l').
  addV('Vertex').as('2').property(single, 'name', 'l2').property(single, 'label', 'l').
  addV('Vertex').as('3').property(single, 'name', 'l3').property(single, 'label', 'l').
  addV('Tag').as('4').property(single, 'name', 't1').property(single, 'label', 't').
  addV('Tag').as('5').property(single, 'name', 't2').property(single, 'label', 't').
  addV('Tag').as('6').property(single, 'name', 't3').property(single, 'label', 't').
  addE('connected').from('1').to('4').
  addE('connected').from('1').to('5').
  addE('connected').from('2').to('6')

【问题讨论】:

    标签: gremlin tinkerpop tinkerpop3 gremlin-server tinkergraph


    【解决方案1】:

    我认为group() 在这里会很好用:

    gremlin> g.V().has('label','l').
    ......1>   group().
    ......2>     by('name').
    ......3>     by(out().limit(2).values('name').fold())
    ==>[l1:[t1,t2],l2:[t3]]
    gremlin> g.V().has('label','l').
    ......1>   group().
    ......2>     by('name').
    ......3>     by(out().limit(1).values('name').fold())
    ==>[l1:[t1],l2:[t3]]
    

    请注意,第二个 by() 调制器是对您分组的收集项目的归约操作。在那里,您可以根据需要进一步操作该集合。为了演示,我稍微修改了您的数据:

    g.addV('Vertex').as('1').property(single, 'name', 'l1').property(single, 'label', 'l').
      addV('Vertex').as('2').property(single, 'name', 'l2').property(single, 'label', 'l').
      addV('Vertex').as('3').property(single, 'name', 'l3').property(single, 'label', 'l').
      addV('Tag').as('4').property(single, 'name', 't1').property(single, 'label', 't').
      addV('Tag').as('5').property(single, 'name', 't2').property(single, 'label', 't').
      addV('Tag').as('6').property(single, 'name', 't3').property(single, 'label', 't').
      addV('Tag').as('7').property(single, 'name', 't4').property(single, 'label', 't').
      addE('connected').from('1').to('4').
      addE('connected').from('1').to('5').
      addE('connected').from('2').to('6').
      addE('next').from('2').to('7')
    

    在以下情况下,我只是使用 union() 创建了两个列表 - 每个“类型”一个:

    gremlin> g.V().has('label','l').
    ......1>   group().
    ......2>     by('name').
    ......3>     by(union(out("connected").limit(1).values('name').fold(),
    ......4>              out("next").limit(1).values('name').fold()).
    ......5>        fold())
    ==>[l1:[[t1],[]],l2:[[t3],[t4]],l3:[[],[]]]
    

    但您当然可以使用其他collection manipulation techniques 将其转换为其他形式。

    【讨论】:

    • 有没有办法选择多种类型的邻居。例如:``` g.V().has('label','l')。团体()。按名字')。 by(out("oneType").limit(1).values('name').fold())。 by(out("secondType").limit(1).values('name').fold()) ```
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-29
    • 2019-10-20
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多