如何在H2数据库中使用全文搜索?

发布于 2024-11-19 02:45:21 字数 603 浏览 3 评论 0原文

考虑以下示例

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

  1. 我是否必须再次执行此 "PUBLIC"."TEST" WHERE "ID"=1 才能获取包含 'Hello' 单词的记录?
  2. 从 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 .

  1. Do i have to again execute this "PUBLIC"."TEST" WHERE "ID"=1 to get the record containing 'Hello' word ?
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南巷近海 2024-11-26 02:45:22
  1. 是的,使用FT_SEARCH 的查询中的每一行都代表一个架构表行,其中找到了一个关键字。搜索不区分大小写,FT_SEARCHtext 参数可能包含多个单词。例如,

    从测试中删除;
    插入测试值(1, 'Hello World');
    插入测试值(2,'再见世界');
    插入测试值(3, '你好再见');
    调用 FT_REINDEX();
    SELECT * FROM FT_SEARCH('你好再见', 0, 0);
    

    仅返回第三行:

    查询分数  
    “公共”.“测试”,其中“ID”=3 1.0
    

    另请注意,FT_SEARCH_DATA 可用于检索数据本身。例如,

    从 FT_SEARCH_DATA('hello', 0, 0) 中选择 T.* FT,测试 T
    WHERE FT.TABLE='测试' AND T.ID=FT.KEYS[0];
    

    返回包含关键字的两行:

    ID 名称  
    1 你好世界
    3 你好再见
    
  2. Apache Lucene 支持通配符搜索,尽管前导通配符(例如*ell)往往很昂贵。

  1. 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 the text parameter to FT_SEARCH may include more than one word. For example,

    DELETE FROM TEST;
    INSERT INTO TEST VALUES(1, 'Hello World');
    INSERT INTO TEST VALUES(2, 'Goodbye World');
    INSERT INTO TEST VALUES(3, 'Hello Goodbye');
    CALL FT_REINDEX();
    SELECT * FROM FT_SEARCH('hello goodbye', 0, 0);
    

    returns only row three:

    QUERY                           SCORE  
    "PUBLIC"."TEST" WHERE "ID"=3    1.0
    

    Also note that FT_SEARCH_DATA may be used to retrieve the data itself. For example,

    SELECT T.* FROM FT_SEARCH_DATA('hello', 0, 0) FT, TEST T
    WHERE FT.TABLE='TEST' AND T.ID=FT.KEYS[0];
    

    returns both rows containing the keyword:

    ID   NAME  
    1    Hello World
    3    Hello Goodbye
    
  2. Apache Lucene supports wildcard searches, although leading wildcards (e.g. *ell) tend to be expensive.

攒眉千度 2024-11-26 02:45:22

我是否必须再次执行此“PUBLIC”.“TEST” WHERE“ID”=1才能获取包含“Hello”单词的记录?

是的,除非您按照trashgod 的描述使用连接。原因是:通常行比两个单词大得多。例如,一行包含带有文档的 CLOB。如果全文搜索的结果包含数据,那么全文搜索会慢得多。

从 FT_Search 中搜索所有包含“ell”单词的记录的查询是什么。比如H2原生全文搜索中的%ell%

原生全文搜索不能直接做到这一点。原因是:全文搜索仅索引整个单词。 (顺便说一句:如果你只知道一个单词的一部分,Google是否支持搜索?Apache Lucene确实支持它)实际上,对于H2,有一种方法:首先,在单词表(FT.WORDS)中搜索匹配项,然后使用常规搜索。

Do i have to again execute this "PUBLIC"."TEST" WHERE "ID"=1 to get the record containing 'Hello' word ?

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.

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文