【问题标题】:Gremlin.Net return multi sub vertex in the same arrayGremlin.Net 在同一个数组中返回多个子顶点
【发布时间】:2018-01-13 16:13:09
【问题描述】:

通常家庭中只有一个父亲和一个母亲,但有多个孩子, 在 gremlin 中,我可以说得到一个家庭:

g.V(61464).as('me').outE('father').inV().as('father').select('me').outE('mother').inV().as('mother').select('me').inE('father').outV().as('child').select('father', 'mother', 'child')

这将返回以下内容:

 - ==> *{father=v[16408], mother=v[450608], child=v[139504]}*
 - ==> *{father=v[16408], mother=v[450608], child=v[163880]}*
 - ==> *{father=v[16408], mother=v[450608], child=v[176368]}*

但我想以这种方式获得它们:

==> {父亲=v[16408], 母亲=v[450608], 孩子=[v[139504], v[163880], v[176368]]

有没有办法在 gremlin 中做到这一点,在 Gremlin.Net 中更具体。谢谢

【问题讨论】:

  • 当您向 Gremlin 提问时,如果在 Gremlin 控制台中复制以 Gremlin 查询形式存在的示例图表会很有帮助。这样可以更轻松地为您提供帮助。

标签: c# gremlin janusgraph


【解决方案1】:

最简单的方法可能是使用project 步骤:

gremlin> g.V(61464).project('father','mother','children').
             by(out('father')).
             by(out('mother')).
             by(__.in('father').fold())
==>[father:v[4344],mother:v[4152],children:[v[8440],v[12536],v[40964200]]]

(id 与你的不匹配,因为我必须自己创建图表并获得其他 id。)

请注意,__.in('father')__ 仅对 Gremlin-Groovy 是必需的,因为 in 是 Groovy 中的保留关键字,并且 out('father')outE('father').inV() 的缩写形式。

您可以在 Gremlin.Net 中编写相同的遍历。然后它看起来像这样:

g.V(61464).Project<object>("father", "mother", "children").
            By(Out("father")).
            By(Out("mother")).
            By(In("father").Fold()).Next();

(您需要using static Gremlin.Net.Process.Traversal.__; 才能编写这样的遍历。否则By-steps 将如下所示:By(__.Out("father"))。有关more information on this,请参阅 TinkerPop 文档。)

【讨论】:

  • 非常感谢,非常有帮助:)
  • 你好 Florian Hockmann 当我使用这个 g.V(61464).Project("father", "mother", "children")。由(出(“父亲”))。由(出(“母亲”))。 By(In("父亲").Fold()).Next();如果选定的顶点没有父查询,我该如何避免呢?
  • 我不确定这是否是这里的最佳解决方案,但解决此问题的一种方法是使用 coalesce stepBy(Out("father")) 变为 By(Coalesce&lt;object&gt;(Out("father"), Constant("has no father")))Coalesce 的作用是当Out("father") 没有返回顶点时将执行Constant 步骤。
猜你喜欢
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 2015-02-18
  • 2021-01-22
  • 2018-10-01
相关资源
最近更新 更多