【发布时间】:2021-01-24 21:23:15
【问题描述】:
- 让我们有大量的帖子。
- 作为用户,我想查找所有包含“hello”和“world”的帖子。
- 假设有一个帖子包含这样的文字“Hello world,这个地方很漂亮”。
现在:
- a) 如果用户搜索“hello”,则查找文本,
- b) 查找文本 如果用户搜索“hello”、“world”、
- c) 如果用户搜索“hello”、“world”、“funny”,则不会找到文本。
为了减少可能的候选人数量,我正在考虑这样做:
for each post (
if number_of_search_words == number_of_post_words -> proceed with search logic
if number_of_search_words < number_of_post_words -> proceed with search logic
if number_of_search_words > number_of_post_words -> don't proceed with search logic
)
但这也需要一个包含每个帖子字数的数字,这会导致更复杂。
有没有优雅的方法来解决这个问题?
【问题讨论】:
-
通常,您会为大量帖子中的所有单词创建一个索引。然后,对于多词搜索,您计算每个词的帖子列表的 intersection 或 union。例如,假设“hello”的索引列表是 {1,2,5},“world”的索引列表是 {2,4,5}。搜索包含“hello”和“world”的帖子将返回帖子 2 和 5。但搜索包含“hello”或“world”的帖子将返回帖子 1、2、4 和 5。
标签: algorithm search full-text-search