首页 > 解决方案 > 使用部分匹配的 POSTGRESQL 在字符串中搜索多个单词

问题描述

我目前在这样的字符串中搜索多个单词:

select name from restaurant where regexp_split_to_array(lower(name), '\s+') @> array['bar', 'food'];

它将字符串拆分为这样的数组: The Food Bar = [the, food, bar]

但它只有在完整的单词都在字符串中时才有效。例如,如果我使用“ba”和“foo”,我想得到相同的结果。我怎样才能实现它?

标签: sqlpostgresql

解决方案


全文搜索可以解决问题:

WHERE to_tsvector('simple', name) @@ to_tsquery('simple', 'the:* | food:* | bar:*')

推荐阅读