0

I have the following Graph

enter image description here

If I write a Query g.V('A').Out(), how can I get that the values of the Edges which were traversed and the vertex which were encounterned in the traveral ?

Florian Hockmann
  • 2,634
  • 13
  • 24
allthenutsandbolts
  • 1,513
  • 1
  • 13
  • 34

1 Answers1

5

You would need to tell Gremlin to not skip the edges. g.V().out() is shorthand for g.V().outE().inV(). In this case, you can interact with the value of the edges as you've explicitly told Gremlin to traverse them. I'll demonstrate with a few examples using the "modern" toy graph:

gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]

To start, you might want to filter on a particular edge property and then traverse to adjacent vertices:

gremlin> g.V().outE().has('weight',gt(0.5)).inV()
==>v[4]
==>v[5]

You mentioned in your question that you might want to see the values of edges and the vertices you encountered. One way would be to use path():

gremlin> g.V().outE().has('weight',gt(0.5)).inV().path()
==>[v[1],e[8][1-knows->4],v[4]]
==>[v[4],e[10][4-created->5],v[5]]

You might also get more explicit to get specific properties from the edge:

gremlin> g.V().outE().has('weight',gt(0.5)).inV().path().by().by('weight')
==>[v[1],1.0,v[4]]
==>[v[4],1.0,v[5]]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • What would be the difference between using a tree() vs path() ? – allthenutsandbolts Jan 26 '18 at 14:29
  • 1
    two different data structures. a path is a list and a tree is hierarchical. i think it just depends on what you need to have as output. – stephen mallette Jan 26 '18 at 14:30
  • That makes sense. If I have those vertexes, how would I fetch them and extract some more properties from them ? – allthenutsandbolts Jan 26 '18 at 15:08
  • `values()` step extracts the values of specified properties from graph elements like a `Vertex`, but there are other ways, like what i demonstrated above with the `by("weight")` modulator to `path()`. basically, how you "fetch them and extract some more properties" is a really general question and an exact answer is how to give without knowing exactly what you want. – stephen mallette Jan 26 '18 at 15:51
  • What I actually need to do is this, For all the traversed vertex and edges, I need to be able to extract a certain property or check for some conditions on the properties on the vertex which are in the path and depending on the outcome emit the Vertex. Hope that makes sense. – allthenutsandbolts Jan 26 '18 at 16:30
  • my examples above do exactly that. note the last example most closely. it traverses edges and vertices, uses `has()` to filter on an edge property (`has()` works equally well on vertices and their properties). if you want to extract a specific vertex from the outputted path then realize that the `Path` is just a `List` so you can `unfold()` it to a stream, `tail()` it to pop off last values, `limit()` it to get first values, etc. – stephen mallette Jan 26 '18 at 17:53
  • For what you're asking you might not even need to analyze paths....it all depends on what you specifically need. Perhaps you should write a new question with a sample graph of your data as a Gremlin script that can be pasted to the console and an example of the output you'd like to have with the filters you would like to see. – stephen mallette Jan 26 '18 at 17:53