全文搜索

全文搜索是基于全文索引对值为字符串类型的属性进行前缀搜索、通配符搜索、正则表达式搜索和模糊搜索。

LOOKUPMATCH语句中,使用WHERE子句指定字符串的搜索条件。

前提条件

请确保已经部署全文索引。详情请参见部署全文索引部署listener

注意事项

使用全文索引前,请确认已经了解全文索引的使用限制

自然语言全文搜索

自然语言搜索将搜索的字符串解释为自然人类语言中的短语。搜索不区分大小写。

语法

  1. LOOKUP ON {<tag> | <edge_type>} WHERE <expression> [YIELD <return_list>];
  2. <expression> ::=
  3. PREFIX | WILDCARD | REGEXP | FUZZY
  4. <return_list>
  5. <prop_name> [AS <prop_alias>] [, <prop_name> [AS <prop_alias>] ...]
  • PREFIX(schema_name.prop_name, prefix_string, row_limit, timeout)

  • WILDCARD(schema_name.prop_name, wildcard_string, row_limit, timeout)

  • REGEXP(schema_name.prop_name, regexp_string, row_limit, timeout)

  • FUZZY(schema_name.prop_name, fuzzy_string, fuzziness, operator, row_limit, timeout)

    • fuzziness:可选项。允许匹配的最大编辑距离。默认值为AUTO。查看其他可选值和更多信息,请参见Elasticsearch官方文档

    • operator:可选项。解释文本的布尔逻辑。可选值为OR(默认)和and

  • row_limit:可选项。指定要返回的行数。默认值为100

  • timeout:可选项。指定超时时间。单位:毫秒(ms)。默认值为200

示例

  1. nebula> CREATE SPACE basketballplayer (partition_num=3,replica_factor=1, vid_type=fixed_string(30));
  2. nebula> SIGN IN TEXT SERVICE (127.0.0.1:9200);
  3. nebula> USE basketballplayer;
  4. nebula> ADD LISTENER ELASTICSEARCH 192.168.8.5:46780;
  5. nebula> CREATE TAG player(name string, age int);
  6. nebula> CREATE TAG INDEX name ON player(name(20));
  7. nebula> INSERT VERTEX player(name, age) VALUES \
  8. "Russell Westbrook": ("Russell Westbrook", 30), \
  9. "Chris Paul": ("Chris Paul", 33),\
  10. "Boris Diaw": ("Boris Diaw", 36),\
  11. "David West": ("David West", 38),\
  12. "Danny Green": ("Danny Green", 31),\
  13. "Tim Duncan": ("Tim Duncan", 42),\
  14. "James Harden": ("James Harden", 29),\
  15. "Tony Parker": ("Tony Parker", 36),\
  16. "Aron Baynes": ("Aron Baynes", 32),\
  17. "Ben Simmons": ("Ben Simmons", 22),\
  18. "Blake Griffin": ("Blake Griffin", 30);
  19. nebula> LOOKUP ON player WHERE PREFIX(player.name, "B");
  20. +-----------------+
  21. | _vid |
  22. +-----------------+
  23. | "Boris Diaw" |
  24. +-----------------+
  25. | "Ben Simmons" |
  26. +-----------------+
  27. | "Blake Griffin" |
  28. +-----------------+
  29. nebula> LOOKUP ON player WHERE WILDCARD(player.name, "*ri*") YIELD player.name, player.age;
  30. +-----------------+-----------------+-----+
  31. | _vid | name | age |
  32. +-----------------+-----------------+-----+
  33. | "Chris Paul" | "Chris Paul" | 33 |
  34. +-----------------+-----------------+-----+
  35. | "Boris Diaw" | "Boris Diaw" | 36 |
  36. +-----------------+-----------------+-----+
  37. | "Blake Griffin" | "Blake Griffin" | 30 |
  38. +-----------------+-----------------+-----+
  39. nebula> LOOKUP ON player WHERE WILDCARD(player.name, "*ri*") | YIELD count(*);
  40. +----------+
  41. | COUNT(*) |
  42. +----------+
  43. | 3 |
  44. +----------+
  45. nebula> LOOKUP ON player WHERE REGEXP(player.name, "R.*") YIELD player.name, player.age;
  46. +---------------------+---------------------+-----+
  47. | _vid | name | age |
  48. +---------------------+---------------------+-----+
  49. | "Russell Westbrook" | "Russell Westbrook" | 30 |
  50. +---------------------+---------------------+-----+
  51. nebula> LOOKUP ON player WHERE REGEXP(player.name, ".*");
  52. +---------------------+
  53. | _vid |
  54. +---------------------+
  55. | "Danny Green" |
  56. +---------------------+
  57. | "David West" |
  58. +---------------------+
  59. | "Russell Westbrook" |
  60. +---------------------+
  61. ...
  62. nebula> LOOKUP ON player WHERE FUZZY(player.name, "Tim Dunncan", AUTO, OR) YIELD player.name;
  63. +--------------+--------------+
  64. | _vid | name |
  65. +--------------+--------------+
  66. | "Tim Duncan" | "Tim Duncan" |
  67. +--------------+--------------+