在 select 语句中对 order by 的边属性求和

Summing edges properties for order by in select statement

我正在尝试做一个 select 语句来根据它们之间的联系对结果集进行排序。

CREATE CLASS Entity EXTENDS V;
CREATE CLASS isConnectedTo EXTENDS E;

CREATE PROPERTY isConnectedTo.strength INTEGER;

'isConnectedTo' 边将 Entities 关联到另一个 Entities

棘手的部分是我在相同实体之间有双重连接:

CREATE VERTEX Entity SET name = "John";
CREATE VERTEX Entity SET name = "Mike";
CREATE VERTEX Entity SET name = "Susan";

CREATE EDGE isConnectedTo FROM (SELECT FROM Entity WHERE name = "Mike") TO (SELECT FROM Entity WHERE name = "John") SET strength = 3;
CREATE EDGE isConnectedTo FROM (SELECT FROM Entity WHERE name = "Mike") TO (SELECT FROM Entity WHERE name = "Susan") SET strength = 4;
CREATE EDGE isConnectedTo FROM (SELECT FROM Entity WHERE name = "John") TO (SELECT FROM Entity WHERE name = "Mike") SET strength = 2;

所以 MikeJohn 有联系,后者也与他有联系。另外 Mike 连接到 Susan

在这种情况下,如果我要 运行 我想要的关于 Mike 的查询,它应该 return:

  1. 约翰
  2. 苏珊

按这个顺序是因为迈克和约翰之间的力量总和是3 + 2 = 5而迈克和苏珊之间只有4.

我已经遍历了许多可能的查询,但都无济于事,但我似乎无法理解为什么最后一个拒绝工作。

LET $main = SELECT FROM Entity WHERE name = "John";
LET $vset = SELECT expand(in('isConnectedTo')) FROM $main;
SELECT @rid, eval('$ei.strength + $eo.strength') as total_strength FROM $vset
   LET $ei = (SELECT expand(inE('isConnectedTo')) FROM $current WHERE $main.@rid IN out),
   LET $eo = (SELECT expand(outE('isConnectedTo')) FROM $current WHERE $main.@rid IN in)
ORDER BY total_strength DESC;

注意:我已经用散列字典索引了实体的名称,'isConnectedTo' 边用实体之间的唯一链接索引(因此它们不能在同一方向重复)

我进行的所有其他试验或此处其他答案的帮助导致查询时间 >= 2 分钟。

试试这个查询

select expand(rid) from (select @rid,sum($a[0].sum,$b[0].sum) as sum from Entity 
let $a= (select sum(strength) as sum from isConnectedTo where in.name="Mike" and out.name=$parent.current.name),
$b= (select sum(strength) as sum from isConnectedTo where out.name="Mike" and in.name=$parent.current.name)
where name<>"Mike" order by sum desc)

试试这个查询:

select expand($a.rid) from (select from Entity where name="Mike")
let $a=(select @rid,sum(bothE().strength) as sum from Entity where both('isConnectedTo').name contains $parent.current.name group by name order by sum desc)

我想对您的域提出一个小改动。不要创建 2 条边以防两个人都连接,而是使用具有 2 个属性的相同边。

CREATE CLASS Entity EXTENDS V;
CREATE CLASS isConnectedTo EXTENDS E;
CREATE PROPERTY isConnectedTo.strengthOut INTEGER;
CREATE PROPERTY isConnectedTo.strengthIn INTEGER;

CREATE VERTEX Entity SET name = "John";
CREATE VERTEX Entity SET name = "Mike";
CREATE VERTEX Entity SET name = "Susan";

CREATE EDGE isConnectedTo FROM (SELECT FROM Entity WHERE name = "Mike") TO (SELECT FROM Entity WHERE name = "John") SET strengthOut = 3, strengthIn = 2;
CREATE EDGE isConnectedTo FROM (SELECT FROM Entity WHERE name = "Mike") TO (SELECT FROM Entity WHERE name = "Susan") SET strengthOut = 4;

在这种情况下,您的边缘较少,您可以使用这个超快速查询:

SELECT out.name as name1, in.name as name2, eval('strengthOut + strengthIn') as strength
FROM (
  SELECT expand( bothE('isConnectedTo') ) FROM Entity WHERE name = "Mike"
) ORDER BY strength

注意:记得在 Entity.name 上创建索引以加速内部查询。