首批通过分布式安全可靠测评,为关键业务系统打造
全文查询
更新时间:2024-09-20 23:00:00
全文查询是指在文本数据中进行全文搜索或检索的操作。它用于查找包含特定关键词、短语或文本表达式的文本内容。全文查询能够更全面地搜索整个文本,并返回与搜索条件匹配的结果。
语法
使用以下语法进行全文索引查询时,可以根据指定的列和关键字或短语来执行全文搜索,并可以选择使用特定的搜索修饰符来调整搜索模式(即搜索条件或规则)。
MATCH (column_name [, column_name ...]) AGAINST (expr [search_modifier])
search_modifier:
IN NATURAL LANGUAGE MODE
注意
当前版本仅支持在 WHERE 子句后使用全文查询过滤。
相关参数说明如下:
column_name:指定要进行全文搜索的列。如果要列出多个列,列之间需要使用英文逗号分隔。expr:指定要搜索的关键字或短语。search_modifier:可选项,用来指定搜索模式。取值如下:IN NATURAL LANGUAGE MODE:默认值,用于指定使用自然语言搜索模式进行搜索。说明
搜索模式当前仅支持
IN NATURAL LANGUAGE MODE。
示例
创建表
tbl1,同时创建全文索引full_idx1_tbl1。CREATE TABLE tbl1(col1 INT PRIMARY KEY, col2 VARCHAR(100), col3 TEXT, FULLTEXT INDEX full_idx1_tbl1(col2, col3));向表
tbl1中添加测试数据。INSERT INTO tbl1 (col1, col2, col3) VALUES (1, 'Hello World', 'This is a test'), (2, 'OceanBase', 'OceanBase Database is a native, enterprise-level distributed database developed independently by the OceanBase team'), (3, 'Database Management', 'Learn about SQL and database administration'), (4, 'Full Text Searching', 'Master the art of full text searching');返回结果如下:
Query OK, 4 rows affected Records: 4 Duplicates: 0 Warnings: 0通过在
MATCH子句指定对表tbl1中的col2列和col3列进行搜索,搜索的关键字是 ‘OceanBase’。同时,采用了搜索修饰符IN NATURAL LANGUAGE MODE,表示使用自然语言搜索模式进行搜索。
SELECT * FROM tbl1
WHERE MATCH (col2, col3) AGAINST ('OceanBase' IN NATURAL LANGUAGE MODE);
返回结果如下:
+------+-----------+---------------------------------------------------------------------------------------------------------------------+
| col1 | col2 | col3 |
+------+-----------+---------------------------------------------------------------------------------------------------------------------+
| 2 | OceanBase | OceanBase Database is a native, enterprise-level distributed database developed independently by the OceanBase team |
+------+-----------+---------------------------------------------------------------------------------------------------------------------+
1 row in set