0

Usually family contains one father and mother but multi children, In gremlin I can say to get a family:

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')

this will return the following:

 - ==> *{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]}*

but I want to get them in this way:

==> {father=v[16408], mother=v[450608], children=[v[139504], v[163880], v[176368]]

Is there any way to do that in gremlin and more specific in Gremlin.Net. Thanks

Rai Vu
  • 1,595
  • 1
  • 20
  • 30
janus graph
  • 407
  • 1
  • 3
  • 18
  • It would be helpful to have a sample graph in the form of Gremlin queries that be copied in the Gremlin Console when you ask Gremlin questions. That makes it much easier to help you. – Florian Hockmann Jan 13 '18 at 16:53

1 Answers1

3

The easiest way to do this is probably with the project step:

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]]]

(The ids don't match with yours as I had to create the graph myself and got other ids.)

Note, that the __ for __.in('father') is only necessary for Gremlin-Groovy as in is a reserved keyword in Groovy and out('father') is a shorter form of outE('father').inV().

You can write the same traversal in Gremlin.Net. Then it looks like this:

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

(You need a using static Gremlin.Net.Process.Traversal.__; to be able to write the traversal like this. Otherwise the By-steps would look like this: By(__.Out("father")). See the TinkerPop docs for more information on this.)

Florian Hockmann
  • 2,634
  • 13
  • 24
  • Thank you very much, It was very helpful :) – janus graph Jan 13 '18 at 20:09
  • Hi Florian Hockmann when I use this g.V(61464).Project("father", "mother", "children"). By(Out("father")). By(Out("mother")). By(In("father").Fold()).Next(); if the selected vertex doesn't have a father the query break out, how can i avoid that? – janus graph Jan 16 '18 at 18:24
  • 1
    I'm not sure if it's the best solution here, but one way to solve this is with the [coalesce step](http://tinkerpop.apache.org/docs/3.2.7/reference/#coalesce-step): `By(Out("father"))` becomes `By(Coalesce(Out("father"), Constant("has no father")))`. `Coalesce` has the effect that the `Constant` step will be executed when `Out("father")` returns no vertices. – Florian Hockmann Jan 16 '18 at 20:41