0

I have the following graph:

enter image description here

I'd like to write an AQL query that returns all vertices which are neighbor's INBOUND vertices colored in RED from the start vertex colored in GREEN.

I tried the following AQL to retrieve red vertices from the green vertex.

WITH collection_A, collection_W
LET A_Neighbors = (FOR t IN collection_edges
                    FILTER t._to == 'collection_W/W'
                    RETURN t._from)
                    
let all_w = []
for item in A_Neighbors
    let sub_w = (for v1 in collection_edges
                        FILTER v1._to == item
                        return v1 )
    return APPEND(all_w, sub_w)

Is there any good solution other than this? Because I'm not sure this gives the correct values for start vertex collection_W/W.

My collection_edges contains following two kind of documents.

{
 _from: collection_W/w,
 _to: collection_A/a,
 label: INBOUND
}

and

{
 _from: collection_A/a,
 _to: collection_W/w,
 label: OUTBOUND
}
Sajitha Liyanage
  • 443
  • 7
  • 18
  • I don't understand the goal of the query. Based on the image you drew, what would the correct result be? This sentence needs to be a little clearer: `I'd like to write an AQL query that returns all vertices which are neighbor's INBOUND vertices colored in RED from the start vertex colored in GREEN.` – David Thomas Feb 20 '22 at 17:29
  • I want to get all the red vertices starts from the green vertex as in the image. – Sajitha Liyanage Feb 21 '22 at 05:55
  • Sounds like you want to traverse with a depth of 1 in ANY direction ("A" vertices), followed by another traversal in INBOUND direction ("W" vertex on the left-hand side)? – CodeManX Feb 21 '22 at 23:53
  • Actually, I want to get neighbors' neighbors from start vertex green. – Sajitha Liyanage Feb 22 '22 at 05:46

1 Answers1

0

Given the diagram, I would suggest using a graph traversal specific [min[..max]] value, like this (using an anonymous graph):

WITH collection_A, collection_W
FOR vertex IN 2 ANY 'collection_W/W' // green start node
    collection_edges
    RETURN vertex

The [min[..max]] value can be a range (1..3) or it can be a single value (1).

  • 0 will return the start node
  • 1 will return adjacent nodes
  • 2 will skip the adjacent nodes and return only nodes at the next level (if any)
  • 2..999 will return all nodes (up to 999 hops away) from the start node

Further, if you want to make sure that you're only returning nodes from a specific collection, add a filter for that:

WITH collection_A, collection_W
FOR vertex IN 2 ANY 'collection_W/W' // green start node
    collection_edges
    FILTER IS_SAME_COLLECTION('collection_W', vertex)
    RETURN vertex

You can also filter on edges (if you've added a specific attribute/value to your edges):

WITH collection_A, collection_W
FOR vertex, edge IN 2 ANY 'collection_W/W' // green start node
    collection_edges
    FILTER edge.someProperty == 'someValue' // only return vertices that are beyond matching edges
    RETURN vertex

Or limit the traversal with PRUNE:

WITH collection_A, collection_W
FOR vertex, edge IN 2 ANY 'collection_W/W' // green start node
    collection_edges
    PRUNE edge.someProperty == 'someValue' // stop traversal when this is matched
    RETURN vertex
kerry
  • 757
  • 6
  • 16