【问题标题】:Google-alike search algorithm类似谷歌的搜索算法
【发布时间】:2011-06-04 05:03:48
【问题描述】:

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

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

这就是我的数据的结构:

参赛作品:

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

标签:

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

EntryTags:

 -------------------
| 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 库编写(当然,随着我的进一步发展,我会保持线程更新)

【问题讨论】:

    标签: php sql algorithm search-engine


    【解决方案1】:

    您可以使用一种Bloom filter(至少这是Google 策略的一部分)。首先,您查找具有所有输入标签的条目。如果您一无所获,请尝试所有缺少一个标签的组合,然后再缺少两个标签......直到您有足够的匹配项。布隆过滤器中的查找非常快,因此可以进行多次查找。

    【讨论】:

    【解决方案2】:

    哇,让我们保持简单(KISS),这太复杂了,不够灵活。

    如何:使用 SQL,对每个搜索词进行一次搜索,并包含一列,为该词的特定相关性放入一个“点”值。总结对这个“点”值的搜索,并通过“点”找到具有最大相关性的结果。

    看看这个:http://www.jarrodgoddard.com/web-development/advanced-web-site-search-with-sql

    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; 
    

    【讨论】:

    • -1 我们忘记使用索引如何,让事情变得慢得要命,这听起来很有趣;除了不回答问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多