【问题标题】:Is there a tool or a postgres feature that I can use to recreate google news alerts?是否有可用于重新创建谷歌新闻提醒的工具或 postgres 功能?
【发布时间】:2015-05-27 05:05:21
【问题描述】:

我在 postgres 中有 news_alertsnews_items 表,索引如下:

CREATE TABLE news_items (
  id INTEGER NOT NULL,
  content character varying
);
CREATE TABLE news_alerts (
  id INTEGER NOT NULL,
  user_id INTEGER NOT NULL,
  terms character varying(255)
);
CREATE INDEX news_alerts_terms ON news_alerts
  USING gin (to_tsvector('english'::regconfig, (terms)::text));

还有以下新闻提醒:

INSERT INTO news_alerts (user_id, terms) values (1, 'Jim Jarmusch');
INSERT INTO news_alerts (user_id, terms) values (1, 'Kim Kardashian');
INSERT INTO news_alerts (user_id, terms) values (2, 'Kim Kardashian');

当有新消息出现时,我将其添加到news_items

INSERT INTO news_items (content) values ('Breaking: Kim Kardashian posts unconscionable new selfies from birthday party');

在这一点上,我想提醒两个(尽管不明智地)选择接收金卡戴珊新闻的用户。我想找到所有termscontent 匹配的news_alerts news_item 并通知这些用户。

有没有办法通过 postgres 索引甚至外部工具或服务来做到这一点?

【问题讨论】:

  • 如果您将相关表的 CREATE TABLE 语句和一些 INSERT 语句粘贴到您的问题中(至少是 news_items 和 news_alerts 表),您会吸引更多的兴趣。我们大多数人可以根据不完整的描述对表格进行逆向工程,但我们并不总是选择以这种方式投入时间。
  • 感谢@MikeSherrill'CatRecall'。我添加了表定义。 INSERT 将符合预期。
  • @muirbot 你能发布一些插页吗?它将允许有兴趣的人进行测试,而不必自己发明一个虚拟数据集。

标签: database postgresql search indexing


【解决方案1】:

您可以尝试滥用 ts_debug 功能。要点如下。 t 表将包含您的新闻提醒。你可能不应该直接使用ts_debug - 它是一个sql语言函数,看看它是如何实现的。

当您有很多行时,您不需要禁用顺序扫描。我这样做只是为了表明可以使用索引。

这有点简洁,有问题可以问。

db=# create temp table t as select array_agg(distinct lexeme) as x from (select unnest(lexemes) as lexeme from ts_debug('english', 'Kim Kardashian')) a;
SELECT 1
db=# create index on t using gin(x);
CREATE INDEX
db=# set enable_seqscan to off;
SET
db=# select * from t where x <@ (select array_agg(distinct lexeme) as x from (select unnest(lexemes) as lexeme from ts_debug('english', 'Kim Kardashian is a woman')) a);
        x         
------------------
 {kardashian,kim}
(1 row)

db=# explain select * from t where x <@ (select array_agg(distinct lexeme) as x from (select unnest(lexemes) as lexeme from ts_debug('english', 'Kim Kardashian is a woman')) a);
                                    QUERY PLAN                                     
-----------------------------------------------------------------------------------
 Bitmap Heap Scan on t  (cost=1765.76..1769.78 rows=1 width=32)
   Recheck Cond: (x <@ $0)
   InitPlan 1 (returns $0)
     ->  Aggregate  (cost=1757.75..1757.76 rows=1 width=32)
           ->  Function Scan on ts_debug  (cost=0.25..507.75 rows=100000 width=32)
   ->  Bitmap Index Scan on t_x_idx  (cost=0.00..8.00 rows=1 width=0)
         Index Cond: (x <@ $0)
(7 rows)

【讨论】:

    猜你喜欢
    • 2022-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多