1

我有以下几行文字:

"Blue pill"; "Red pill"; "Blue shift"; "Red eye".

我想选择行,Red第一个单词在哪里或Pill第二个单词在哪里。假设,可以使用tsqueryand来完成tsvector,因为 的输出tsvector还包含每个词位的位置。但是我没有找到任何允许通过它们的数字访问向量词素的函数。有没有选择行的正确方法,ts_query在定义的位置匹配?

4

1 回答 1

2

使用 tsvector可以做到这一点:

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)

不过,我认为使用正则表达式可能更容易做到这一点。

于 2019-05-20T14:06:46.483 回答