使用Laravel Scout时突出显示搜索结果

时间:2018-06-28 12:40:55

标签: php laravel search laravel-5

在我的应用程序中,我将Laravel ScoutTNTSearch Driver结合使用,以在网站导航中创建一个搜索栏,该搜索栏与搜索方法相关。

搜索方法搜索一堆不同的模型并将返回的结果返回给视图。

以下是方法:

/**
 * 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()函数搜索每个模型,然后在结果上加上一些其他约束,然后再将它们返回到我的视图。

视图本身

enter image description here

我希望顶部突出显示的文本也可以突出显示搜索结果本身,但我似乎在文档中找不到有关将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;
    });

1 个答案:

答案 0 :(得分:1)

我对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>标签的样式