Postgresql 类似于带和的Postgres

Postgresql 类似于带和的Postgres,postgresql,Postgresql,有没有办法将此或表达式更改为和 title SIMILAR TO '%(foo|bar)%' 现在,它返回标题中包含foo或bar的所有行。如何返回包含foo和bar但在此顺序中不是必需的行 所以“foo-bar”和“bar-foo”以及“foo-bla-bar”都是有效的结果。但是“foo沙拉”不是你可以这样做: title SIMILAR TO '%(foo%bar|bar%foo)%' 使用简单的像和all(数组[…]): 您只搜索整个单词的情况要复杂一点。您应该使用并检查结果数组是

有没有办法将此或表达式更改为和

title SIMILAR TO '%(foo|bar)%'
现在,它返回标题中包含foo或bar的所有行。如何返回包含foo和bar但在此顺序中不是必需的行

所以“foo-bar”和“bar-foo”以及“foo-bla-bar”都是有效的结果。但是“foo沙拉”不是你可以这样做:

 title SIMILAR TO '%(foo%bar|bar%foo)%'

使用简单的
all(数组[…])

您只搜索整个单词的情况要复杂一点。您应该使用并检查结果数组是否包含搜索词数组(使用):


是的,但这并不优雅,因为我必须生成多个单词的所有可能变体。是的,这很有效。你知道怎么忽略这个案子吗。例如foo和foo,foo是same@StevenSeagull
ILIKE
就是这样的
I
nsensitive
LIKE
@StevenSeagull别忘了加快搜索速度。@klin我刚刚注意到这个解决方案还返回:searched word=“test”它还返回“best”和“latest”有没有一种方法,我只能用这个完整的词?所以只有“测试”才是正确的回答
with the_data(title) as (
values 
    ('foo bar'),
    ('bar foo'),
    ('foo bla bar'),
    ('foo salad')
)
select *
from the_data
where title like all(array['%foo%', '%bar%']);

    title    
-------------
 foo bar
 bar foo
 foo bla bar
(3 rows)
with the_data(title) as (
values 
    ('Foo bar'),
    ('Bar foo'),
    ('foo bla bar'),
    ('foo salad'),
    ('foobar'),
    ('barfoo')
)
select *
from the_data
where regexp_split_to_array(lower(title), ' ') @> array['foo', 'bar'];

    title    
-------------
 Foo bar
 Bar foo
 foo bla bar
(3 rows)