Introduction
Almost every application needs search. Whether your users are searching a knowledge base for relevant articles, exploring a product catalog, or asking natural-language questions against a corpus of documents, Laravel provides built-in tools to handle each of these scenarios — and you often don’t need any external services to get there.Feature comparison
| Feature | External service | Description |
|---|---|---|
whereFullText | None required | Native full-text indexes on MariaDB / MySQL / PostgreSQL |
whereVectorSimilarTo | None (PostgreSQL + pgvector) | Similarity search by meaning. Requires the AI SDK |
Reranking | AI provider | Re-order any result set by semantic relevance |
| Laravel Scout | None (database engine) / optional | Automatic index sync for Eloquent models |
Full-Text Search
WhileLIKE queries work well for simple substring matching, they don’t understand language. Full-text search uses specialised indexes that understand word boundaries, stemming, and relevance scoring, so the database can return the most relevant results first.
Fast full-text search is built into MariaDB, MySQL, and PostgreSQL — no external search service is required.
Adding full-text indexes
To use full-text search, first add a full-text index to the columns you want to search. You may pass an array of columns to create a composite index that searches across multiple fields at once:Running full-text queries
Once the index is in place, use thewhereFullText query builder method to search against it. Laravel generates the appropriate SQL for your database driver — MATCH(...) AGAINST(...) on MariaDB and MySQL, and to_tsvector(...) @@ plainto_tsquery(...) on PostgreSQL:
whereFullText:
On MariaDB and MySQL, results are automatically ordered by relevance score. On PostgreSQL,
whereFullText filters matching records but does not order them by relevance. If you need automatic relevance ordering on PostgreSQL, consider using Scout’s database engine, which handles this for you.orWhereFullText method adds a full-text search clause as an “or” condition. For complete details, consult the query builder documentation.
Semantic / Vector Search
Full-text search relies on keyword matching — the words in the query must appear in the data. Semantic search takes a fundamentally different approach: it uses AI-generated vector embeddings to represent the meaning of text as arrays of numbers, and then finds results whose meaning is closest to the query. For example, a search for “best wineries in Napa Valley” can surface an article titled “Top Vineyards to Visit” — even though the words don’t overlap at all.Vector search requires the Laravel AI SDK and is supported by PostgreSQL (requires the
pgvector extension) and MongoDB (requires the Laravel MongoDB package). All Postgres databases on Laravel Cloud already have pgvector installed.Generating embeddings
An embedding is a high-dimensional numeric array that represents the semantic meaning of a piece of text. You may generate embeddings using thetoEmbeddings method on Laravel’s Stringable class:
Embeddings class:
Storing and indexing vectors
Define avector column in your migration, specifying the number of dimensions that matches your embedding provider’s output (for example, 1536 for OpenAI’s text-embedding-3-small). Call index() to create an HNSW index, which dramatically speeds up similarity searches on large datasets:
array so that Laravel automatically handles conversion between PHP arrays and the database’s vector format:
Querying by similarity
Once you have stored embeddings, usewhereVectorSimilarTo to find similar records. This method compares the given embedding against stored vectors using cosine similarity, filters out results below the minSimilarity threshold, and orders results by relevance. The threshold is a value between 0.0 and 1.0, where 1.0 means the vectors are identical:
whereVectorDistanceLessThan, selectVectorDistance, and orderByVectorDistance methods are also available. For complete details, consult the query builder documentation and the AI SDK documentation.
Reranking
Reranking is a technique where an AI model reorders a set of results by how semantically relevant each result is to a given query. Unlike vector search, which requires pre-computed stored embeddings, reranking works on any collection of text — it takes raw content and a query as input and returns the items sorted by relevance. Reranking is especially powerful as a second stage after a fast initial retrieval step. Use full-text search to quickly narrow thousands of records down to the top 50 candidates, then use reranking to put the most relevant results at the top. This “retrieve then rerank” pattern gives you both speed and semantic accuracy.rerank macro that accepts a field name (or closure) and a query, making it easy to rerank Eloquent results:
Laravel Scout
The search techniques described above are all query builder methods that you call directly in your code. Laravel Scout takes a different approach: it provides aSearchable trait that you add to your Eloquent models, and Scout automatically keeps your search indexes in sync as records are created, updated, and deleted.
Database engine
Scout’s built-in database engine performs full-text andLIKE searches against your existing database — no external service or extra infrastructure required. Add the Searchable trait to your model and define a toSearchableArray method that returns the columns you want to be searchable.
You may use PHP attributes to control the search strategy for each column:
| Attribute | Search strategy |
|---|---|
SearchUsingFullText | Uses the database’s full-text index (MATCH...AGAINST / to_tsvector) |
SearchUsingPrefix | Prefix match (example%) |
| None | Wildcard on both sides (%example%) |
search method. Scout’s database engine automatically orders results by relevance, even on PostgreSQL:
Third-party engines
Scout also supports third-party search engines such as Algolia, Meilisearch, and Typesense. These dedicated search services offer advanced features like typo tolerance, faceted filtering, geo-search, and custom ranking rules. Since Scout provides a unified API across all of its drivers, switching from the database engine to a third-party engine requires minimal code changes.Many applications never need an external search engine. The built-in techniques described on this page cover the vast majority of use cases.
Combining techniques
The search techniques described on this page are not mutually exclusive — combining them often produces the best results.Full-text retrieval + reranking
Use full-text search to quickly narrow a large dataset down to a candidate set, then apply reranking to sort those candidates by semantic relevance. This gives you the speed of database-native full-text search with the accuracy of AI-powered relevance scoring:Vector search + traditional filters
Combine vector similarity with standardwhere clauses to scope semantic search to a subset of records. This is useful when you need meaning-based search but want to restrict results by ownership, category, or any other attribute:
Related pages
Laravel Scout
Complete guide to automatic index syncing with the Searchable trait.
Laravel AI SDK
Configure vector embeddings and reranking with the AI SDK.
Query Builder
Detailed reference for whereFullText, whereVectorSimilarTo, and more.
Migrations
How to create full-text indexes and vector columns.