Feat: Adding Search prefixes to homepage search

This commit is contained in:
arvanitakis committed 2026-10-19 17:32:35 +03:00
1 parent 623851bd70
commit 4f56edc4fc
6 files changed
+222 -5

No files matched your search

+41 -1
View File
@@ -2,17 +2,34 @@
namespace App\Http\Controllers;
use App\Catalog\ProductCard;
use App\Catalog\ProductListing;
use App\Catalog\ProductListingPage;
use Modules\Core\Catalog\Services\ProductSearchService;
class SearchController extends Controller
{
public function __construct(private readonly ProductListingPage $listingPage) {}
/** Live search answers from this many characters on. */
public const LIVE_MIN_CHARS = 3;
/** How many products the live search dropdown lists. */
private const LIVE_LIMIT = 6;
public function __construct(
private readonly ProductListingPage $listingPage,
private readonly ProductSearchService $search,
) {}
public function show(string $locale)
{
$query = trim((string) request()->query('q', ''));
// The search overlay's live results: the same endpoint, asked for by
// its <turbo-frame id="search-suggestions"> as the shopper types.
if (request()->header('Turbo-Frame') === 'search-suggestions') {
return $this->suggestions($query);
}
if ($query === '') {
return redirect()->route('products');
}
@@ -28,4 +45,27 @@ public function show(string $locale)
return view('search.index', [...$data, 'query' => $query]);
}
/**
* The first few matches and how many there are in all — the dropdown
* under the search overlay. Empty below LIVE_MIN_CHARS characters.
*/
private function suggestions(string $query)
{
$products = collect();
$total = 0;
if (mb_strlen($query) >= self::LIVE_MIN_CHARS) {
$result = $this->search->search($query, perPage: self::LIVE_LIMIT);
$products = collect($result->products->items())->map(ProductCard::fromIndexed(...));
$total = $result->products->total();
}
return view('search.suggestions', [
'query' => $query,
'products' => $products,
'total' => $total,
'searched' => mb_strlen($query) >= self::LIVE_MIN_CHARS,
]);
}
}
+74
View File
@@ -0,0 +1,74 @@
<?php
namespace App\Support;
use Illuminate\Support\HtmlString;
use Normalizer;
/**
* Underlines the words of a product name that a search query matched, for
* the live search dropdown: every word starting with one of the query's
* words ("click" → "Edamame _Clicker_"), ignoring case and accents the way
* Meilisearch does ("παπ" → "_Παπάκι_"). Typo matches aren't marked — the
* name is shown as is.
*/
final class SearchHighlight
{
public static function name(string $name, string $query, string $class = 'underline underline-offset-4'): HtmlString
{
$terms = array_values(array_filter(preg_split('/[^\p{L}\p{N}]+/u', self::fold($query)) ?: []));
$chars = mb_str_split($name);
if ($terms === [] || $chars === []) {
return new HtmlString(e($name));
}
// One folded character per original one, so positions line up.
$folded = array_map(self::fold(...), $chars);
$html = '';
$i = 0;
$count = count($chars);
while ($i < $count) {
if (! self::isWordChar($chars[$i])) {
$html .= e($chars[$i++]);
continue;
}
$end = $i;
while ($end < $count && self::isWordChar($chars[$end])) {
$end++;
}
$word = implode('', array_slice($chars, $i, $end - $i));
$foldedWord = implode('', array_slice($folded, $i, $end - $i));
$matched = collect($terms)->contains(fn (string $term) => str_starts_with($foldedWord, $term));
$html .= $matched ? '<span class="'.e($class).'">'.e($word).'</span>' : e($word);
$i = $end;
}
return new HtmlString($html);
}
/**
* Lower case, without accents; Greek final sigma as σ.
*/
private static function fold(string $text): string
{
if (class_exists(Normalizer::class)) {
$text = preg_replace('/\p{Mn}+/u', '', Normalizer::normalize($text, Normalizer::FORM_D) ?: $text);
}
return strtr(mb_strtolower($text), [
'ά' => 'α', 'έ' => 'ε', 'ή' => 'η', 'ί' => 'ι', 'ό' => 'ο', 'ύ' => 'υ', 'ώ' => 'ω',
'ϊ' => 'ι', 'ϋ' => 'υ', 'ΐ' => 'ι', 'ΰ' => 'υ', 'ς' => 'σ',
]);
}
private static function isWordChar(string $char): bool
{
return (bool) preg_match('/[\p{L}\p{N}]/u', $char);
}
}