【发布时间】:2010-10-07 20:43:58
【问题描述】:
搜索引擎和数据库允许您使用连续的字符串搜索(例如"this is a test"),它匹配this is a test that will match,但不会匹配test this is a。
我知道有些数据库具有内置功能,允许您使用相同的功能而无需编写任何代码(例如 MySQL 的全文搜索)。这不是我要寻找的答案。
我想知道的是使用什么样的算法和数据库结构来实现这种快速的字符串搜索。
在上面的示例中,索引表会是什么样子?会不会是类似的东西?
// IndexedItemID | Position | Word
1 | 0 | this
1 | 1 | is
1 | 2 | a
1 | 3 | test
1 | 4 | that
1 | 5 | will
1 | 6 | match
2 | 0 | test
2 | 1 | this
2 | 2 | is
2 | 3 | a
既然有索引项,那么如何有效地创建与这些项匹配的 SQL 语句?
这是我能想到的一个例子:
select IndexedItemID form
(select IndexedItemID, Position from indexedWords where Word = "this") as word1Position
where
exists(select * from indexedWords where IndexedItemID = word1Position.IndexedItemID AND Word = "is" AND Position = word1Position.Position + 1)
AND exists(select * from indexedWords where IndexedItemID = word1Position.IndexedItemID AND Word = "a" AND Position = word1Position.Position + 2)
AND exists(select * from indexedWords where IndexedItemID = word1Position.IndexedItemID AND Word = "test" AND Position = word1Position.Position + 3)
我确信可能有更标准化的方式更有效。
【问题讨论】:
-
MySQL的全文索引是开源的afaik,所以下载并找出:)
标签: algorithm database-design search