GO遍历中,$^ 疑问

参考这个问题,properties($$/$^)的顺序 - #2,来自 jmq2020

src(edge) 永远表示的 edge 中的src, $^ 表示的 从哪个点开始拓展 哪个点就是 src,也就是说,如果是出向边,$^指的是开始节点,如果是反相边,那$^指的就是结束节点,所以在遍历中,不能通过$^指代开始节点,src虽然能永远表示开始节点,但是只能得到开始节点的id,那如果我想在遍历中得到开始节点的其他属性,我该怎么做?

1 个赞

边的一个内置属性 _type 正负决定了方向,可以用 CASE 去根据边 _type 正负来 properties() 取 $^ 还是 $$

https://docs.nebula-graph.com.cn/3.0.1/3.ngql-guide/4.variable-and-composite-queries/3.property-reference/#_7

引用内置的边属性¶
除了自定义的边属性,每条边还有如下四种内置属性:

参数 说明
_src 边的起始点
_dst 边的目的点
_type 边的类型内部编码,正负号表示方向:正数为正向边,负数为逆向边
_rank 边的 rank 值

1 个赞

谢谢你的回复,这个我有在文档里看到过,但是_type 内置属性必须要通过 具体边名称._type才能获取到,我用GO遍历的时候,是一个边的集合,我没办法知道具体是在遍历哪种类型的边

确实,这时候就很不好写了,用 match 比较容易。
ngql的话,我这么写了一个两种边的情况


(root@nebula) [basketballplayer]> GO FROM "player100" OVER * bidirect YIELD edge as e , CASE (follow._type IS NOT NULL AND follow._type > 0) OR (serve._type IS NOT NULL AND serve._type > 0) WHEN true THEN properties($^) ELSE properties($$) END AS src
+-----------------------------------------------------------------------+--------------------------------------+
| e                                                                     | src                                  |
+-----------------------------------------------------------------------+--------------------------------------+
| [:follow "player101"->"player100" @0 {degree: 95}]                    | {age: 36, name: "Tony Parker"}       |
| [:follow "player102"->"player100" @0 {degree: 75}]                    | {age: 33, name: "LaMarcus Aldridge"} |
| [:follow "player104"->"player100" @0 {degree: 55}]                    | {age: 32, name: "Marco Belinelli"}   |
| [:follow "player105"->"player100" @0 {degree: 70}]                    | {age: 31, name: "Danny Green"}       |
| [:follow "player107"->"player100" @0 {degree: 80}]                    | {age: 32, name: "Aron Baynes"}       |
| [:follow "player108"->"player100" @0 {degree: 80}]                    | {age: 36, name: "Boris Diaw"}        |
| [:follow "player109"->"player100" @0 {degree: 80}]                    | {age: 34, name: "Tiago Splitter"}    |
| [:follow "player113"->"player100" @0 {degree: 99}]                    | {age: 29, name: "Dejounte Murray"}   |
| [:follow "player125"->"player100" @0 {degree: 90}]                    | {age: 41, name: "Manu Ginobili"}     |
| [:follow "player144"->"player100" @0 {degree: 80}]                    | {age: 47, name: "Shaquille O'Neal"}  |
| [:serve "player100"->"team204" @0 {end_year: 2016, start_year: 1997}] | {age: 42, name: "Tim Duncan"}        |
| [:follow "player100"->"player101" @0 {degree: 95}]                    | {age: 42, name: "Tim Duncan"}        |
| [:follow "player100"->"player125" @0 {degree: 95}]                    | {age: 42, name: "Tim Duncan"}        |
+-----------------------------------------------------------------------+———————————————————+
1 个赞

既然已经通过src(edge)拿到vid,接个管道fetch prop拿属性可以满足要求吗?

1 个赞

:+1:t2:,真的可以,奇怪,我之前记得 fetch 不能接变量昨天就没试。

(root@nebula) [basketballplayer]> GO FROM "player100" OVER * bidirect YIELD src(edge) as vid | fetch prop on * $-.vid YIELD properties(vertex);
+--------------------------------------+
| properties(VERTEX)                   |
+--------------------------------------+
| {age: 33, name: "LaMarcus Aldridge"} |
| {age: 32, name: "Marco Belinelli"}   |
| {age: 32, name: "Aron Baynes"}       |
| {age: 41, name: "Manu Ginobili"}     |
| {age: 29, name: "Dejounte Murray"}   |
| {age: 31, name: "Danny Green"}       |
| {age: 34, name: "Tiago Splitter"}    |
| {age: 36, name: "Tony Parker"}       |
| {age: 42, name: "Tim Duncan"}        |
| {age: 42, name: "Tim Duncan"}        |
| {age: 42, name: "Tim Duncan"}        |
| {age: 36, name: "Boris Diaw"}        |
| {age: 47, name: "Shaquille O'Neal"}  |
+--------------------------------------+
Got 13 rows (time spent 25441/78874 us)

谢谢二位的热情回复,学习到了,谢谢你们!

如果你觉得上面 wey or xjc 的回复解决了你的问题,可以勾选为解决方案哈。谢谢 young112

我目前是用的case when对比一下src(edge)和id($^),相等就表示$^是开始节点,不相等就表示$^是结束节点,这样也是行的

YIELD DISTINCT CASE src(edge) == id($^) WHEN true THEN properties($^).name ELSE properties($$).name END AS fromName,properties(edge).name AS relName,CASE dst(edge) == id($$) WHEN true THEN properties($$).name ELSE properties($^).name END AS toName

可以 explain 来比较看看不同方式的开销,看起来 xjc 提的 | fetch src(edge) 的方式比较轻,少一些 project,并没有额外多 getVertices

好的,谢谢你

此话题已在最后回复的 7 天后被自动关闭。不再允许新回复。