【问题标题】:exact word fulltext search postgresql精确词全文搜索 postgresql
【发布时间】:2018-12-07 10:38:09
【问题描述】:

Postgresql 全文搜索匹配附近的单词,但我想在大型内容表中搜索确切的单词(数十万行中的数千个单词)。 我的搜索必须非常快(不到一秒)。 使用 like 或 ilike 会减慢(200000 字:超过 5 秒)。 有没有人想咨询我?

【问题讨论】:

  • 请说明为什么 PostgreSQL 全文搜索不符合要求。
  • 因为如果我搜索“servant”,例如,postgresql 全文搜索也匹配“server”,但在我的情况下,我希望 postgresql 只匹配确切的单词“servant”,而不是附近的单词,例如“服务器”或其他。
  • 哦,这很简单。只需使用应用词干提取的字典。
  • 我该怎么做?你有例子吗?我使用的是法语词典,但有一个很好解释的英语词典可以帮助我。
  • 我有自己的字典,用这篇文章做了一些改编shisaa.jp/postset/postgresql-full-text-search-part-2.html

标签: postgresql full-text-search exact-match


【解决方案1】:

如果您使用simple 字典并创建适当的 GIN 索引,您应该能够解决 PostgreSQL 全文搜索的问题:

CREATE TABLE haystack (id serial PRIMARY KEY, string text NOT NULL);
INSERT INTO haystack (string) VALUES ('I am your servant');
INSERT INTO haystack (string) VALUES ('What use is a server without a client?');

CREATE INDEX haystack_fts_ind ON haystack USING gin (to_tsvector('simple', string));

让我们禁用顺序扫描以便使用索引,即使示例表太小:

SET enable_seqscan=off;

现在只找到完全匹配,并且不进行词干提取:

SELECT * FROM haystack
WHERE to_tsvector('simple', string) @@ to_tsquery('simple', 'servant');

 id |      string       
----+-------------------
  1 | I am your servant
(1 row)

索引可以用来加速查询:

EXPLAIN (COSTS off) SELECT * FROM haystack
WHERE to_tsvector('simple', string) @@ to_tsquery('simple', 'servant');

                                        QUERY PLAN                                        
------------------------------------------------------------------------------------------
 Bitmap Heap Scan on haystack
   Recheck Cond: (to_tsvector('simple'::regconfig, string) @@ '''servant'''::tsquery)
   ->  Bitmap Index Scan on haystack_fts_ind
         Index Cond: (to_tsvector('simple'::regconfig, string) @@ '''servant'''::tsquery)
(4 rows)

【讨论】:

    猜你喜欢
    • 2011-07-04
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 2023-03-28
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多