LOOKUP
LOOKUP
根据索引遍历数据。用户可以使用LOOKUP
实现如下功能:
根据
WHERE
子句搜索特定数据。通过Tag列出点:检索指定Tag的所有点ID。
通过Edge type列出边:检索指定Edge type的所有边的起始点、目的点和rank。
统计包含指定Tag的点或属于指定Edge type的边的数量。
OpenCypher兼容性
本文操作仅适用于原生nGQL。
注意事项
索引会导致写性能大幅降低(降低90%甚至更多)。请不要随意在生产环境中使用索引,除非很清楚使用索引对业务的影响。
如果用
LOOKUP
语句基于指定属性查询时该属性没有索引,系统会在可用的索引中随机选择一个。例如,Tag
player
有属性name
和age
,Tagplayer
本身和属性name
有索引,而属性age
没有索引。当运行LOOKUP ON player WHERE player.age == 36 YIELD player.name;
时,系统会在Tagplayer
和属性age
的索引中随机使用一个。
前提条件
请确保LOOKUP
语句有至少一个索引可用。如果需要创建索引,但是已经有相关的点、边或属性,用户必须在创建索引后重建索引,才能使其生效。
语法
LOOKUP ON {<vertex_tag> | <edge_type>}
[WHERE <expression> [AND <expression> ...]]
[YIELD <return_list>];
<return_list>
<prop_name> [AS <col_alias>] [, <prop_name> [AS <prop_alias>] ...];
WHERE <expression>
:指定遍历的过滤条件,还可以结合布尔运算符AND和OR一起使用。详情请参见WHERE。YIELD <return_list>
:指定要返回的结果和格式。如果只有
WHERE
子句,没有YIELD
子句:LOOKUP
Tag时,返回点ID。LOOKUP
Edge type时,返回起始点ID、目的点ID和rank。
WHERE语句限制
在LOOKUP
语句中使用WHERE
子句,不支持如下操作:
$-
和$^
。- 在关系表达式中,不支持运算符两边都有字段名,例如
tagName.prop1 > tagName.prop2
。 - 不支持运算表达式和函数表达式中嵌套AliasProp表达式。
- 字符串类型索引不支持范围扫描。
- 不支持XOR和NOT运算符。
检索点
返回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 |
============
| 101 |
------------
nebula> LOOKUP ON player \
WHERE player.name == "Tony Parker" \
YIELD player.name, player.age;
=======================================
| VertexID | player.name | player.age |
=======================================
| 101 | Tony Parker | 36 |
---------------------------------------
nebula> LOOKUP ON player \
WHERE player.name == "Kobe Bryant" \
YIELD player.name AS name |\
GO FROM $-.VertexID OVER serve \
YIELD $-.name, serve.start_year, serve.end_year, $$.team.name;
==================================================================
| $-.name | serve.start_year | serve.end_year | $$.team.name |
==================================================================
| Kobe Bryant | 1996 | 2016 | Lakers |
------------------------------------------------------------------
检索边
返回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 |
+-------------+-------------+---------+
| "player101" | "player102" | 0 |
+-------------+-------------+---------+
| "player133" | "player114" | 0 |
+-------------+-------------+---------+
| "player133" | "player144" | 0 |
+-------------+-------------+---------+
...
nebula> LOOKUP ON follow \
WHERE follow.degree == 90 \
YIELD follow.degree;
+-------------+-------------+---------+---------------+
| SrcVID | DstVID | Ranking | follow.degree |
+-------------+-------------+---------+---------------+
| "player101" | "player102" | 0 | 90 |
+-------------+-------------+---------+---------------+
| "player133" | "player114" | 0 | 90 |
+-------------+-------------+---------+---------------+
| "player133" | "player144" | 0 | 90 |
+-------------+-------------+---------+---------------+
...
nebula> LOOKUP ON follow \
WHERE follow.degree == 60 \
YIELD follow.degree AS Degree |\
GO FROM $-.DstVID OVER serve \
YIELD $-.DstVID, serve.start_year, serve.end_year, $$.team.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列出边
如果需要通过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;
+-------------+
| _vid |
+-------------+
| "player100" |
+-------------+
| "player101" |
+-------------+
查找Edge type为
like
的所有边的信息。nebula> CREATE EDGE like(likeness int);
nebula> CREATE EDGE INDEX like_index on like();
nebula> REBUILD EDGE INDEX like_index;
+------------+
| New Job Id |
+------------+
| 88 |
+------------+
nebula> INSERT EDGE like(likeness) \
VALUES "player100"->"player101":(95);
# 列出所有的 like 边。类似于 MATCH (s)-[e:like]->(d) RETURN id(s), rank(e), id(d) /*, type(e) */。
nebula)> LOOKUP ON like;
+-------------+----------+-------------+
| _src | _ranking | _dst |
+-------------+----------+-------------+
| "player100" | 0 | "player101" |
+-------------+----------+-------------+
统计点或边
统计Tag为player
的点和Edge type为like
的边。
nebula> LOOKUP ON player |\
YIELD COUNT(*) AS Player_Number;
+---------------+
| Player_Number |
+---------------+
| 2 |
+---------------+
nebula> LOOKUP ON like | \
YIELD COUNT(*) AS Like_Number;
+-------------+
| Like_Number |
+-------------+
| 1 |
+-------------+