[英]Sort by Relevance in a basic mysql search engine
i have a table in mysql. 我在mysql中有一张桌子。 it contains links, titles, descriptions and keywords for about one hundred websites.
它包含约一百个网站的链接,标题,描述和关键字。 Not every site has a keywords field.
并非每个站点都有一个关键字字段。 i crawled Apple.com on a web crawler i wrote and i indexed the majoirty of apple's sites.
我在编写的网络搜寻器上搜寻Apple.com,并为苹果网站的美感编制了索引。
i linked the table up to my search engine and i had a problem. 我将表格链接到搜索引擎,但出现了问题。 the code i was using doesn't sort by relevance.
我使用的代码没有按相关性排序。 by that, i mean if a user types in 'Apple' into the search engine, it will return fairly random results.
通过这种方式,我的意思是,如果用户在搜索引擎中输入“ Apple”,它将返回相当随机的结果。 if i type in 'buy ipad' the ipad would come up instead of the apple store.
如果我输入“购买ipad”,则ipad会出现,而不是苹果商店。 i want the query to scan all the fields to see how many times each user input is in the field and then rank by most relevant.
我希望查询扫描所有字段,以查看每个用户输入在该字段中的次数,然后按最相关的排名。 *f there is a clash, the higher id wins...basically.
*如果发生冲突,则较高的ID会赢...基本上。
btw, if i typed in iphone or ipad, the correct results would show. 顺便说一句,如果我输入了iphone或ipad,则会显示正确的结果。 i have two different queries for seperate and together terms and would love to also know how to mix them together to have 1 query.
我对单独的术语和共同的术语有两个不同的查询,也很想知道如何将它们混合在一起以进行1个查询。
Terms together:
$query = " SELECT * FROM scan WHERE ";
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0) {
$query .= "AND";
}
$query.= "Match(title, description, keywords, link) Against ('".implode(' ', $terms )." in boolean mode);
}
Terms Seperate
$query = " SELECT * FROM scan WHERE ";
$terms = array_map('mysql_real_escape_string', $terms);
$i = 0;
foreach ($terms as $each) {
if ($i++ !== 0) {
$query .= "or";
}
$query.= "Match(title, description, keywords, link) Against ('".implode(' ', $terms )." in boolean mode);
}
You need to use an alias for your MATCH statement. 您需要为您的MATCH语句使用别名。
Select *, MATCH(tags) AGAINST ('search query' IN BOOLEAN MODE) as relevance
from scan HAVING relevance >0 ORDERY BY relevance DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.