Skip to main content

Laravel Scout Full-Text Search

Implement full-text search with Laravel Scout using Meilisearch or Algolia, with faceted filtering, custom ranking, and typo-tolerant search.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Implement a comprehensive search system using Laravel Scout for a technical blog and documentation platform with 50,000 searchable records across 4 models. Use Meilisearch as the search driver. Build it in these steps: 1) Install and configure Scout with Meilisearch: set up the connection in scout.php, configure the queued via Horizon for production, sync for development indexing (sync vs. queued), and define global search settings — typo tolerance (enabled with max 2 typos), minimum word length for typos, stop words for English, and ranking rules priority (words, typo, proximity, attribute, sort, exactness, published_at:desc). 2) Make 4 models searchable: for each model (Post, Page, Documentation, FAQ), define toSearchableArray() with title (weight 3x), excerpt (2x), body (1x), tags, category name, author name — include only searchable fields, flatten relationships (e.g., category.name, author.name), add computed fields for search boosting, and configure searchableAs() for custom index names. Implement shouldBeSearchable() to exclude draft posts, archived pages, and private documentation. 3) Configure per-index settings using the Scout config or a setup command: define searchable attributes with priority ordering, filterable attributes for facets (category, tags, author, published_year, content_type), sortable attributes (published_at, views_count, relevance), and custom synonyms for JS/JavaScript, DB/database, auth/authentication, API/endpoint. 4) Build the search API endpoint: create a SearchController with a search() method that accepts: query string, filters (faceted), sort order, page/per_page, and search-specific options. Implement multi-index search, facet counts, snippets, and suggestions: multi-model search across all indices, facet count retrieval, highlighted result snippets, and "did you mean" suggestions for zero-result queries. 5) Create the frontend search experience: build a Livewire component with Alpine.js for interactivity search component with debounced input (300ms), instant results dropdown, faceted filter sidebar, pagination, and search analytics tracking (queries, clicks, conversions). 6) Optimize for performance: implement index warming on deployment, configure searchable data transformers that reduce index size by 40% by excluding body HTML and large text fields from stored attributes, set up Scout model observers that handle batch updates efficiently, and monitor search latency with a target of under 50ms. 7) Handle edge cases: index recovery after data migration (scout:import with chunk size 500), soft-deleted record handling, polymorphic search across multiple models, and zero-downtime reindexing using index swapping with a temporary index.

What this prompt does

This prompt makes the AI implement a full-text search system with Laravel Scout, spanning [record_count] records across [model_count] models using [search_engine]. It configures Scout, sets [queue_setting] indexing, and tunes global settings like [typo_tolerance], stop words for [language], and [ranking_rules]. Stating the record count and engine lets the AI size the index and plan reindexing rather than producing a setup that works on a handful of rows but stalls at scale.

The structure works because good search is the sum of these decisions. Each model in [searchable_models] gets a toSearchableArray() using [field_strategy] with flattened relationships and shouldBeSearchable() to skip [exclude_criteria]; per-index settings define [facet_fields], [sort_fields], and [synonym_groups]; the API exposes [search_features] like facet counts and suggestions; and a [frontend_component] debounced at [debounce_ms] drives the UX. The optimization step targets [latency_target] and zero-downtime reindexing via [reindex_strategy]. Declaring [record_count] and [queue_setting] lets the AI decide between sync and queued indexing and size scout:import chunks with [chunk_size] so a full reindex does not exhaust memory. The SearchController exposes the [search_features] you ask for — multi-index search, facet counts, highlighted snippets, and did-you-mean suggestions for zero-result queries — and the [debounce_ms] setting on the [frontend_component] keeps the instant-results dropdown from firing a request on every keystroke. Edge cases like soft-deleted records and polymorphic search are handled explicitly rather than left to break later.

When to use it

  • You need fast, typo-tolerant search across multiple models in a Laravel app
  • You want faceted filtering and facet counts in a search sidebar
  • You need queued indexing in production with sync indexing locally
  • You want did-you-mean suggestions for zero-result queries
  • You need zero-downtime reindexing after a data migration
  • You want to keep search latency low by trimming the index payload deliberately

Example output

The AI returns Scout and [search_engine] configuration, toSearchableArray() and shouldBeSearchable() implementations for each model in [searchable_models], per-index settings for searchable attributes, [facet_fields], [sort_fields], and [synonym_groups], a SearchController with multi-index search, facet counts, highlighted snippets, and suggestions, a [frontend_component] with debounced input and a faceted sidebar, plus optimization steps including index warming, scout:import with chunk size [chunk_size], and [reindex_strategy] for zero-downtime reindexing.

Pro tips

  • Order [field_strategy] weights so titles outrank body text; relevance is mostly attribute priority
  • Trim stored attributes toward [size_reduction_target] — excluding body HTML is what keeps [latency_target] reachable
  • Define [synonym_groups] for your domain (JS/JavaScript, auth/authentication) so searchers find results regardless of phrasing
  • Use shouldBeSearchable() with [exclude_criteria] so drafts and archived records never leak into results
  • Tune [chunk_size] for scout:import to balance reindex speed against memory on your box
  • Always plan [reindex_strategy] (index swapping) so a full reindex never serves empty results to live users

Frequently Asked Questions

Does this work with both Meilisearch and Algolia?
Yes. You set the driver in `[search_engine]`; the default is Meilisearch, but the same Scout-based structure applies to Algolia. The per-index settings for ranking, facets, and synonyms map onto whichever engine you choose.
How does it keep drafts and private records out of search?
Each model implements `shouldBeSearchable()` to exclude records matching `[exclude_criteria]`, such as draft posts, archived pages, and private documentation. That keeps unpublished content out of the index even though the model is otherwise searchable.
Can I reindex without taking search offline?
Yes. The optimization step uses `[reindex_strategy]`, defaulting to index swapping with a temporary index, so a full `scout:import` builds in the background and atomically replaces the live index. Users keep getting results throughout the reindex.
How does it keep search latency low?
It trims stored attributes toward `[size_reduction_target]` by excluding large fields like body HTML, warms the index on deploy, and targets `[latency_target]`. Smaller stored payloads and ordered ranking rules are the main levers for fast responses.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Laravel & PHP Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support