簡體   English   中英

使用Laravel Scout時突出顯示搜索結果

[英]Highlighting search results when using Laravel Scout

在我的應用程序中,我將Laravel ScoutTNTSearch驅動程序配合使用,以在網站導航中創建一個搜索欄,該搜索欄已連接到搜索方法。

搜索方法搜索一堆不同的模型,並將找到的結果返回給視圖。

方法如下:

/**
 * 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>標簽設置樣式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM