php - 使用 Laravel Scout 时突出显示搜索结果
问题描述
在我的应用程序中,我使用Laravel Scout和TNTSearch 驱动程序在我的站点导航中创建一个搜索栏,该搜索栏连接到搜索方法。
search 方法搜索一堆不同的模型并将找到的内容返回给视图。
这是方法:
/**
* Perform a search given what the user entered into the search box.
* Uses Laravel Scout to do initial search but because the use of WHERE is limited,
* we use a filter function instead, on each collection.
*
* @param Request $request
* @return void
*/
public function search(Request $request)
{
// The search string entered
$search = $request->get('q');
// Laravel Scout search() method
$users = User::search($search)->get();
$articles = Article::search($search)->get();
$events = Event::search($search)->get();
$files = FileMetaData::search($search)->get();
// The date and time as of right now
$today = Carbon::now();
/**
* Below are the filters in place for each model search
* 1. News articles must be open
* 2. \Events are split into open and closed
*/
$articles = $articles->filter(function ($articles) {
return $articles->published === 'published';
});
$upcomingEvents = $events->filter(function ($events) use ($today) {
return $events->startDate->gt($today);
});
$pastEvents = $events->filter(function ($events) use ($today) {
return $events->startDate->lt($today);
});
$userCount = count($users);
$articleCount = count($articles);
$eventCount = count($events);
$upcomingEventCount = count($upcomingEvents);
$pastEventCount = count($pastEvents);
$fileCount = count($files);
return view('pages.search.index', compact('search', 'users', 'articles', 'upcomingEvents', 'pastEvents', 'userCount', 'articleCount', 'upcomingEventCount', 'pastEventCount', 'files', 'fileCount'));
}
如您所见,我正在使用 Scout 的search()
功能来搜索每个模型,然后对结果添加一些额外的约束,然后再将它们返回到我的视图中。
视图本身
我希望顶部突出显示的文本也可以在适当的位置突出显示搜索结果本身,但我似乎在文档中找不到任何关于将 TNT Highlighter 类与 Laravel 一起使用的内容。
通过 Laracasts 论坛拖网,我发现了这个:https ://laracasts.com/discuss/channels/laravel/algolia-highlighting-in-laravel-53?page=1
兴趣点
<?php
use TeamTNT\TNTSearch\TNTSearch;
$articles = Article::search($searchString)->get();
$tnt = new TNTSearch;
$articles = $articles->map(function($article) use ($searchString, $tnt) {
$article->title = $tnt->highlight($title, $searchString, 'em'),
});
就我而言,每个结果集都需要这个片段吗?
更新:
$articles = Article::search($search)->get();
/**
* Declaire where highlighting should occur for each collection
*/
// Articles
$articles = $articles->map(function($article) use ($search, $tnt){
$article->title = $tnt->highlight($article->title, $search, b, [
'simple' => false,
'wholeWord' => false,
'tagOptions' => [
'class' => 'search-term',
'title' => 'test'
]
]);
return $article;
});
解决方案
我不熟悉 TNT 荧光笔,但如果你想尝试自己的方法,你可以使用这样的东西:
/**
* @$str = The string to highlight
* @$search_term = The term we are looking for in $str
**/
function highlightString($str, $search_term) {
if (empty($search_term))
return $str;
$pos = strpos(strtolower($str), strtolower($search_term));
if ($pos !== false) {
$replaced = substr($str, 0, $pos);
$replaced .= '<em>' . substr($str, $pos, strlen($search_term)) . '</em>';
$replaced .= substr($str, $pos + strlen($search_term));
} else {
$replaced = $str;
}
return $replaced;
}
只是不要忘记设置<em>
标签的样式
推荐阅读
- python - Python Beautiful Soup IndexError:列表索引超出范围
- haskell - GHC 奇怪的生成成本中心
- python - 在循环的每次迭代中创建一个 CSV 文件
- java - 从文件中的数组中查找整数的出现:
- python - 在我不断开发的过程中,如何只使用 Github 来保存我的代码版本?
- jquery - 根据不同下拉列表的选择填充下拉列表
- r - R:表描述性统计:3 个变量
- laravel - Laravel - 如何通过关系对字段执行 where 查询?
- javascript - 使用 Chart.js 从世界银行 javascript JSON 下载中获取变更统计信息
- python - 一个更聪明的函数来确定 C 是否是 S 的精确覆盖?