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

hgc7kmma  于 2022-12-09  发布在  Go
关注(0)|答案(3)|浏览(196)

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

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}

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

[
      {
        "edges": [
          {
            "_from": "entities/198593",
            "_key": "391330",
            "_to": "entities/198603",
            "type": 300
          },
          {
            "_from": "entities/198593",
            "_key": "391390",
            "_to": "entities/198477",
            "type": 110
          },
          ...
        ],
        "vertices": [
          { "_key": "198603", "name": "A" },
          { "_key": "198477", "name": "B" },
          ...
        ]
      }
    ]

如何通过一个查询获得相同的结果(唯一顶点和唯一边)?
PS:结果被 Package 在一个数组中,知道为什么吗?如何避免这种情况?

wwwo4jvm

wwwo4jvm1#

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

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

但是,您需要删除bfs:我想是的。
在此之后,您需要在浏览器的javascript中删除重复数据,或者如果您使用foxx,则可以在返回结果之前在API调用中执行此操作。
然而,如果你使用foxx,你可以在你的服务index.js文件中使用下面的JS,或者你可以在你的应用程序/网站中使用uniqueList函数,这样你就有了唯一的列表。
1.创建一个函数,根据对象的_id属性获取对象的唯一数组
1.接电话
1.调用后,通过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 }
 });
zpjtge22

zpjtge222#

你很接近...
通过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,但是我不知道如果你有uniqueEdges: "path",你如何得到重复的路径。
看看这是否有帮助,如果没有,张贴回复,我们会看看我们能做些什么。

r3i60tvu

r3i60tvu3#

//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}

对我来说,这是一个过于复杂的练习,这样一个明显的要求,从一个图形数据库。我不评论的选项添加在前面的答案。

相关问题