LOOKUP 语法

LOOKUP 语句指定过滤条件对数据进行查询。LOOKUP 语句之后通常跟着 WHERE 子句。WHERE 子句用于向条件中添加过滤性的谓词,从而对数据进行过滤。

注意: 在使用 LOOKUP 语句之前,请确保已创建索引。查看索引文档了解有关索引的更多信息。

  1. LOOKUP ON {<vertex_tag> | <edge_type>} WHERE <expression> [ AND | UNION expression ...]) ] [YIELD <return_list>]
  2. <return_list>
  3. <col_name> [AS <col_alias>] [, <col_name> [AS <col_alias>] ...]
  • LOOKUP 语句用于寻找点或边的集合。
  • WHERE 指定被筛选的逻辑条件。同样支持逻辑关键词 AND、UNION、NOT,详情参见 WHERE 的用法。 注意: WHERE 子句在 LOOKUP 中暂不支持如下操作:
    • $-$^
    • 在关系表达式中,暂不支持操作符两边都是field-name 的表达式,如 (tagName.column1 > tagName.column2)
    • 暂不支持运算表达式和 function 表达式中嵌套 AliasProp 表达式。
  • YIELD 指定返回结果。如未指定,则在 LOOKUP 标签时返回点 ID,在 LOOKUP 边类型时返回边的起点 ID、终点 ID 和 ranking 值。

点查询

如下示例返回名称为 Tony Parker,标签为 player 的顶点。

  1. nebula> CREATE TAG INDEX index_player ON player(name, age);
  2. nebula> LOOKUP ON player WHERE player.name == "Tony Parker";
  3. ============
  4. | VertexID |
  5. ============
  6. | 101 |
  7. ------------
  8. nebula> LOOKUP ON player WHERE player.name == "Tony Parker" \
  9. YIELD player.name, player.age;
  10. =======================================
  11. | VertexID | player.name | player.age |
  12. =======================================
  13. | 101 | Tony Parker | 36 |
  14. ---------------------------------------
  15. nebula> LOOKUP ON player WHERE player.name== "Kobe Bryant" YIELD player.name AS name | \
  16. GO FROM $-.VertexID OVER serve YIELD $-.name, serve.start_year, serve.end_year, $$.team.name;
  17. ==================================================================
  18. | $-.name | serve.start_year | serve.end_year | $$.team.name |
  19. ==================================================================
  20. | Kobe Bryant | 1996 | 2016 | Lakers |
  21. ------------------------------------------------------------------

边查询

如下示例返回 degree 为 90,边类型为 follow 的边。

  1. nebula> CREATE EDGE INDEX index_follow ON follow(degree);
  2. nebula> LOOKUP ON follow WHERE follow.degree == 90;
  3. =============================
  4. | SrcVID | DstVID | Ranking |
  5. =============================
  6. | 100 | 106 | 0 |
  7. -----------------------------
  8. nebula> LOOKUP ON follow WHERE follow.degree == 90 YIELD follow.degree;
  9. =============================================
  10. | SrcVID | DstVID | Ranking | follow.degree |
  11. =============================================
  12. | 100 | 106 | 0 | 90 |
  13. ---------------------------------------------
  14. nebula> LOOKUP ON follow WHERE follow.degree == 90 YIELD DISTINCT follow._src;
  15. ===========================================
  16. | SrcVID | DstVID | Ranking | follow._src |
  17. ===========================================
  18. | 121 | 116 | 0 | 121 |
  19. -------------------------------------------
  20. | 140 | 114 | 0 | 140 |
  21. -------------------------------------------
  22. | 142 | 117 | 0 | 142 |
  23. -------------------------------------------
  24. | 133 | 114 | 0 | 133 |
  25. -------------------------------------------
  26. | 143 | 150 | 0 | 143 |
  27. -------------------------------------------
  28. | 114 | 103 | 0 | 114 |
  29. -------------------------------------------
  30. | 136 | 117 | 0 | 136 |
  31. -------------------------------------------
  32. | 127 | 114 | 0 | 127 |
  33. -------------------------------------------
  34. | 147 | 136 | 0 | 147 |
  35. -------------------------------------------
  36. | 118 | 120 | 0 | 118 |
  37. -------------------------------------------
  38. | 128 | 116 | 0 | 128 |
  39. -------------------------------------------
  40. | 138 | 115 | 0 | 138 |
  41. -------------------------------------------
  42. | 129 | 116 | 0 | 129 |
  43. -------------------------------------------
  44. nebula> LOOKUP ON follow WHERE follow.degree == 90 YIELD DISTINCT follow._dst;
  45. ===========================================
  46. | SrcVID | DstVID | Ranking | follow._dst |
  47. ===========================================
  48. | 121 | 116 | 0 | 116 |
  49. -------------------------------------------
  50. | 140 | 114 | 0 | 114 |
  51. -------------------------------------------
  52. | 142 | 117 | 0 | 117 |
  53. -------------------------------------------
  54. | 133 | 114 | 0 | 114 |
  55. -------------------------------------------
  56. | 114 | 103 | 0 | 103 |
  57. -------------------------------------------
  58. | 136 | 117 | 0 | 117 |
  59. -------------------------------------------
  60. | 118 | 120 | 0 | 120 |
  61. -------------------------------------------
  62. | 128 | 116 | 0 | 116 |
  63. -------------------------------------------
  64. nebula> LOOKUP ON follow WHERE follow.degree == 60 YIELD follow.degree AS Degree | \
  65. GO FROM $-.DstVID OVER serve YIELD $-.DstVID, serve.start_year, serve.end_year, $$.team.name;
  66. ================================================================
  67. | $-.DstVID | serve.start_year | serve.end_year | $$.team.name |
  68. ================================================================
  69. | 105 | 2010 | 2018 | Spurs |
  70. ----------------------------------------------------------------
  71. | 105 | 2009 | 2010 | Cavaliers |
  72. ----------------------------------------------------------------
  73. | 105 | 2018 | 2019 | Raptors |
  74. ----------------------------------------------------------------

FAQ

错误码411

  1. [ERROR (-8)]: Unknown error(411):

错误码411表示WHERE过滤时没有有效的索引。Nebula Graph的索引使用的是最左匹配原则,即从最左边的为起点任何连续的索引都能匹配上。例如:

  1. # 为标签t的前三个属性创建索引。
  2. nebula> CREATE TAG INDEX example_index ON TAG t(p1, p2, p3);
  3. # 无法匹配索引,因为不是从p1开始。
  4. nebula> LOOKUP ON t WHERE p2 == 1 and p3 == 1;
  5. # 可以匹配索引。
  6. nebula> LOOKUP ON t WHERE p1 == 1;
  7. # 可以匹配索引,因为p1和p2是连续的。
  8. nebula> LOOKUP ON t WHERE p1 == 1 and p2 == 1;
  9. # 可以匹配索引,因为p1、p2、p3是连续的。
  10. nebula> LOOKUP ON t WHERE p1 == 1 and p2 == 1 and p3 == 1;