[英]Count distinct nodes from traversal in AQL
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: 我尝试按照docs中的建议使用
LENGTH(DISTINCT v._key)
,但这不是AQL的正确语法:
syntax error, unexpected DISTINCT modifier near 'DISTINCT v._key)'
语法错误,“ DISTINCT v._key)附近有意外的DISTINCT修饰符”
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? 天真的解决方案是获取所有密钥并在客户端进行计数,但是我很好奇如何在服务器端进行操作?
What RETURN DISTINCT
does is to remove duplicate values , but only after the traversal. RETURN DISTINCT
作用是删除重复值 ,但仅在遍历之后才删除。
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. 遍历选项
uniqueVertices
可以设置为"global"
这样就不会从该遍历中两次返回相同的顶点。 The option for breadth-first search bfs
needs to be enabled to use uniqueVertices: "global"
. 需要启用广度优先搜索
bfs
的选项才能使用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. 深度优先搜索不支持此唯一性选项的原因是结果不确定,因此此组合被禁用。
Inspired by this blogpost http://jsteemann.github.io/blog/2014/12/12/aql-improvements-for-24/ I prepared the solution using LET
: 受此博客文章http://jsteemann.github.io/blog/2014/12/12/aql-improvements-for-24/的启发,我使用
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. 它可能不是最佳解决方案,但可以按我预期的那样工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.