组合查询以按正确顺序获取所有匹配的搜索文本

uqdfh47h  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(233)

我有下表:

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' 不会被退回。我知道我可以把它拿回来 <2> (例如)距离运算符 to_tsquery ,手动。
但我想了解,如何在sql语句中实现这一点,以便在距离为的情况下首先得到结果 1 然后 2 以此类推(可能到 6-7 ). 最后,用搜索词的实际计数追加结果,如以下查询:

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

是否可以在单个查询中以良好的性能执行?

igetnqfo

igetnqfo1#

我看不出有什么固定的解决办法。听起来您需要使用plainto\u tsquery来获取所有词素的所有结果,然后实现您自己的自定义排序函数,根据词素之间的距离对它们进行排序,并可能筛选出顺序错误的结果。

相关问题