Search code examples
arangodbaql

ArangoDB Traversal with conditions on vertice's neighbours


I have the following graph: enter image description here

I'd like to write a AQL query that returns paths, starting from the node Q1, in which the second node in the path has the neighbour P6 (neighbours of the start node Q1 always have a P-node (P6, P4, ..) in its neighbourhood).

For example, the good paths are : Q1->Q1-P6-3->STRING-2, Q1->Q1-P6-3->Q1-P6-3-QUAL-P2-2->QUANT-2, etc. What is the best way to check the condition? For now, I've added to nodes such as Q1-P6-3 an attribute property with the key of P-node from its neighbourhood and just filter paths:

FILTER p.vertices[1].property == 'P6'

But I believe there is a smarter way to do that. Thank you in advance!


Solution

  • The AQL first gets the list of all nodes that have P6 as a direct neighbor and then uses that to filter the paths in the traversal query. This way you do not need to keep track of the property in the vertices:

    LET p6Neighbors = (FOR t IN testEdge
                        FILTER t._to == 'test2/P6'
                        RETURN t._from)
    for v,e,p in 1..5 ANY 'test2/Q1' testEdge
    FILTER p.vertices[1]._id in p6Neighbors
    return p