在可能有多个值的列上使用 posgresql to_tsquery 前缀匹配

问题描述 投票:0回答:1

如果只查询一个单词,这效果很好:

select to_tsvector('english', 'matchsticks only applicable things'), to_tsquery('english', 'match:*'),
to_tsvector('english', 'matchsticks only applicable things') @@ to_tsquery('english', 'match:*')

现在我怎样才能让它与像 db_column 这样的列一起工作,它可以有带空格的字符串:

select to_tsvector('english', 'matchsticks only applicable things'), to_tsquery('english', db_column),
to_tsvector('english', 'matchsticks only applicable things') @@ to_tsquery('english', db_column)

例如,如果

db_column = "match test square"
并且我想通过
"match:*&test:*&square:*"
?尝试 plainto_tsquery,但缺少 ':*' 来允许前缀匹配

postgresql full-text-search matching
1个回答
0
投票

您可以操作 tsquery 字符串:

select  (replace(
          plainto_tsquery('english', 'match test square')::text,
          ' &',
          ':*&') || ':*'
         )::tsquery;


             tsquery
----------------------------------
 'match':* & 'test':* & 'squar':*
(1 row)
© www.soinside.com 2019 - 2024. All rights reserved.