[英]Returning a single document of vertices and edges from an AQL traversal
When I do a traversal in Arango I get an array of json structures that look like this: 当我在Arango中进行遍历时,我得到一个json结构数组,如下所示:
{
"vertex" : {
"_id" : "vertices/857831247835",
"_key" : "857831247835",
"_rev" : "857831247835",
},
"path" : {
"edges" : [
{
"_id" : "edges/857831575515",
"_key" : "857831575515",
"_rev" : "857831575515",
"_from" : "vertices/857831247835",
"_to" : "vertices/857821417435",
}
],
"vertices" : [
{
"_id" : "vertices/857821417435",
"_key" : "857821417435",
"_rev" : "857821417435",
},
{
"_id" : "vertices/857831247835",
"_key" : "857831247835",
"_rev" : "857831247835",
}
]
},
"startVertex" : "vertices/857821417435"
}
Is there a way of getting all edges/vertices found in the traversal into a single structure like the one above using AQL? 有没有办法将遍历中找到的所有边/顶点变为单个结构,就像上面使用AQL一样?
You can actually use two ways to get your result: 您实际上可以使用两种方法来获得结果:
Easy-way extend your AQL-query: 轻松扩展您的AQL查询:
FOR x IN (<<Your previous query here>>) RETURN {edges: x.path.edges, vertices: x.path.vertices, vertex: x.vertex, startVertex: x.startVertex}
More performant way (you short-circuit the object creation): Register a user-defined function following: https://docs.arangodb.com/AqlExtending/Functions.html One time using arangosh
. 更arangosh
方式(短路对象创建):注册以下用户定义的函数: https : arangosh
一次使用arangosh
。
This should be something like this: 这应该是这样的:
require("org/arangodb/aql/functions").register("myVisitors::flatVisitor", function (config, result, vertex, path) { result.push({ vertex: vertex, edges: path.edges, vertices: path.vertices}); });
And then in your AQL add the additional option visitor: "myVisitors::flatVisitor"
same as paths: true
. 然后在你的AQL中添加额外的选项visitor: "myVisitors::flatVisitor"
与paths: true
相同。
BTW: paths: true
will be ignored in this case as it was used in our default visitor only. BTW:在这种情况下, paths: true
将被忽略,因为它仅在我们的默认访问者中使用。
HINT: If you only need certain attributes in your result instead of the complete document only return these ones in your visitor. 提示:如果您只需要结果中的某些属性而不是完整文档,则只需在访问者中返回这些属性。 This will give a significant performance improvement. 这将显着提高性能。
A bit old but for the sake of new visitors to this question. 有点旧,但为了这个问题的新访问者。 another way is to accumulate edges from the path uniquely (syntax is of the arangojs official client): 另一种方法是从路径中唯一地累积边缘(语法是arangojs官方客户端):
graph.traversal('verticies/startkey', {
direction: 'any',
init: `result.verticies = [];
result.edges = [];`,
visitor: `
path.edges
.forEach(function (x) {
if (result.edges.indexOf(x) === -1) {
result.edges.push(x);
}
});
result.verticies.push(vertex);
`,
uniqueness: {
"vertices": "global",
"edges": "global"
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.