Skip to content

Instantly share code, notes, and snippets.

@wey-gu
Last active May 26, 2023 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wey-gu/f41e8a5651d95506b37bee6a8b5a1b8c to your computer and use it in GitHub Desktop.
Save wey-gu/f41e8a5651d95506b37bee6a8b5a1b8c to your computer and use it in GitHub Desktop.
The pitfalls that we should avoid in Nebula Graph. 使用 Nebula Graph 时候我们要注意的陷阱

TOC

Table of contents generated with markdown-toc

Good to read/know URLs:


Condition expressions on multiple hops in MATCH

https://discuss.nebula-graph.com.cn/t/topic/6625/5

如果想要通过描述多跳的边的过滤条件,比如-[e:follow*2]->,这时候 e 不再是单条边时候的数据类型了,而是一列边组成的列表,例如: 以下语句可以运行但是没有返回数据,因为e是一个列表,没有.degree的属性。

nebula> MATCH p=(v:player{name:"Tim Duncan"})-[e:follow*2]->(v2) \
        WHERE e.degree > 1 \
        RETURN DISTINCT v2 AS Friends;

这是正确的表达:

nebula> MATCH p=(v:player{name:"Tim Duncan"})-[e:follow*2]->(v2) \
        WHERE ALL(e_ in e WHERE e_.degree > 0) \
        RETURN DISTINCT v2 AS Friends;

进一步,这是表达对多跳边的第一跳的边属性过滤的表达:

nebula> MATCH p=(v:player{name:"Tim Duncan"})-[e:follow*2]->(v2) \
        WHERE e[0].degree > 98 \
        RETURN DISTINCT v2 AS Friends;

还有一种表达,用花括号,注意花括号要在星号之后:

(root@nebula) [basketballplayer]> match (n:player)-[e:follow{degree:95}*3]-(v) return n
[ERROR (-1004)]: SyntaxError: syntax error near `*3]-(v) '

Fri, 26 Nov 2021 17:11:34 CST

(root@nebula) [basketballplayer]> match (n:player)-[e:follow*3{degree:95}]-(v) return n
+-------------------------------------------------------+
| n                                                     |
+-------------------------------------------------------+
| ("player100" :player{age: 42, name: "Tim Duncan"})    |
+-------------------------------------------------------+
| ("player125" :player{age: 41, name: "Manu Ginobili"}) |
+-------------------------------------------------------+

FIND PATH

https://discuss.nebula-graph.com.cn/t/topic/6636

Use MATCH to get STATS

We shoud use STATS JOB instead.

Wrong:

MATCH (v:player) return COUNT(v)

We should use:

SUBMIT JOB STATS;
# Wait for its being finished

SHOW STATS;
+---------+------------+-------+
| Type    | Name       | Count |
+---------+------------+-------+
| "Tag"   | "player"   | 51    |
| "Tag"   | "team"     | 30    |
| "Edge"  | "follow"   | 81    |
| "Edge"  | "serve"    | 152   |
| "Space" | "vertices" | 81    |
| "Space" | "edges"    | 233   |
+---------+------------+-------+

Important reference variable and functions you could miss!

How to describe wildcard edge properties?

(root@nebula) [basketballplayer]> go from "player100" over * YIELD properties(edge)
+------------------------------------+
| properties(EDGE)                   |
+------------------------------------+
| {end_year: 2016, start_year: 1997} |
+------------------------------------+
| {degree: 95}                       |
+------------------------------------+
| {degree: 95}                       |
+------------------------------------+
Got 3 rows (time spent 1379/50293 us)

Fri, 26 Nov 2021 12:12:44 CST

(root@nebula) [basketballplayer]> go from "player100" over * WHERE properties(edge).degree > 1 YIELD properties(edge)
+------------------+
| properties(EDGE) |
+------------------+
| {degree: 95}     |
+------------------+
| {degree: 95}     |
+------------------+
Got 2 rows (time spent 1518/35277 us)

Fri, 26 Nov 2021 12:13:13 CST

nGQL MATCH v.s. OpenCypher MATCH

Escaping key words

i.e. User is a keyword, we should quote it with `.

(root@nebula) [amundsen]> MATCH (u:User) WHERE id(u) == "" RETURN u
[ERROR (-1009)]: SemanticError: `user': Unknown tag

(root@nebula) [amundsen]> MATCH (u:`User`) WHERE id(u) == "" RETURN u
+---+
| u |
+---+
+---+
Empty set (time spent 3313/9641 us)

RETURN values should be explicitly specified in latest WITH Clause

TBD

@wey-gu
Copy link
Author

wey-gu commented Aug 16, 2022

Get dst/src in bidirect traversal

-- src/dst function honers the actual src or dst of edge, while $$ doesn't

GO FROM "player100" OVER * bidirect YIELD src(edge) as vid | fetch prop on * $-.vid YIELD properties(vertex);

-- OR do in this way by checking ._type value
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

@wey-gu
Copy link
Author

wey-gu commented Jan 13, 2023

query all data and render in the studio w/o any indexes

MATCH (n) WITH n LIMIT 1000
OPTIONAL MATCH p=(n)--()
RETURN p, n

or

MATCH (n) WITH n LIMIT 1000
OPTIONAL MATCH (n)-[e]-(m)
RETURN n,e,m

@wey-gu
Copy link
Author

wey-gu commented Apr 13, 2023

FIND PATH that with one certain vertex:

FIND ALL PATH FROM "player100" TO "player101" OVER * BIDIRECT YIELD path as p |
    YIELD $-.p WHERE "player104" IN [n in nodes($-.p)| id(n)]

@wey-gu
Copy link
Author

wey-gu commented May 26, 2023

Query block subgraphs

MATCH (startNode)-[e*..2]-(connectedNode)
WHERE id(startNode) IN ["player100", "player101"]
WITH startNode, collect(connectedNode) AS connectedNodes, e AS connectedEdges, rand32() AS block
RETURN block, startNode + connectedNodes AS connectedNodes, connectedEdges

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment