使用Laravel Scout时高亮显示搜索结果

yhxst69z  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(184)

在我的应用程序中,我使用Laravel ScoutTNTSearch Driver在我的站点导航中创建一个搜索栏,它连接到一个搜索方法。
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荧光笔类与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;
    });
83qze16e

83qze16e1#

我不熟悉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>标记的样式

pdtvr36n

pdtvr36n2#

好溶液:

public function getTitle($q = null)
{
    if (!$q) {
        return $this->__get('title');
    } else {
        return preg_replace("/($q)/i", '<em>$1</em>', $this->__get('title'));
    }
}

相关问题