如何在H2数据库中使用全文搜索?
考虑以下示例
CREATE ALIAS IF NOT EXISTS FT_INIT FOR "org.h2.fulltext.FullText.init";
CALL FT_INIT();
DROP TABLE IF EXISTS TEST;
CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR);
INSERT INTO TEST VALUES(1, 'Hello World');
CALL FT_CREATE_INDEX('PUBLIC', 'TEST', NULL);
,我已执行以下查询,
SELECT * FROM FT_SEARCH('Hello', 0, 0);
但此查询返回 "PUBLIC"."TEST" WHERE "ID"=1 。
- 我是否必须再次执行此
"PUBLIC"."TEST" WHERE "ID"=1
才能获取包含 'Hello' 单词的记录? - 从 FT_Search 中搜索所有包含“ell”单词的记录的查询是什么。比如H2原生全文搜索中的%ell%
Consider the following example
CREATE ALIAS IF NOT EXISTS FT_INIT FOR "org.h2.fulltext.FullText.init";
CALL FT_INIT();
DROP TABLE IF EXISTS TEST;
CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR);
INSERT INTO TEST VALUES(1, 'Hello World');
CALL FT_CREATE_INDEX('PUBLIC', 'TEST', NULL);
and i have executed the following query
SELECT * FROM FT_SEARCH('Hello', 0, 0);
But this query is returning "PUBLIC"."TEST" WHERE "ID"=1 .
- Do i have to again execute this
"PUBLIC"."TEST" WHERE "ID"=1
to get the record containing 'Hello' word ? - What is the query to search all records with 'ell' word in them from the FT_Search. such as like %ell% in H2 Native Full-Text Search
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,使用
FT_SEARCH
的查询中的每一行都代表一个架构表行,其中找到了一个关键字。搜索不区分大小写,FT_SEARCH
的text
参数可能包含多个单词。例如,仅返回第三行:
另请注意,
FT_SEARCH_DATA
可用于检索数据本身。例如,返回包含关键字的两行:
Apache Lucene 支持通配符搜索,尽管前导通配符(例如*ell)往往很昂贵。
Yes, each row in a query using
FT_SEARCH
represents a schema-table-row where one of the key words was found. The search is case insensitive, and thetext
parameter toFT_SEARCH
may include more than one word. For example,returns only row three:
Also note that
FT_SEARCH_DATA
may be used to retrieve the data itself. For example,returns both rows containing the keyword:
Apache Lucene supports wildcard searches, although leading wildcards (e.g. *ell) tend to be expensive.
是的,除非您按照trashgod 的描述使用连接。原因是:通常行比两个单词大得多。例如,一行包含带有文档的 CLOB。如果全文搜索的结果包含数据,那么全文搜索会慢得多。
原生全文搜索不能直接做到这一点。原因是:全文搜索仅索引整个单词。 (顺便说一句:如果你只知道一个单词的一部分,Google是否支持搜索?Apache Lucene确实支持它)实际上,对于H2,有一种方法:首先,在单词表(FT.WORDS)中搜索匹配项,然后使用常规搜索。
Yes, except if you use a join as described by trashgod. The reason is: usually rows are much larger than just two words. For example, a row contains a CLOB with a document. If the result of the fulltext search would contain the data, then fulltext search would be much slower.
The native fulltext search can't do that directly. The reason is: fulltext search only indexes whole words. (By the way: does Google support searches if you only know a part of a word? Apache Lucene does support it) Actually, for H2, there would be a way: first, search the words table (FT.WORDS) for matches, and then use a regular search.