full-text-search postgresql

postgresql - Postgres和全文搜索:搜索带有多个负数的短语的正确SQL查询是什么

发布于 2020-03-27 10:19:32

我有一个带description的桌子

在此表中,我添加了一个名为tsvtsvector 的额外列

我正在尝试在tsv列中查询某个短语,但添加否定短语以减少返回的误报的数量,但是我不确定这样做的正确方法是什么,因为我为每个返回的结果数量不同我正在尝试的方式。

例如,像在一条小河中一样,选择stream

我懂了

WHERE tsv @@ to_tsquery('english', '
        stream
    ')

RETURNS: 26

但是在一些描述中,因为我已经手动检查了全部26个,所以它们仅谈论:

...光从两个窗口都流进来...(其中有1个)

要么

...自然光的流...(其中有2个)

这与一点流水无关。

这总共是3个,所以我希望可以返回23个。

到目前为止,这是我尝试过的方法,没有一个返回23:

WHERE tsv @@ to_tsquery('english', '
        stream
        & ! light<->streaming
        | ! stream<2>natural<->light
    ')

>RETURNS: 261

要么

WHERE tsv @@ to_tsquery('english', '
        stream
        & ! light<->streaming
        & ! stream<2>natural<->light
    ')

>RETURNS: 3

要么

WHERE tsv @@ to_tsquery('english', '
        stream
        & ! ( 
            light<->streaming
            | stream<2>natural<->light
        )
    ')

>RETURNS: 8

要么

WHERE tsv @@ to_tsquery('english', '
        stream
        & ( 
            ! light<->streaming
            | ! stream<2>natural<->light
        )
    ')

>RETURNS: 26

我究竟做错了什么?还是有一种完全不同的方式来执行此操作?

提前致谢

加成

如此一来,我就可以肯定并出于自己的理智,我想对每个要取反的术语都使用此代码

WHERE tsv @@ to_tsquery('english',
        'light<->streaming'
     )

>RETURNS: 1
WHERE tsv @@ to_tsquery('english',
        'stream<2>natural<->light'
     )

>RETURNS: 2

两者都返回了我要从主查询中删除的记录。

查看更多

查看更多

提问者
User13342
被浏览
148
User13342 2019-07-25 22:37

跟进

我无法以尝试执行此操作的方式来解决此问题,例如,全部都在一个子句中。

因此,为了解决这个问题,我为每个否定项创建了单独的where子句。

所以代替

...  
WHERE tsv @@ to_tsquery('english',
                        'stream
                         & !(light <-> stream)
                         & !(stream <2> natural <-> light)'
                     );

我做了这个

...
WHERE tsv @@ to_tsquery('english','stream')
AND NOT WHERE tsv @@ to_tsquery('english','light <-> stream')
AND NOT WHERE tsv @@ to_tsquery('english','stream <2> natural <-> light')

这样做可以产生我所期望的结果。

希望这对遇到同样问题的人有所帮助。