jeudi 28 juin 2018

Highlighting search results when using Laravel Scout

In my application, I am using Laravel Scout with the TNTSearch Driver to create a search bar in my site navigation, which is connected to a search method.

The search method searches a bunch of different models and returns what it finds to a view.

Here is the method:

/**
 * 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'));
}

As you can see, I'm using Scout's search() function to search each model and then put some additional constraints on the results, before returning them to my view.

The view itself

enter image description here

I would like the highlighted text at the top to also highlight where appropriate with the search results themselves but I can't seem to find anything in the documentation about using the TNT Highlighter class with Laravel.

Trawling through Laracasts forums, I found this: https://laracasts.com/discuss/channels/laravel/algolia-highlighting-in-laravel-53?page=1

Point of interest

<?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'),
});

In my case would I need this snippet for each result set?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2tE22vi
via IFTTT

Aucun commentaire:

Enregistrer un commentaire