php - 类似谷歌的搜索算法

标签 php sql algorithm search-engine

我正在尝试在我的简单数据结构中实现搜索算法。但是,这不是“我该怎么做?”的问题,而是“我如何优化算法?”

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

这是我的数据结构:

条目:

 ------------------------------------
|  id  | description | short | score | 
 ------------------------------------

标签:

 -------------
|  id  | text |
 -------------

入口标签:

 -------------------
| entry_id | tag_id |
 -------------------

在搜索字段中,搜索请求将始终变成用加号 (+) 分开的单个词。

在下面的示例中,我将搜索“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

当然这没有优化或任何东西,但我缺乏使用更复杂的 SQL 的经验让我很不爽 :(

最后,所有这些都将用 PHP 和 mysqli 库编写(当然,我会随着我的进一步发展保持线程更新)

最佳答案

你可以使用一种 Bloom filter (至少这是谷歌战略的一部分)。首先,您查找具有所有输入标签的条目。如果您什么也没找到,请尝试所有缺少一个标签的组合,然后尝试缺少两个标签……直到您有足够的匹配项。 Bloom filter中的lookup非常快,所以lookup的次数多也没问题。

关于php - 类似谷歌的搜索算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6232396/

相关文章:

java - 这个解决方案可以优化吗?

algorithm - Dijkstra、Bellman ford 和拓扑最短路径算法的限制?

PHP PDO 使用 SSL 连接到 MS SQLServer Express

mysql - SQL:为具有多个 ID 的项目创建唯一 ID

sql - 在 Postgres 中搜索整数数组

php - 强制 MySQL CHAR() 列要求所有数字或自动左补零

java - 这是什么算法?盒装/背包?

PHP - 回显时通过表单发送的 POST 数据不正确?

php - 在对象被转换时返回特定值的对象上的函数

php - 正则表达式/通配符替换字符串 PHP