Joshua
Joshua

Reputation: 1

How to query multiple vertexes' common tail vertex in titan?

In the graph, there are 3 vertexes A, B, C, they all connect to a vertex D with a edge E. Now I have A B C, how can I query their common tail vertex D with gremlin query?

Upvotes: 0

Views: 83

Answers (1)

stephen mallette
stephen mallette

Reputation: 46206

Here's one way you might do it - note that I added an E vertex that was not in common with A, B and C:

gremlin> g.addV(id,'A').as('a').
......1>   addV(id,'B').as('b').
......2>   addV(id,'C').as('c').
......3>   addV(id,'D').as('d').
......4>   addV(id,'E').as('e').
......5>   addE('knows').from('d').to('a').
......6>   addE('knows').from('d').to('b').
......7>   addE('knows').from('d').to('c').
......8>   addE('knows').from('e').to('a').
......9>   addE('knows').from('e').to('c').iterate()
gremlin> ids = ['A','B','C']
==>A
==>B
==>C
gremlin> g.V(ids).in().
......1>   where(out().id().fold().order().is(eq(ids))).
......2>   dedup()
==>v[D]

The where() basically traverses back to the head vertex and collects them into a list of identifiers which can then be compared against the original list of identifiers we used to start the traversal. Note that we rely on the identifier order() to ensure equality of the lists as:

gremlin> ['A','B','C'] == ['A','C','B']
==>false

Upvotes: 1

Related Questions