Add competitor-discovery search (/discover)
The actual "who sells honey with a .es domain" feature: search sites by keyword — matched against page title/description/keywords/body text via a new MySQL fulltext index on site_pages — combined with an optional TLD filter on the domain. Results are sites ranked by number of matching pages. Shows nothing until a filter is entered, so it doesn't just duplicate the plain Sites browse page. Verified against real .es data pulled in for the test (dominio -> 4 matching sites, correctly ranked and TLD-filtered).
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Livewire\Discover;
|
||||
|
||||
use App\Models\Site;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Livewire\Attributes\Layout;
|
||||
use Livewire\Attributes\Url;
|
||||
use Livewire\Component;
|
||||
use Livewire\WithPagination;
|
||||
|
||||
/**
|
||||
* The competitor-discovery search: "who sells honey with a .es domain?" — keyword
|
||||
* matches a site's pages via MySQL fulltext (natural language mode, so free-text input
|
||||
* never trips a boolean-mode syntax error), TLD filters the site's domain suffix.
|
||||
*/
|
||||
#[Layout('components.layouts.app')]
|
||||
class Search extends Component
|
||||
{
|
||||
use WithPagination;
|
||||
|
||||
#[Url(as: 'q', history: true)]
|
||||
public string $keyword = '';
|
||||
|
||||
#[Url(as: 'tld', history: true)]
|
||||
public string $tld = '';
|
||||
|
||||
public function updatingKeyword(): void
|
||||
{
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
public function updatingTld(): void
|
||||
{
|
||||
$this->resetPage();
|
||||
}
|
||||
|
||||
public function render(): View
|
||||
{
|
||||
$keyword = trim($this->keyword);
|
||||
$tld = strtolower(trim(ltrim($this->tld, '.')));
|
||||
$hasQuery = $keyword !== '' || $tld !== '';
|
||||
|
||||
// Without any filter this would just be the full site list — that's what the
|
||||
// Sites page is for. Discover only returns results once you've asked it something.
|
||||
if (! $hasQuery) {
|
||||
$sites = new LengthAwarePaginator([], 0, 25, 1, ['path' => request()->url()]);
|
||||
} else {
|
||||
$sites = Site::query()
|
||||
->when(
|
||||
$keyword !== '',
|
||||
fn ($query) => $query->whereHas(
|
||||
'pages',
|
||||
fn ($pageQuery) => $pageQuery->whereFullText(['title', 'description', 'keywords', 'content'], $keyword)
|
||||
)
|
||||
)
|
||||
->when($tld !== '', fn ($query) => $query->where('domain', 'like', '%.'.$tld))
|
||||
->withCount(['pages as matching_pages_count' => function ($pageQuery) use ($keyword) {
|
||||
if ($keyword !== '') {
|
||||
$pageQuery->whereFullText(['title', 'description', 'keywords', 'content'], $keyword);
|
||||
}
|
||||
}])
|
||||
->orderByDesc('matching_pages_count')
|
||||
->orderBy('domain')
|
||||
->paginate(25);
|
||||
}
|
||||
|
||||
return view('livewire.discover.search', ['sites' => $sites, 'hasQuery' => $hasQuery]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Backs the product/keyword search used by the discovery page — matching a term
|
||||
* like "honey" against a page's title/description/keywords/body text.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('site_pages', function (Blueprint $table) {
|
||||
$table->fullText(['title', 'description', 'keywords', 'content']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('site_pages', function (Blueprint $table) {
|
||||
$table->dropFullText(['title', 'description', 'keywords', 'content']);
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -11,8 +11,12 @@
|
||||
</head>
|
||||
<body class="min-h-screen bg-white text-neutral-900 antialiased dark:bg-neutral-950 dark:text-neutral-100">
|
||||
<header class="border-b border-neutral-200 dark:border-neutral-800">
|
||||
<div class="mx-auto flex max-w-5xl items-center px-4 py-4">
|
||||
<div class="mx-auto flex max-w-5xl items-center gap-6 px-4 py-4">
|
||||
<a href="{{ route('sites.index') }}" class="text-lg font-semibold">Huginn</a>
|
||||
<nav class="flex gap-4 text-sm">
|
||||
<a href="{{ route('sites.index') }}" class="text-neutral-600 hover:text-neutral-900 dark:text-neutral-400 dark:hover:text-neutral-100">Sites</a>
|
||||
<a href="{{ route('discover') }}" class="text-neutral-600 hover:text-neutral-900 dark:text-neutral-400 dark:hover:text-neutral-100">Discover</a>
|
||||
</nav>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<div class="mx-auto max-w-5xl px-4 py-8">
|
||||
<h1 class="text-2xl font-semibold">Discover competitors</h1>
|
||||
<p class="mt-1 text-sm text-neutral-500">
|
||||
Find sites by what they sell and where — e.g. keyword "honey", TLD "es".
|
||||
</p>
|
||||
|
||||
<div class="mt-6 flex flex-wrap gap-3">
|
||||
<input
|
||||
type="search"
|
||||
wire:model.live.debounce.400ms="keyword"
|
||||
placeholder="Keyword, e.g. honey"
|
||||
class="w-full max-w-sm rounded-md border border-neutral-300 px-3 py-2 text-sm focus:border-neutral-500 focus:outline-none dark:border-neutral-700 dark:bg-neutral-900"
|
||||
>
|
||||
<input
|
||||
type="text"
|
||||
wire:model.live.debounce.400ms="tld"
|
||||
placeholder="TLD, e.g. es"
|
||||
class="w-full max-w-[10rem] rounded-md border border-neutral-300 px-3 py-2 text-sm focus:border-neutral-500 focus:outline-none dark:border-neutral-700 dark:bg-neutral-900"
|
||||
>
|
||||
</div>
|
||||
|
||||
<div class="mt-6 overflow-hidden rounded-lg border border-neutral-200 dark:border-neutral-800">
|
||||
<table class="min-w-full divide-y divide-neutral-200 dark:divide-neutral-800">
|
||||
<thead class="bg-neutral-50 dark:bg-neutral-900">
|
||||
<tr>
|
||||
<th class="px-4 py-2 text-left text-xs font-medium uppercase tracking-wide text-neutral-500">Domain</th>
|
||||
<th class="px-4 py-2 text-left text-xs font-medium uppercase tracking-wide text-neutral-500">Title</th>
|
||||
<th class="px-4 py-2 text-right text-xs font-medium uppercase tracking-wide text-neutral-500">Matching pages</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="divide-y divide-neutral-200 dark:divide-neutral-800">
|
||||
@forelse ($sites as $site)
|
||||
<tr wire:key="site-{{ $site->id }}">
|
||||
<td class="px-4 py-2 text-sm">
|
||||
<a href="{{ route('sites.show', $site) }}" class="font-medium text-blue-600 hover:underline dark:text-blue-400">
|
||||
{{ $site->domain }}
|
||||
</a>
|
||||
</td>
|
||||
<td class="px-4 py-2 text-sm text-neutral-600 dark:text-neutral-400">{{ $site->title ?? '—' }}</td>
|
||||
<td class="px-4 py-2 text-right text-sm text-neutral-600 dark:text-neutral-400">{{ $site->matching_pages_count }}</td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr>
|
||||
<td colspan="3" class="px-4 py-6 text-center text-sm text-neutral-500">
|
||||
@if ($hasQuery)
|
||||
No matching sites.
|
||||
@else
|
||||
Enter a keyword or TLD to search.
|
||||
@endif
|
||||
</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div class="mt-4">
|
||||
{{ $sites->links() }}
|
||||
</div>
|
||||
</div>
|
||||
@@ -1,8 +1,10 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\SiteController;
|
||||
use App\Livewire\Discover\Search as DiscoverSearch;
|
||||
use App\Livewire\Sites\Index as SitesIndex;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::get('/', SitesIndex::class)->name('sites.index');
|
||||
Route::get('/sites/{site}', [SiteController::class, 'show'])->name('sites.show');
|
||||
Route::get('/discover', DiscoverSearch::class)->name('discover');
|
||||
|
||||
Reference in New Issue
Block a user