【问题标题】:Postgres Full Text Search - Find Other Similar DocumentsPostgres 全文搜索 - 查找其他类似文档
【发布时间】:2020-07-30 12:33:04
【问题描述】:

我正在寻找一种方法来使用 Postgres(9.6+ 版)全文搜索来查找与输入文档相似的其他文档 - 本质上是在寻找一种产生与 Elasticsearch's more_like_this 查询类似的结果的方法。据我所知,Postgres 无法将 ts_vectors 相互比较。

我尝试了各种技术,例如将源文档转换回ts_query,或重新处理原始文档,但这需要太多开销。

非常感谢任何建议 - 谢谢!

【问题讨论】:

  • 我同意:用于获取两个 ts_vector 之间的距离度量的函数确实是一个不错的功能。

标签: postgresql full-text-search


【解决方案1】:

看起来唯一的选择是使用 pg_trgm 而不是 Postgres 内置的全文搜索。以下是我最终实现的方式:

使用一个简单的表格(在这种情况下是物化视图) - 它包含帖子的主键和两列中的全文正文。

Materialized view "public.text_index"
Column |  Type   | Collation | Nullable | Default | Storage  | Stats target | Description 
--------+---------+-----------+----------+---------+----------+--------------+-------------
id     | integer |           |          |         | plain    |              | 
post   | text    |           |          |         | extended |              | 

View definition:
SELECT posts.id,
posts.body AS text
FROM posts
ORDER BY posts.publication_date DESC;

然后使用横向连接,我们可以匹配行并按similarity 对它们进行排序,以查找与任何其他帖子“接近”或“相关”的帖子:

select * from text_index tx 
left join lateral (
  select similarity(tx.text, t.text) from text_index t where t.id = 12345
) s on true 
order by similarity 
desc limit 10;

这当然是一种简单的匹配文档的方法,可能需要进一步调整。此外,在文本列上使用 tgrm gin 索引将显着加快搜索速度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 2018-02-03
    相关资源
    最近更新 更多