1
votes

I am able to get all distinct nodes from a query, but not the count:

FOR v in 2..2 OUTBOUND "starting_node" GRAPH "some_graph"
return DISTINCT v._key

I want to get only the count of the result. I tried to use LENGTH(DISTINCT v._key) as suggested in the docs, but it's not a proper syntax of the AQL:

syntax error, unexpected DISTINCT modifier near 'DISTINCT v._key)'

The naive solution is to get all keys and count it on the client side, but I am curious how to do it on the server side?

2

2 Answers

2
votes

What RETURN DISTINCT does is to remove duplicate values, but only after the traversal.

You can set traversal options to eliminate paths during the traversal, which can be more efficient especially if you have a highly interconnected graph and a high traversal depth:

RETURN LENGTH(
  FOR v IN 2..2 OUTBOUND "starting_node" GRAPH "some_graph"
  OPTIONS { uniqueVertices: "global", bfs: true }
    RETURN v._key
)

The traversal option uniqueVertices can be set to "global" so that you don't get the same vertex returned twice from this traversal. The option for breadth-first search bfs needs to be enabled to use uniqueVertices: "global". The reason why depth-first search does not support this uniqueness option is that the result would not be deterministic, hence this combination was disabled.

0
votes

Inspired by this blogpost http://jsteemann.github.io/blog/2014/12/12/aql-improvements-for-24/ I prepared the solution using LET:

LET result = (FOR v in 2..2 OUTBOUND "starting_node" GRAPH "some_graph"
                  return DISTINCT v._key)
RETURN LENGTH(result)

It might be not optimal solution, but it works as I expected.