谷歌一样的搜索算法

Jan*_*aek 3 php sql algorithm search-engine

我试图在我的简单数据结构中实现搜索算法.然而,这不是"如何做到这一点?" - 问题,而是"我怎么能优化算法?"

我试图保留文件索引,每个文件可以与任意数量的标签相关联(类似于一个类别)

这就是我的数据结构:

项:

 ------------------------------------
|  id  | description | short | score | 
 ------------------------------------
Run Code Online (Sandbox Code Playgroud)

标签:

 -------------
|  id  | text |
 -------------
Run Code Online (Sandbox Code Playgroud)

EntryTags:

 -------------------
| entry_id | tag_id |
 -------------------
Run Code Online (Sandbox Code Playgroud)

在搜索字段中,搜索请求将始终转换为使用加号(+)拆分的单个单词.

在下面的例子中,我将搜索"blue + website + simple + layout"

- split searchterm up into array named t
- convert each word in array t into a number using the id from "Tags" table
- for each element in array t, select make new array for each element with "EntryTags" matching the search
- generate array A, where elements that are in all 4 arrays are put into
- generate array B, where elements that are in 3 of the 4 arrays are put into
- generate array C, where elements that are in 2 of the 4 arrays are put into
- generate array D with the last elemenets rest
- sort array A,B,C and D by the score parameter from the table
- output array A, then B, then C, then D
Run Code Online (Sandbox Code Playgroud)

当然这不是优化或任何东西,但我缺乏更复杂的SQL经验踢我的屁股:(

最后,所有这些都将用PHP和mysqli库编写(当我进一步推进时,我将保持线程更新)

Lan*_*dei 5

您可以使用一种Bloom过滤器(至少这是Google战略的一部分).首先,您要查找包含所有输入标签的条目.如果您什么都没找到,请尝试丢失一个标签的所有组合,然后丢失两个标签......直到您有足够的匹配.Bloom过滤器中的查找速度非常快,因此可以进行多次查找.