【问题标题】:Best approach to index a PostgreSQL database with plain html content?用纯 html 内容索引 PostgreSQL 数据库的最佳方法?
【发布时间】:2020-08-22 19:09:55
【问题描述】:

我有一个包含几百万个帖子的数据库,每个帖子都有一个“内容”列,其中包含纯 HTML 格式的帖子内容。

<div class="quoteheader"><a href="http://website.com/message?id=52501">
Quote from: X on October 22, 2013, 02:07:08 PM</a>
</div>
<div class="quote">
Hi, how are you all?
<br></div>
<br>I'm good, how about you?

我想制作一个完整的搜索工具,让人们可以搜索帖子。在这种情况下,有人可以搜索“你好吗”,就会出现这篇文章。

我想过用 gin 创建一个 ts_vector 索引:

CREATE INDEX posts_content_search ON posts using gin(to_tsvector('simple', content));

允许此类搜索。

SELECT * FROM posts WHERE to_tsvector('simple', content) @@ phraseto_tsquery('simple', 'how are you');

但是,在创建它的同时,它不仅不断显示很多这样的消息:

DETAIL:  Words longer than 2047 characters are ignored.
NOTICE:  word is too long to be indexed

但它也会将 html 标签保存在索引中(例如:div、b、a、br...),而最好的方法是删除标签并仅索引发布的真实内容(“嗨,你好吗?你们大家”和“我很好,你们呢”)

创建索引以允许此类搜索的最佳方法是什么?

【问题讨论】:

  • 不要索引整列content,而是仅索引其中的消息(例如Hi, how are you all?I'm good, how about you?)。您需要对数据进行预处理,可能只需删除所有标签即可。

标签: sql postgresql indexing full-text-search


【解决方案1】:

'simple'已经排除了html标签的内容,从strip(to_tsvector('simple',content))的输出可以看出:

                                                         strip                                                         
-----------------------------------------------------------------------------------------------------------------------
 '02' '07' '08' '2013' '22' 'about' 'all' 'are' 'from' 'good' 'hi' 'how' 'i' 'm' 'october' 'on' 'pm' 'quote' 'x' 'you'

注意没有'br'、'div'等。

包含“引自:X”部分是因为它不在标签中。如果你想排除它,你想用什么逻辑来做到这一点?

关于长词的警告可以忽略。如果您需要有关修复它们的建议,您应该向我们展示一个生成它们的示例。

【讨论】:

  • 感谢您的回复。我以为我在将 to_tsvector 与“简单”字典(如“div”)一起使用后看到了一些标签。也许我错了?这就是我在发布问题后稍后使用的内容:CREATE INDEX posts_content_search ON posts using gin(to_tsvector('simple', regexp_replace(regexp_replace(content, E'&lt;br&gt;', ' ', 'gi'), E'Quote from:.*&lt;\\/div&gt;|&lt;[^&gt;]*&gt;|(_|-){3,}', '', 'gi')));。但是现在,这似乎不是最好的主意……我设法看到了较长的单词是什么样的。有些是非常大的链接,例如带有 ruberish 的 google 搜索链接。我会忽略它们。
猜你喜欢
  • 1970-01-01
  • 2012-10-30
  • 2011-09-08
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
  • 2010-11-13
  • 1970-01-01
  • 2017-05-27
相关资源
最近更新 更多