【问题标题】:Combine query to get all the matching search text in right order组合查询以正确顺序获取所有匹配的搜索文本
【发布时间】:2020-11-01 22:16:58
【问题描述】:

我有下表:

postgres=# \d so_rum;
                        Table "public.so_rum"
  Column   |          Type           | Collation | Nullable | Default 
-----------+-------------------------+-----------+----------+---------
 id        | integer                 |           |          | 
 title     | character varying(1000) |           |          | 
 posts     | text                    |           |          | 
 body      | tsvector                |           |          | 
 parent_id | integer                 |           |          | 
Indexes:
    "so_rum_body_idx" rum (body)

我想做短语搜索查询,所以我想出了以下查询,例如:

select id from so_rum
    where body @@ phraseto_tsquery('english','Is it possible to toggle the visibility');

这给了我结果,它只匹配整个文本。但是,有一些文档,其中词法之间的距离更大,而上述查询并没有给我返回这些数据。例如:'it is something possible to do toggle between the. . . visibility' 不会被返回。我知道我可以通过手动输入to_tsquery 来使用<2>(例如)距离运算符返回它。

但我想了解,如何在我的 sql 语句本身中执行此操作,以便我首先获得距离为 1 的结果,然后是 2 等等(可能直到 6-7)。最后将搜索词的实际计数附加到结果中,如下查询:

select count(id) from so_rum
    where body @@ to_tsquery('english','string & string . . . ')

是否有可能在单个查询中实现良好的性能?

【问题讨论】:

标签: sql postgresql search indexing full-text-search


【解决方案1】:

我没有看到解决方案。听起来您需要使用 plainto_tsquery 来获取所有词位的所有结果,然后实现您自己的自定义排名函数以根据词位之间的距离对它们进行排名,并可能过滤掉顺序错误的结果。

【讨论】:

    猜你喜欢
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多