我有一个具有搜索功能的网站,但正在尝试改进对模糊搜索的搜索。
到目前为止,我要做的是查询我的所有产品并使用s imilar_text()
来查看它们与实际搜索字词的距离
然后,我创建一个数组,其中键的相似性和值是产品ID。然后,我按键/相似性对其进行排序。
$term = $_GET['search_term'];
$similar_array = [];
$sql = "SELECT * FROM products";
$stmt = DB::run($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$id = $row["pro_id"];
$result = $row['product'];
similar_text($term,$result,$similarity);
$similar_array[$similarity][] = $id;
}
$closest_match = array_keys($similar_array);
rsort($closest_match);
$match_count = count($closest_match);
for($i=0; $i<$match_count; $i++){
var_dump($similar_array[$closest_match[$i]]);
}
这给了我类似的东西
array (size=9)
0 => int 28
1 => int 1628
2 => int 1665
3 => int 1666
4 => int 1667
5 => int 1668
6 => int 1669
7 => int 1670
8 => int 1671
C:\wamp64\www\V2\search.php:65:
array (size=2)
0 => int 37
1 => int 38
C:\wamp64\www\V2\search.php:65:
array (size=1)
0 => int 481
C:\wamp64\www\V2\search.php:65:
array (size=3)
0 => int 27
1 => int 1009
2 => int 1620
C:\wamp64\www\V2\search.php:65:
array (size=14)
0 => int 30
1 => int 104
2 => int 131
3 => int 134
4 => int 168
5 => int 169
6 => int 170
7 => int 557
8 => int 1011
9 => int 1014
10 => int 1661
11 => int 1662
12 => int 1663
13 => int 1664
我有一个show_products()
函数,我只需要传递一个ID数组,所以我现在想做的是从上方获取ID的多个数组,并按此特定顺序合并,然后裁剪排列到一定数目,这样就不会有所有产品,但是会在经过如此多的结果后中断。
任何帮助或建议,将不胜感激。
答案 0 :(得分:0)
根据您的查询,您可以按照以下步骤操作
outer_module.py
合并所有数组array_merge
(最高合并优先)对数组进行排序; rsort
或array_slice