1

In gremlin is it possible to store a vertex ID inside another vertex? For instance if I created a vertex like this

g.
addV('my_vertex_label').property(id,'my_vertex_id').
property('anotherVertexID','other_vertex_id')

and then queried it

V('my_vertex_id').properties('anotherVertexID').value()

it will return

["other_vertex_id"]

is there anyway I can query the other vertex like this:

V(V('my_vertex_id').properties('anotherVertexID').value())

Note that I am using AWS Neptune so the query must be pure gremlin no java/groovy

rossb83
  • 1,694
  • 4
  • 21
  • 40

1 Answers1

3

You could do something like this

gremlin> g.addV('x').property('otherid','3').iterate()

gremlin> g.V().hasLabel('x').as('a').V().where(eq('a')).by(id).by('otherid')
==>v[3]

As far as I am aware the neither the hasId() step nor the V() step can take a traversal but there may be other ways. The example above does work in my testing on Neptune.

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • Was interested if this is possible without empty `V()` step since this queries whole graph, but I have since found work around anyway. Real intent of question is if it is possible to store IDs and labels in properties and later use them for direct querying. Is this the same @Kelvin Lawrence who wrote `Practical Gremlin` :-o – rossb83 Nov 01 '18 at 15:22
  • Guilty as charged :-) – Kelvin Lawrence Nov 01 '18 at 15:43
  • 2
    Kelvin's answer shows you how to do this but I think that it's worth adding that storing `id`/`label` of one vertex within another vertex is an anti-pattern to avoid. If one vertex is related to another then a graph has a first class object for managing that relationship: an edge. You lose the benefit of the graph when you start trying to do joins like this. – stephen mallette Nov 02 '18 at 10:28
  • @stephenmallette this exactly what I found as well and am in fact using an edge to represent this relationship now – rossb83 Nov 03 '18 at 21:22
  • 1
    @stephenmallette As much as I agree with you about the anti-pattern, sometimes you find yourself inheriting a mess and there's not a lot you can do to fix the data. Scanning every vertex really isn't an acceptable solution, so I see this as a deficiency in the flexibility of the language. I guess implementations can try to recognize and optimize the `.where().by().by()` pattern, but there's no certainty that they will. – Huckle Aug 06 '19 at 19:49