【发布时间】: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