gpt4 book ai didi

php - 如何使精确关键字搜索首先出现?

转载 作者:行者123 更新时间:2023-11-29 07:08:02 24 4
gpt4 key购买 nike

我制作了一个 mysql/php 搜索引擎,其中添加了带有关键字 google search engine 的 google url 和带有关键字 yahoo search engine 的另一个 yahoo url。如果我在搜索框中输入 google search engine,如何让 google url 首先出现?

这是代码:

//connect to database
mysql_connect("","","");
mysql_select_db("");

//explode out search term
$search_exploded = explode(" ",$search);

foreach($search_exploded as $search_each)
{

//construct query
$x++;
if ($x==1)
$construct .= "Keywords LIKE '%$search_each%'";
else
$construct .= " OR Keywords LIKE '%$search_each%'";

}

//echo outconstruct
$constructx = "SELECT * FROM searchengine WHERE $construct";

$construct = "SELECT * FROM searchengine WHERE $construct LIMIT $s,$e";
$run = mysql_query($constructx);

$foundnum = mysql_num_rows($run);


$run_two = mysql_query("$construct");

if ($foundnum==0)
echo "No results found for <b>$search</b>";
else

最佳答案

这里最好的办法不是使用LIKE,而是使用Mysql 的FULLTEXT 索引。为此,您在要搜索的列上创建全文索引。

ALTER TABLE searchengine ADD FULLTEXT(keywords);

要查询,做这样的事情:

SELECT * FROM searchengine WHERE MATCH (keywords) AGAINST ("$searchterm");

您可以组合不同的MATCH AGAINST 查询来满足所有关键字。此外,您可以将 MATCH AGAINST 位放在查询的 SELECT 子句中。这将返回一个分数,然后您可以使用该分数对您的结果进行排名。

此外,您还可以在运行查询之前进行一些词干提取工作。使用各种 MATCH 运算符生成的分数,您可以将整个词的排名高于词干词。

关于php - 如何使精确关键字搜索首先出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6164375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com