我有以下几行案文:
"Blue pill"; "Red pill"; "Blue shift"; "Red eye"。
我想选择行,其中Red是第一个单词,或者Pill是第二个单词。假设情况下,可以使用tsquery和tsvector来完成,因为tsvector的输出也包含每个词的位置。然而,我没有发现任何函数允许访问向量的数字。是否有适当的方法来选择行,在定义的位置匹配ts_query?
发布于 2019-05-20 14:06:46
使用to实现这一点是可能的:
with data as (
select * from (
VALUES (1, 'Blue pill'),
(2, 'Red pill'),
(3, 'Blue shift'),
(4, 'Red eye')
) v(id, t)
)
select id, lexeme, positions
FROM data
CROSS JOIN unnest(to_tsvector(t)) u(lexeme, positions, weights)
WHERE (lexeme = 'red' and positions @> '{1}')
OR (lexeme = 'pill' and positions @> '{2}');
id | lexeme | positions
----+--------+-----------
1 | pill | {2}
2 | pill | {2}
2 | red | {1}
4 | red | {1}
(4 rows)不过,我认为使用正则表达式可能更容易做到这一点。
https://stackoverflow.com/questions/56221163
复制相似问题