如何按入局边数计数和排序

时间:2014-05-01 01:39:37

标签: graph arangodb

我正在努力让十位出演过最多电影的人获得电影的名称和数量,并按降序排列。我使用的是IMDB dataset

这是我到目前为止所做的:

arangosh [_system]>   var Graph = require("org/arangodb/graph").Graph;
arangosh [_system]>   var db = require("org/arangodb").db;
arangosh [_system]> statement = db._createStatement({query: 'FOR vert IN imdb_vertices FILTER vert.type == "Person" LET edge_count = (LENGTH(EDGES(imdb_edges, vert, "outbound", [{type: "ACTS_IN"}]))) RETURN {"name": vert.name,  "count": edge_count}'})
[object ArangoStatement]

arangosh [_system]> cursor = statement.execute()
[object ArangoQueryCursor]

arangosh [_system]> cursor.next()
{ 
  "name" : "Stephanie Faracy", 
  "count" : 0 
}

如何使用AQL实现这一目标?

1 个答案:

答案 0 :(得分:4)

没有索引:以下将返回Top-10:

arangosh [_system]> db._createStatement({query: 'FOR vert IN imdb_vertices FILTER vert.type == "Person" LET edge_count = (LENGTH(EDGES(imdb_edges, vert, "outbound", [{"type": "Role", "$label": "ACTS_IN"}]))) SORT edge_count DESC LIMIT 10 RETURN {"name": vert.name,  "count": edge_count}'}).execute().toArray()
[ 
  { 
    "name" : "Clint Eastwood", 
    "count" : 148 
  }, 
  { 
    "name" : "Claude Jade", 
    "count" : 142 
  }, 
  { 
    "name" : "Samuel L. Jackson", 
    "count" : 122 
  }, 
  { 
    "name" : "Armin Mueller-Stahl", 
    "count" : 112 
  }, 
  { 
    "name" : "Gérard Depardieu", 
    "count" : 104 
  }, 
  { 
    "name" : "Marisa Mell", 
    "count" : 104 
  }, 
  { 
    "name" : "Robert De Niro", 
    "count" : 104 
  }, 
  { 
    "name" : "Bruce Willis", 
    "count" : 96 
  }, 
  { 
    "name" : "Jackie Chan", 
    "count" : 94 
  }, 
  { 
    "name" : "Michael Caine", 
    "count" : 90 
  } 
]

基本上你可以使用" sort"也适用于使用LET创建的变量。限制允许你限制到TOP 10.注意顶点中的类型是'角色'和标签是' ACTS_IN'。

将数字添加到文档并使用排序索引会更有效。但这需要更新文件。

arangosh [_system]> c = db._createStatement({query: 'FOR vert IN imdb_vertices FILTER vert.type == "Person" LET edge_count = (LENGTH(EDGES(imdb_edges, vert, "outbound", [{"type": "Role", "$label": "ACTS_IN"}]))) RETURN {"_key": vert._key,  "count": edge_count}'}).execute()
[object ArangoQueryCursor]

arangosh [_system]> while (c.hasNext()) { var d = c.next(); db.imdb_vertices.update(d._key, {COUNT: d.count}); }

arangosh [_system]> db.imdb_vertices.ensureSkiplist("COUNT");

arangosh [_system]> x = db._createStatement({query: 'FOR vert in imdb_vertices FILTER vert.COUNT >= 0 SORT vert.COUNT DESC LIMIT 10 RETURN vert'}).execute()
[object ArangoQueryCursor]