2

I want next:

  1. Traverse part of graph
  2. Take property from first traverse
  3. Put it into other traversal as filter
  4. Get filtered value

When I run next in Gremlin console:

g = TinkerGraph.open().traversal()
g.addV('a').property(id, 1).property('b',2)
g.addV('a').property(id, 2).property('b',2).property('c',3)
g.V(2).properties().key().limit(1).as('q').select('q')
g.V(2).properties().key().limit(1).as('q').V(1).properties().key()
g.V(2).properties().key().limit(1).as('q').V(1).properties().key().select('q')
g.V(2).properties().key().limit(1).as('q').V(1).properties().key().where(__.is('b'))
g.V(2).properties().key().limit(1).as('q').V(1).properties().key().where(__.is(select('q')))

I get:

gremlin>     g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin>     g.addV('a').property(id, 1).property('b',2)
==>v[1]
gremlin>     g.addV('a').property(id, 2).property('b',2).property('c',3)
==>v[2]
gremlin>     g.V(2).properties().key().limit(1).as('q').select('q')
==>b
gremlin>     g.V(2).properties().key().limit(1).as('q').V(1).properties().key()
==>b
gremlin>     g.V(2).properties().key().limit(1).as('q').V(1).properties().key().select('q')
==>b
gremlin>     g.V(2).properties().key().limit(1).as('q').V(1).properties().key().where(__.is('b'))
==>b
gremlin>     g.V(2).properties().key().limit(1).as('q').V(1).properties().key().where(__.is(select('q')))
gremlin>

So I can see that:

  1. My first traverse path gets property of 'b'
  2. Selecting by direct usage of literal 'b' works
  3. Using projection to filter by 'b' does not works.

So question is - how to use value from one part of traversal as filter of other traversal in case described above?

My use case is that I have prototype vertex. I want to grapb all its properties(and may be values), and find all vertices which are similar to that prototype.

Other alternative is to store query inside property of prototype, read it and evaluate it to get vertices which are filtered by it.

I know that I can do application side join of strings, but I want to stay only in code less part of Gremlin to have proper provider portability.

UPDATE:

Example from official documentation:

gremlin> firstYear = g.V().hasLabel('person').
                           local(properties('location').values('startTime').min()).
                           max().next()
==>2004
gremlin> l = g.V().hasLabel('person').as('person').
                   properties('location').or(has('endTime',gt(firstYear)),hasNot('endTime')).as('location').
                   valueMap().as('times').
                   select('person','location','times').by('name').by(value).by().toList()

How can I use firstYear without having variables in console, but to reference that from query?

Dzmitry Lahoda
  • 939
  • 1
  • 13
  • 34

1 Answers1

2

I see your question was answered on the Gremlin Users list. [1] Copying the answer here for others that may search for the same question.

What you're looking for is:

g.V(2).properties().key().limit(1).as('q').V(1).properties().key().where(eq('q'))

See documentation for the Where Step to learn about the different usage patterns of where.

[1] https://groups.google.com/forum/#!topic/gremlin-users/f1NfwUw9ZVI

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38