How do you use the semantic() search method with Typesense in Laravel 13?

In Laravel 13, using the semantic() method for Typesense semantic search requires first completing the embedding configuration for the model, then calling it directly on the Scout search query.

Core Usage.

The basic syntax is as follows, calling semantic() after the search()method:

$articles = Article::search( istaying cool in the summer )
   ->semantic()
   ->get();

If the engine supports it, you can also specify a minimum similarity threshold:

$articles = Article::search( renewable energy storage )
   ->semantic(minSimilarity: 0.6)
   ->get();

To combine full-text search with semantic search, use the hybrid()method, where the first two parameters control the weights of text and semantics respectively:

$articles = Article::search( renewable energy storage )
   ->hybrid(textWeight: 1, semanticWeight: 2)
   ->get();

Prerequisites: Vector Generation Methods.

Typesense is semantic search supports three vector generation approaches:

  1. Generating vectors via Laravel AI: Implementing the toSearchableEmbedding() method on the model to generate embeddings requires the Laravel AI SDK.
  2. Using Typesense is native auto-embedding: Utilizing Typesense is built-in embedding functionality, no additional SDK required.
  3. Using precomputed vectors: Directly providing precomputed vector values.

Important Notes.

  • This feature was introduced in Laravel 13, and Scout is Typesense engine now natively supports semantic() and hybrid() searches.
  • If you generate embeddings using Laravel AI, you need to configure the AI SDK first; if you use Typesense is native embedding or precomputed vectors, you do not need the Laravel AI SDK.
  • Semantic search matches based on "meaning" rather than keywords, making it suitable for scenarios requiring understanding of query intent.
8 Views