【发布时间】:2020-12-29 23:31:29
【问题描述】:
到目前为止,我一直在尝试制作一个搜索引擎,但我缺少的功能是,当用户查询一个长句子或两个或三个单词时,我的数据库中的 on not in line-wise 不是将要显示,它显示 0 个结果。
到目前为止,它在单字搜索方面运行良好。
public function getResultsHtml($page, $pageSize, $term){
$fromLimit = ($page - 1) * $pageSize;
//page 1: (1-1) * 20
//page 2:(2-1) * 20
$query = $this->con->prepare("SELECT *
FROM sites WHERE title LIKE :term
OR url LIKE :term
OR keywords LIKE :term
OR description LIKE :term
ORDER BY clicks DESC
LIMIT :fromLimit,:pageSize");
$searchTerm = "%" . $term ."%";
$query->bindParam(":term",$searchTerm);
$query->bindParam(":fromLimit",$fromLimit,PDO::PARAM_INT);
$query->bindParam(":pageSize",$pageSize,PDO::PARAM_INT);
$query->execute();
$resultsHtml ="<div class='siteResults'>";
while($row = $query->fetch(PDO::FETCH_ASSOC)){
$id = $row["id"];
$url = $row["url"];
$title = $row["title"];
$description = $row["description"];
$title = $this->trimField($title,55);
$description = $this->trimField($description,230);
$resultsHtml .="<div class='resultContainer'>
<h3 class='title'>
<a class='result' href='$url' data-linkId='$id'>
$title
</a>
</h3>
<span class='url'>$url</span>
<span class='description'>$description</span>
</div>";
}
$resultsHtml .= "</div>";
return $resultsHtml;
}
因此,当用户搜索苹果时,它正在检索数据,我们可以在第一张图片的“苹果商店搜索”中看到搜索结果。但是在第二张图片中,当我们搜索“苹果搜索”时,它应该能够向我们展示“苹果商店搜索”。
【问题讨论】:
-
向我们展示一些用户输入的示例以及应该匹配什么样的数据
-
您可以对条件进行分组:
WHERE (col1 LIKE :word1 OR col2 LIKE :word1) AND (col1 LIKE :word2 OR col2 LIKE :word2)。您拥有的作品越多,您可以添加更多AND组。这将要求所有单词都存在,但可以在不同的列中。不过,您需要先在空间上爆炸单词,以便可以将它们作为单独的值传递。 -
要么将用户的输入划分为单独的单词/标记并单独搜索它们,要么使用 FTS。
-
感谢您提供清晰的解决方案,我已添加图像