在ArangoDB AQL中,如何从图遍历返回顶点和边?

时间:2019-04-12 20:31:54

标签: arangodb aql

我想返回在图形遍历查询期间遇到的所有唯一边和所有唯一顶点的列表。 这恰好给了我想要的结果,但是我两次执行了相同的查询:

HttpURLConnection

查询结果,每个边缘和顶点仅包含一次:

    LET eResults = (    
        FOR v,e
            IN 1..2
            ANY "entities/198593"
            relations
            OPTIONS { uniqueEdges: "path", bfs: true }
            RETURN DISTINCT KEEP(e, "_key", "_from", "_to", "type")
    )
    LET vResults = (
        FOR v,e
            IN 1..2
            ANY "entities/198593"
            relations
            OPTIONS { uniqueEdges: "path", bfs: true }
            RETURN DISTINCT KEEP(v, "_key", "name")
    )
    RETURN { edges: eResults, vertices: vResults}

如何通过一个查询获得相同的结果(唯一的顶点和唯一的边)?

PS:将结果包装在一个数组中,知道为什么吗?如何避免这种情况?

3 个答案:

答案 0 :(得分:2)

您可以尝试两件事,第一件事是半解决方案:

{uniqueEdges: "path", uniqueVertices: "global"} 

但是您需要删除bfs:我想是的。

此后,您需要在浏览器中的javascript中进行重复数据删除,或者如果您使用的是foxx,则可以在返回结果之前在API调用中进行此操作。

但是,如果您使用的是foxx,则可以在服务index.js文件中使用以下JS。或者,您可以只在应用程序/网站等中使用uniqueList函数。您将拥有唯一列表。

  1. 创建一个函数以根据对象的_id属性获取唯一的对象数组

  2. 拨打电话

  3. 在调用后,通过uniqueList函数运行结果,然后返回结果

function uniqueList(nonuniquelist){ 
    var u_list = []
    var u_edge_out_list = []
    //Go through each array and create unique set
    for(var w = 0; w < nonuniquelist.length; w++) {
        console.log()
        if (nonuniquelist[w] != null){
            if (u_list.indexOf(nonuniquelist[w]['_id']) <= 0){
                u_edge_out_list.push(nonuniquelist[w])
                console.log(nonuniquelist[w])
                u_list.push(nonuniquelist[w]['_id'])
                }
        }
    }
    return (u_edge_out_list);
}

router.get("/almost_unique_things",function(req,res){
   var bind_variables(entity_id: "entities/198593" );
   var aql = 
    `LET eResults = ( 
        FOR v,e
            IN 1..2
            ANY @entity_id
            relations
            OPTIONS { uniqueEdges: "path", bfs: true }
            RETURN DISTINCT KEEP(e, "_key", "_from", "_to", "type")
    )
    LET vResults = (
        FOR v,e
            IN 1..2
            ANY @entity_id"
            relations
            OPTIONS { uniqueEdges: "path", bfs: true }
            RETURN DISTINCT KEEP(v, "_key", "name")
    )
    RETURN { edges: eResults, vertices: vResults}`

    var results = db._query(aql,bind_variables).toArray();
    var uniqueEdges = uniqueList(edges);
    var uniqueVertices = uniqueList(vertices);
    RETURN { edges: uniqueEdges , vertices: uniqueVertices }
 });

答案 1 :(得分:0)

您要关闭...

使用行throws = [6,6,2,6,6,6,3,6,6,3,6,6,6] counter_6 = 0 stack = 0 for i in throws: if i == 6: stack += 1 else: if stack == 2: counter_6 += 1 stack = 0 if stack == 2: counter_6 += 1 print(counter_6) # --> 2 ,您可以访问另一个变量FOR v,e

尝试一下:

p

编辑:如果您想删除重复的路径,可以使用 FOR v,e,p IN 1..2 ANY "entities/198593" relations OPTIONS { uniqueEdges: "path", bfs: true } RETURN DISTINCT p ,但是我不确定如果拥有RETURN DISTINCT p,如何获得重复的路径。

看看是否有帮助(如果没有帮助),请发表回复,我们将拭目以待。

答案 2 :(得分:0)

//Starting from the path result above:
    let path =(FOR v,e,p
            IN 1..2
            ANY "entities/198593"
            relations
            OPTIONS { uniqueEdges: "path", bfs: true }
            RETURN DISTINCT p)
    
//compress, flatten and return first record out of array result the unique //vertices and edges:
          let vertices = first(return unique(flatten(path[**].vertices)))
          let edges = first(return unique(flatten(path[**].edges)))

//combine into a single graph object & return (as you've done above): 
    return {"vertices":vertices, "edges":edges}

对我来说,对于图形数据库的这种明显请求,这是一个过于复杂的练习。我没有评论上一个答案中添加的选项。