
本文旨在解决 laravel 8 中使用 eloquent orm 进行多词模糊搜索时遇到的挑战,特别是当搜索词包含多个单词(如姓名和姓氏)时无法正确匹配的问题。通过介绍一种将搜索短语分词并结合 `orwhere` 语句进行动态查询的策略,实现更灵活、准确的数据库搜索功能。
在 Laravel 中,当我们需要在多个数据库字段中执行模糊搜索时,通常会使用 where 或 orWhere 结合 LIKE 操作符。例如,以下代码片段展示了在多个字段中搜索单个关键词的常见做法:
$query = Immovable::query()
->leftJoin('streets', 'streets.gus_id', '=', 'immovables.street_gus_id')
->select(
'immovables.id',
'immovables.street_gus_id',
'immovables.building_number',
'immovables.apartment_number',
'streets.name as street_name'
);
if (request()->has('search')) {
$searchTerm = "%" . request()->search['value'] . "%";
$query->where('streets.name', 'like', $searchTerm)
->orWhere('immovables.community', 'like', $searchTerm)
// ... 其他 orWhere 条件
->orWhere('immovables.name', 'like', $searchTerm)
->orWhere('immovables.surname', 'like', $searchTerm);
}
$results = $query->get();这种方法对于单个单词的搜索(例如,搜索 "Karol" 或 "Krawczyk")是有效的。然而,当用户输入包含多个单词的搜索短语(例如 "Karol Krawczyk")时,上述代码会将其作为一个整体进行匹配。这意味着数据库将尝试查找一个字段中同时包含 "Karol Krawczyk" 整个字符串的记录。如果 "Karol" 在 immovables.name 字段,而 "Krawczyk" 在 immovables.surname 字段,或者它们分散在不同的字段中,那么这种整体匹配的方式将无法返回任何结果,从而导致搜索失败。
要解决这个问题,我们需要将用户输入的搜索短语拆分成独立的单词,并对每个单词在所有目标字段中执行独立的模糊匹配。
为了实现更灵活的多词搜索,我们可以采取以下策略:
以下是优化后的 Laravel 查询代码,它演示了如何实现上述策略:
use Illuminate\Http\Request;
use App\Models\Immovable; // 假设您的模型名为 Immovable
class SearchController extends Controller
{
public function search(Request $request)
{
$query = Immovable::query()
->leftJoin('streets', 'streets.gus_id', '=', 'immovables.street_gus_id')
->select(
'immovables.id',
'immovables.street_gus_id',
'immovables.building_number',
'immovables.apartment_number',
'streets.name as street_name'
);
if ($request->has('search') && !empty($request->search['value'])) {
// 将搜索值按空格拆分成多个单词
$searches = explode(" ", $request->search['value']);
// 使用一个闭包来分组所有的 OR 条件,确保逻辑清晰
$query->where(function ($q) use ($searches) {
foreach ($searches as $search) {
$searchTerm = "%" . $search . "%"; // 为每个单词构建模糊匹配字符串
// 对每个单词在所有相关字段中执行 OR 匹配
$q->orWhere('streets.name', 'like', $searchTerm);
$q->orWhere('immovables.community', 'like', $searchTerm);
$q->orWhere('immovables.city', 'like', $searchTerm);
$q->orWhere('immovables.building_number', 'like', $searchTerm);
$q->orWhere('immovables.granted_comments', 'like', $searchTerm);
$q->orWhere('immovables.inspections', 'like', $searchTerm);
$q->orWhere('immovables.oze_installations', 'like', $searchTerm);
$q->orWhere('immovables.pesel', 'like', $searchTerm);
$q->orWhere('immovables.name', 'like', $searchTerm);
$q->orWhere('immovables.surname', 'like', $searchTerm);
$q->orWhere('immovables.email1', 'like', $searchTerm);
$q->orWhere('immovables.email2', 'like', $searchTerm);
$q->orWhere('immovables.email3', 'like', $searchTerm);
$q->orWhere('immovables.phone1', 'like', $searchTerm);
$q->orWhere('immovables.phone2', 'like', $searchTerm);
$q->orWhere('immovables.phone3', 'like', $searchTerm);
$q->orWhere('immovables.description', 'like', $searchTerm);
}
});
}
$results = $query->get();
return view('your.view', compact('results')); // 假设您将结果传递给视图
}
}通过将用户输入的搜索短语进行分词,并结合 Laravel Eloquent 的 orWhere 闭包和循环结构,我们可以有效地实现多词模糊搜索功能。这种方法解决了单一 LIKE 匹配无法处理多单词搜索的局限性,使得搜索结果更加灵活和符合用户预期。然而,对于大规模应用,仍需权衡性能,并考虑引入更专业的全文搜索解决方案。
以上就是Laravel 8 数据库多词模糊搜索优化指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号