2

我在 PostgreSQL 表中有一堆文本行,我正在尝试查找常用字符串。

例如,假设我有一个基本表,例如:

CREATE TABLE a (id serial, value text);
INSERT INTO a (value) VALUES
    ('I go to the movie theater'), 
    ('New movie theater releases'), 
    ('Coming out this week at your local movie theater'),
    ('New exposition about learning disabilities at the children museum'),
    ('The genius found in learning disabilities')
;

我正在尝试在所有行中找到流行的字符串movie theaterlearning disabilities目标是显示像 Twitter“趋势”这样的“趋势”字符串之王的列表)

我使用全文搜索,并且尝试过ts_stat结合使用,ts_headline但结果非常令人失望。

有什么想法吗?谢谢!

4

2 回答 2

4

Posgres 没有现成的文本搜索功能来查找最流行的短语。对于两个单词的短语,您可以使用它ts_stat()来查找最流行的单词、消除助词、介词等,并将这些单词交叉连接以找到最流行的配对。

对于实际数据,您可能希望更改标记为--> parameter.查询在较大数据集上可能会非常昂贵。

with popular_words as (
    select word
    from ts_stat('select value::tsvector from a')
    where nentry > 1                                --> parameter
    and not word in ('to', 'the', 'at', 'in', 'a')  --> parameter
)
select concat_ws(' ', a1.word, a2.word) phrase, count(*) 
from popular_words as a1
cross join popular_words as a2
cross join a
where value ilike format('%%%s %s%%', a1.word, a2.word)
group by 1
having count(*) > 1                                 --> parameter
order by 2 desc;


        phrase         | count 
-----------------------+-------
 movie theater         |     3
 learning disabilities |     2
(2 rows)
于 2017-03-09T19:49:46.633 回答
1

怎么样: SELECT * FROM a WHERE value LIKE '%movie theater%';

这将在 value 列的某处找到与模式“电影院”匹配的行(并且可以在它之前或之后包含任意数量的字符)。

于 2017-03-09T18:37:41.753 回答