LOOKUPGraph
LOOKUP
根据索引遍历数据。用户可以使用LOOKUP
实现如下功能:
- 根据
WHERE
子句搜索特定数据。
- 通过Tag列出点:检索指定Tag的所有点ID。
- 通过Edge type列出边:检索指定Edge type的所有边的起始点、目的点和rank。
- 统计包含指定Tag的点或属于指定Edge type的边的数量。
OpenCypher兼容性Graph
本文操作仅适用于原生nGQL。
注意事项Graph
- 索引会导致写性能大幅降低(降低90%甚至更多)。请不要随意在生产环境中使用索引,除非很清楚使用索引对业务的影响。
-
如果用
LOOKUP
语句基于指定属性查询时该属性没有索引,系统会在可用的索引中随机选择一个。例如,Tag
player
有属性name
和age
,Tagplayer
本身和属性name
有索引,而属性age
没有索引。当运行LOOKUP ON player WHERE player.age == 36 YIELD player.name;
时,系统会在 Tagplayer
和属性name
的索引中随机使用一个。历史版本兼容性
在此前的版本中,如果用
LOOKUP
语句基于指定属性查询时该属性没有索引,系统将报错,而不会使用其它索引。
前提条件Graph
请确保LOOKUP
语句有至少一个索引可用。如果需要创建索引,但是已经有相关的点、边或属性,用户必须在创建索引后Graph,才能使其生效。
语法Graph
LOOKUP ON {<vertex_tag> | <edge_type>}
[WHERE <expression> [AND <expression> ...]]
[YIELD <return_list> [AS <alias>]];
<return_list>
<prop_name> [AS <col_alias>] [, <prop_name> [AS <prop_alias>] ...];
WHERE <expression>
:指定遍历的过滤条件,还可以结合布尔运算符AND和OR一起使用。详情请参见Graph。
-
YIELD
:定义需要返回的输出。LOOKUP
Tag时,除了返回定义的属性,额外返回VertexID
。如果没有YIELD
子句,返回VertexID
。LOOKUP
Edge type时,除了返回定义的属性,额外返回起始点ID
、目的点ID
和rank
。如果没有YIELD
子句,返回起始点ID
、目的点ID
和rank
。
AS
:设置别名。
WHERE语句限制Graph
在LOOKUP
语句中使用WHERE
子句,不支持如下操作:
$-
和$^
。- 在关系表达式中,不支持运算符两边都有字段名,例如
tagName.prop1 > tagName.prop2
。 - 不支持运算表达式和函数表达式中嵌套AliasProp表达式。
- 不支持XOR和NOT运算符。
- 不支持除
STARTS WITH
之外的字符串操作。
检索点Graph
返回Tag为player
且name
为Tony Parker
的点。
nebula> CREATE TAG INDEX index_player ON player(name(30), age);
nebula> REBUILD TAG INDEX index_player;
+------------+
| New Job Id |
+------------+
| 15 |
+------------+
nebula> LOOKUP ON player \
WHERE player.name == "Tony Parker";
+-------------+
| VertexID |
+-------------+
| "player101" |
+-------------+
nebula> LOOKUP ON player \
WHERE player.name == "Tony Parker" \
YIELD properties(vertex).name AS name, properties(vertex).age AS age;
+-------------+---------------+-----+
| VertexID | name | age |
+-------------+---------------+-----+
| "player101" | "Tony Parker" | 36 |
+-------------+---------------+-----+
nebula> LOOKUP ON player \
WHERE player.age > 45;
+-------------+
| VertexID |
+-------------+
| "player140" |
| "player144" |
+-------------+
nebula> LOOKUP ON player \
WHERE player.name STARTS WITH "B" \
AND player.age IN [22,30] \
YIELD properties(vertex).name, properties(vertex).age;
+-------------+-------------------------+------------------------+
| VertexID | properties(VERTEX).name | properties(VERTEX).age |
+-------------+-------------------------+------------------------+
| "player134" | "Blake Griffin" | 30 |
| "player149" | "Ben Simmons" | 22 |
+-------------+-------------------------+------------------------+
nebula> LOOKUP ON player \
WHERE player.name == "Kobe Bryant"\
YIELD properties(vertex).name AS name |\
GO FROM $-.VertexID OVER serve \
YIELD $-.name, properties(edge).start_year, properties(edge).end_year, properties($$).name;
+---------------+-----------------------------+---------------------------+---------------------+
| $-.name | properties(EDGE).start_year | properties(EDGE).end_year | properties($$).name |
+---------------+-----------------------------+---------------------------+---------------------+
| "Kobe Bryant" | 1996 | 2016 | "Lakers" |
+---------------+-----------------------------+---------------------------+---------------------+
检索边Graph
返回Edge type为follow
且degree
为90
的边。
nebula> CREATE EDGE INDEX index_follow ON follow(degree);
nebula> REBUILD EDGE INDEX index_follow;
+------------+
| New Job Id |
+------------+
| 62 |
+------------+
nebula> LOOKUP ON follow \
WHERE follow.degree == 90;
+-------------+-------------+---------+
| SrcVID | DstVID | Ranking |
+-------------+-------------+---------+
| "player150" | "player143" | 0 |
| "player150" | "player137" | 0 |
| "player148" | "player136" | 0 |
...
nebula> LOOKUP ON follow \
WHERE follow.degree == 90 \
YIELD properties(edge).degree;
+-------------+-------------+---------+-------------------------+
| SrcVID | DstVID | Ranking | properties(EDGE).degree |
+-------------+-------------+---------+-------------------------+
| "player150" | "player143" | 0 | 90 |
| "player150" | "player137" | 0 | 90 |
| "player148" | "player136" | 0 | 90 |
...
nebula> LOOKUP ON follow \
WHERE follow.degree == 60 \
YIELD properties(edge).degree AS Degree |\
GO FROM $-.DstVID OVER serve \
YIELD $-.DstVID, properties(edge).start_year, properties(edge).end_year, properties($$).name;
+-------------+------------------+----------------+--------------+
| $-.DstVID | serve.start_year | serve.end_year | $$.team.name |
+-------------+------------------+----------------+--------------+
| "player105" | 2010 | 2018 | "Spurs" |
| "player105" | 2009 | 2010 | "Cavaliers" |
| "player105" | 2018 | 2019 | "Raptors" |
+-------------+------------------+----------------+--------------+
通过Tag列出所有的对应的点/通过Edge type列出边Graph
如果需要通过Tag列出所有的点,或通过Edge type列出边,则Tag、Edge type或属性上必须有至少一个索引。
例如一个Tag player
有属性name
和age
,为了遍历所有包含Tag player
的点ID,Tag player
、属性name
或属性age
中必须有一个已经创建索引。
- 查找所有Tag为
player
的点 VID。nebula> CREATE TAG player(name string,age int); nebula> CREATE TAG INDEX player_index on player(); nebula> REBUILD TAG INDEX player_index; +------------+ | New Job Id | +------------+ | 66 | +------------+ nebula> INSERT VERTEX player(name,age) \ VALUES "player100":("Tim Duncan", 42), "player101":("Tony Parker", 36); # 列出所有的 player。类似于 MATCH (n:player) RETURN id(n) /*, n */。 nebula> LOOKUP ON player; +-------------+ | VertexID | +-------------+ | "player100" | | "player101" | +-------------+
- 查找Edge type为
follow
的所有边的信息。nebula> CREATE EDGE follow(degree int); nebula> CREATE EDGE INDEX follow_index on follow(); nebula> REBUILD EDGE INDEX follow_index; +------------+ | New Job Id | +------------+ | 88 | +------------+ nebula> INSERT EDGE follow(degree) \ VALUES "player100"->"player101":(95); # 列出所有的 follow 边。类似于 MATCH (s)-[e:follow]->(d) RETURN id(s), rank(e), id(d) /*, type(e) */。 nebula)> LOOKUP ON follow; +-------------+-------------+---------+ | SrcVID | DstVID | Ranking | +-------------+-------------+---------+ | "player100" | "player101" | 0 | +-------------+-------------+---------+
统计点或边Graph
统计Tag为player
的点和Edge type为follow
的边。
nebula> LOOKUP ON player |\
YIELD COUNT(*) AS Player_Number;
+---------------+
| Player_Number |
+---------------+
| 51 |
+---------------+
nebula> LOOKUP ON follow | \
YIELD COUNT(*) AS Follow_Number;
+---------------+
| Follow_Number |
+---------------+
| 81 |
+---------------+
Note
使用Graph也可以统计点和边。
最后更新: November 1, 2021