What is the purpose of the `whereVectorSimilarTo` method in Laravel 13?

The whereVectorSimilarTo method in Laravel 13 is designed to perform semantic search by comparing vector embeddings. Its primary purpose is to find records whose meaning is similar to a given search query, rather than matching exact keywords.

Core Functionality

This method works by calculating the cosine similarity between a query embedding and the vectors stored in your database column. It then filters out any results that fall below a specified minSimilarity threshold (a value between 0.0 and 1.0, where 1.0 means identical) and automatically orders the remaining results by relevance, with the most similar records first.

Key Convenience Feature

A major benefit is that you do not need to manually generate the embedding if you are using the Laravel AI SDK. You can pass a plain string (like a user is search query) directly, and Laravel will automatically convert it into an embedding using your configured provider before running the query.

Example Usage

Here is how you would typically use it in a query:

$documents = Document::query()
   ->whereVectorSimilarTo( embedding , best wineries in Napa Valley , minSimilarity: 0.4)
   ->limit(10)
   ->get();

Database Support

While initially built for PostgreSQL with the pgvector extension, Laravel 13 also added native support for MariaDB 11.7+ for these vector queries . Note that this method will throw an exception on a standard MySQL connection, as standard MySQL lacks the necessary native vector distance functions.

If you are interested in the lower-level vector methods like whereVectorDistanceLessThan or how to generate embeddings manually, just let me know.

9 Views