让我们假设OrientDB图中的以下模型:
我有一个Profile顶点.配置文件与2个边连接:喜欢和评论.两个边都有一个" 值 "字段,表示动作的计数(或边缘的"权重").
因此,如果用户A在用户B的帖子上评论了3次,则会有一个从用户A到用户B 的评论边缘,其值为3.
现在,假设我希望获得与用户B交互的所有用户(喜欢或评论),按交互的权重排序.我可以使用以下SQL执行此操作:
select * from (traverse out from
(select out, sum(value) as value from
(traverse * from (select from Profile where username="B") while $depth < 3)
where @class="Liked" or @class="Commented" group by out order by value desc)
while $depth < 2 ) where @class="Profile" )
Run Code Online (Sandbox Code Playgroud)
但是,如果我也想知道互动的重量呢?在进行最后一次遍历时如何传播"值"?
编辑
根据该建议,此查询的简化版本将是:
select expand(out) from (
select out, sum(value) as value from (
select expand(inE("Liked", "Commented")) from Profile
where username="B"
) group by out order by value desc
)
Run Code Online (Sandbox Code Playgroud)
但我仍然找不到使用LET将值插入外部扩展对象的方法.$ parent似乎没有指向在最外部选择上展开的对象.
编辑2
我正在以我能想到的各种方式玩$ parent.在这种情况下,我看不出你如何使用它.再次 - 我试图解决的问题是如何将sum(值)传递给外部结果集.在进行GROUP BY时我没有看到使用LET的方法,当最外面的select选择进行扩展时我也没有看到使用LET的方法(因为你不能同时做其他的投影)扩大).
此外,使用$ current的结果似乎不是预期的结果.例如,以下查询:
select expand($v) from
(select from
(select expand(inE("Liked", "Commented")) from Profile where @rid=#11:0)
let $v = $current
)
Run Code Online (Sandbox Code Playgroud)
返回此:
{
"result" : [{
"@type" : "d",
"@rid" : "#14:4",
"@version" : 2,
"@class" : "Commented",
"value" : 1,
"out" : "#11:165",
"in" : "#11:0"
}, {
"@type" : "d",
"@rid" : "#14:4",
"@version" : 2,
"@class" : "Commented",
"value" : 1,
"out" : "#11:165",
"in" : "#11:0"
}, {
"@type" : "d",
"@rid" : "#14:4",
"@version" : 2,
"@class" : "Commented",
"value" : 1,
"out" : "#11:165",
"in" : "#11:0"
}
]
}
Run Code Online (Sandbox Code Playgroud)
同一个节点一遍又一遍,而不是所有的边缘,这是我所期望的.
我发现您使用的是旧版本的 OrientDB。使用更新的版本,您可以通过以下方式简化它。示例:原始查询:
select * from (
traverse out from (
select out, sum(value) as value from (
traverse * from (
select from Profile where username="B"
) while $depth < 3
) where @class="Liked" or @class="Commented" group by out order by value desc
) while $depth < 2
) where @class="Profile" )
Run Code Online (Sandbox Code Playgroud)
您可以通过使用 out()/in()/both() 传递 Edge 的标签/类来跳过某些步骤,例如:
select expand( out(["Liked","Commented]) ) from Profile where username="B"
Run Code Online (Sandbox Code Playgroud)
但是,要传递值,您可以将变量与 LET 子句一起使用。例子:
select from XXX let $parent.a = value
Run Code Online (Sandbox Code Playgroud)
通过这种方式,您可以将变量“a”设置到上层上下文中,但您也可以这样做:
select from XXX let $parent.$parent.a = value
Run Code Online (Sandbox Code Playgroud)
将其设置为 2 个级别。