Php 谷歌相似搜索算法
我试图在我的简单数据结构中实现一个搜索算法。然而,这不是一个“我如何做到这一点?”的问题,而是一个“我如何优化算法?” 我试图保存一个文件索引,每个文件都可以与任意数量的标签相关联(就像一个类别) 以下是我的数据的结构: 参赛作品:Php 谷歌相似搜索算法,php,sql,algorithm,search-engine,Php,Sql,Algorithm,Search Engine,我试图在我的简单数据结构中实现一个搜索算法。然而,这不是一个“我如何做到这一点?”的问题,而是一个“我如何优化算法?” 我试图保存一个文件索引,每个文件都可以与任意数量的标签相关联(就像一个类别) 以下是我的数据的结构: 参赛作品: ------------------------------------ | id | description | short | score | ------------------------------------ 标签: 入口标签: -----
------------------------------------
| id | description | short | score |
------------------------------------
标签:
入口标签:
-------------------
| entry_id | tag_id |
-------------------
在搜索字段中,搜索请求将始终转换为以加号(+)分隔的单个单词
在下面的例子中,我将搜索“蓝色+网站+简单+布局”
当然,这并没有经过优化,但我缺乏使用更复杂SQL的经验,这让我很恼火:(
最后,所有这些都将用PHP和mysqli库编写(当然,随着我的进一步进步,我会不断更新线程)哇,让我们保持简单(KISS),这太复杂了,不灵活 这样如何:使用SQL,对每个搜索词进行一次搜索,并包含一列,该列为该词的特定相关性输入一个“点”值。对该“点”值的搜索进行汇总,并通过“点”找到相关性最大的结果 看看这个:
你可以使用一种(至少这是谷歌战略的一部分)。首先,您查找包含所有输入标记的条目。如果没有找到任何内容,请尝试所有组合,其中一个标记缺失,然后两个标记缺失……直到您有足够的匹配项。Bloom筛选器中的查找非常快,因此可以进行多次查找。-1不如我们忘记使用索引,让事情变得非常缓慢,t这听起来很有趣;除了不回答这个问题之外。+1个有趣的想法。这里有一个使用bloom过滤器优化SQL的链接:
-------------------
| entry_id | tag_id |
-------------------
- 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
SELECT title, filename, sum(relevance)
FROM (
SELECT title, filename, 10 AS relevance FROM page WHERE title like ‘%about%’
UNION
SELECT title, filename, 7 AS relevance FROM page WHERE filename like ‘%about%’
UNION
SELECT title, filename, 5 AS relevance FROM page WHERE keywords like ‘%about%’
UNION
SELECT title, filename, 2 AS relevance FROM page WHERE description like ‘%about%’
) results
GROUP BY title, filename
ORDER BY relevance desc;