如何使用php和mySQL实施简单的站点搜索? [英] How would I implement a simple site search with php and mySQL?

查看:191
本文介绍了如何使用php和mySQL实施简单的站点搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个允许用户提交报价的网站.我该如何创建一个(相对简单的)搜索来返回最相关的引号?

I'm creating a site that allows users to submit quotes. How would I go about creating a (relatively simple?) search that returns the most relevant quotes?

例如,如果搜索词是土耳其",那么我将返回引号,其中单词土耳其"出现两次,而引号仅出现一次.

For example, if the search term was "turkey" then I'd return quotes where the word "turkey" appears twice before quotes where it only appears once.

(我将添加一些其他规则来帮助过滤掉不相关的结果,但是我主要担心的是.)

(I would add a few other rules to help filter out irrelevant results, but my main concern is that.)

推荐答案

每个人都建议使用MySQL全文搜索,但是您应该注意一个巨大的警告.全文搜索引擎仅适用于MyISAM引擎(不适用于InnoDB,InnoDB由于其参照完整性和ACID兼容性而成为最常用的引擎).

Everyone is suggesting MySQL fulltext search, however you should be aware of a HUGE caveat. The Fulltext search engine is only available for the MyISAM engine (not InnoDB, which is the most commonly used engine due to its referential integrity and ACID compliance).

所以您有一些选择:

1..最简单的方法由粒子树概述.您可以从纯SQL中获得排名搜索(没有全文,没有任何内容).下面的SQL查询将搜索表格并根据搜索字段中出现的字符串的数量对结果进行排名:

1. The simplest approach is outlined by Particle Tree. You can actaully get ranked searches off of pure SQL (no fulltext, no nothing). The SQL query below will search a table and rank results based off the number of occurrences of a string in the search fields:

SELECT
    SUM(((LENGTH(p.body) - LENGTH(REPLACE(p.body, 'term', '')))/4) +
        ((LENGTH(p.body) - LENGTH(REPLACE(p.body, 'search', '')))/6))
    AS Occurrences
FROM
    posts AS p
GROUP BY
    p.id
ORDER BY
    Occurrences DESC

编辑了他们的示例,以使其更加清晰

上述SQL查询的变化,添加WHERE语句(WHERE p.body LIKE'%whatever%you%want')等,可能会完全满足您的需求.

Variations on the above SQL query, adding WHERE statements (WHERE p.body LIKE '%whatever%you%want'), etc. will probably get you exactly what you need.

2..您可以更改数据库架构以支持全文本.通常,无需安装 Sphinx全文搜索引擎之类的插件即可保持InnoDB引用完整性,ACID遵从性和速度.对于MySQL来说,a>是将报价数据拆分成它自己的表.基本上,您将有一个Quotes表,它是一个InnoDB表,而不是您的TEXT字段为"data",而是具有引用"quote_data_id",该引用指向Quote_Data表(即MyISAM表)上的ID.您可以在MyISAM表上使用全文本,将InnoDB表返回的ID结合在一起,然后就可以得到结果.

2. You can alter your database schema to support full text. Often what is done to keep the InnoDB referential integrity, ACID compliance, and speed without having to install plugins like Sphinx Fulltext Search Engine for MySQL is to split the quote data into it's own table. Basically you would have a table Quotes that is an InnoDB table that, rather than having your TEXT field "data" you have a reference "quote_data_id" which points to the ID on a Quote_Data table which is a MyISAM table. You can do your fulltext on the MyISAM table, join the IDs returned with your InnoDB tables and voila you have your results.

3..安装 Sphinx .祝你好运.

鉴于您的描述,我会高度建议您采用我介绍的第一种方法,因为您有一个简单的数据库驱动站点.第一种解决方案很简单,可以快速完成工作. Lucene将非常麻烦地进行设置,尤其是当您要将Lucene与数据库集成时,Lucene的主要目的是索引文件而不是数据库. Google自定义网站搜索只会使您的网站失去大量声誉(使您看起来很业余和被黑),而MySQL全文很可能会导致您更改数据库架构.

Given what you described, I would HIGHLY recommend you take the 1st approach I presented since you have a simple database driven site. The 1st solution is simple, gets the job done quickly. Lucene will be a bitch to setup especially if you want to integrate it with the database as Lucene is designed mainly to index files not databases. Google custom site search just makes your site lose tons of reputation (makes you look amateurish and hacked), and MySQL fulltext will most likely cause you to alter your database schema.

这篇关于如何使用php和mySQL实施简单的站点搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆