SQLite3 多FTS表搜索

发布于 2024-11-14 17:05:46 字数 747 浏览 2 评论 0原文

我的 sqlite 数据库中有三个 fts 虚拟表 - 两个具有 1 个内容列,另一个具有 2 列 - 标题和内容。假设一张表用于存放某些文章的内容和标题,另外两张表用于存放评论及其注释。有没有一种方法可以在一个查询中对所有这些进行搜索?我需要合并所有结果并按相关性对它们进行排序,首先是文章及其标题,然后是评论和注释。我已经做了一些解决方法。我用搜索结果填充一个表格,以便在每次新搜索时显示,但我认为这不是一个好主意:

DROP TABLE IF EXISTS srch_res;
CREATE TABLE srch_res(type integer, docid integer, snippet text, rank numeric);"
INSERT INTO srch_res SELECT '0', docid, snippet(reviews_c), rank(matchinfo(reviews_c), 0.5) FROM reviews_c WHERE reviews_c MATCH '%s';
INSERT INTO srch_res SELECT '1', docid, snippet(notes_c), rank(matchinfo(notes_c), 0.25) FROM notes_c WHERE notes_c MATCH '%s';
INSERT INTO srch_res SELECT '2', docid, snippet(arts_c), rank(matchinfo(arts_c), 1.0, 0.75) FROM arts_c WHERE arts_c MATCH '%s';

I have three fts virtual tables in my sqlite database - two with one content column, and one with 2 columns - title and content. Say one table is for some articles' content and titles, and other two is for reviews and their notes. Is there some way to commit a search among them all in one query? I need to merge all results and sort them by relevance, first the articles and their titles, then reviews and notes. I have made some workaround. I fill a table with search results to display on every new search, but I think that is not very good idea:

DROP TABLE IF EXISTS srch_res;
CREATE TABLE srch_res(type integer, docid integer, snippet text, rank numeric);"
INSERT INTO srch_res SELECT '0', docid, snippet(reviews_c), rank(matchinfo(reviews_c), 0.5) FROM reviews_c WHERE reviews_c MATCH '%s';
INSERT INTO srch_res SELECT '1', docid, snippet(notes_c), rank(matchinfo(notes_c), 0.25) FROM notes_c WHERE notes_c MATCH '%s';
INSERT INTO srch_res SELECT '2', docid, snippet(arts_c), rank(matchinfo(arts_c), 1.0, 0.75) FROM arts_c WHERE arts_c MATCH '%s';

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

櫻之舞 2024-11-21 17:05:46

使用:

 TRUNCATE TABLE srch_res;

INSERT INTO srch_res (type, docid, snippet, rank)
    SELECT '0', docid, snippet(reviews_c), rank(matchinfo(reviews_c), 0.5)
      FROM reviews_c
     WHERE reviews_c MATCH '%s'
 UNION ALL
    SELECT '1', docid, snippet(notes_c), rank(matchinfo(notes_c), 0.25)
      FROM notes_c
     WHERE notes_c MATCH '%s'
 UNION ALL
    SELECT '2', docid, snippet(arts_c), rank(matchinfo(arts_c), 1.0, 0.75)
      FROM arts_c
     WHERE arts_c MATCH '%s'
;

Use:

 TRUNCATE TABLE srch_res;

INSERT INTO srch_res (type, docid, snippet, rank)
    SELECT '0', docid, snippet(reviews_c), rank(matchinfo(reviews_c), 0.5)
      FROM reviews_c
     WHERE reviews_c MATCH '%s'
 UNION ALL
    SELECT '1', docid, snippet(notes_c), rank(matchinfo(notes_c), 0.25)
      FROM notes_c
     WHERE notes_c MATCH '%s'
 UNION ALL
    SELECT '2', docid, snippet(arts_c), rank(matchinfo(arts_c), 1.0, 0.75)
      FROM arts_c
     WHERE arts_c MATCH '%s'
;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文