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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user