Supercharged Search with Typesense
Typesense is a wildly fast, open-source search engine that doesn't require a PhD to configure and query. Its API just makes sense. Even better, it integrates seamlessly with Laravel, thanks to the official Typesense PHP client and Laravel Scout.
I spent months researching and preparing this course so that you can update your app with powerful Typesense-backed search in record time. Let's go!
Progress
Series Info
- Episodes
- 22
- Run Time
- 3h 55m
- Difficulty
- Intermediate
- Last Updated
- Jun 11, 2025
- Version
- v28
Series Episodes
- Getting Started (3)
Hello, Typesense
TypeSense makes advanced search feel approachable and actually enjoyable, cutting through the usual confusion and frustration. You’ll dive into everything from basicqueriesandfiltersto ranking, semantic search, vector queries, and even the TypeSense Cloud—building up your skills step-by-step without feeling overwhelmed.How To Install Typesense
You install theTypeSenseserver locally using Homebrew, start the service, and verify it’s running with a simplecurlrequest to the health endpoint on port8108. Then you spin up a fresh Laravel app, set environment variables forTypeSensehost, port, and API key, and pull in the officialTypeSensePHP client via Composer to prep for integrating search functionality.Collections And Documents Explained
We grab a big books dataset from the TypeSense docs and set up aTypeSenseClientin Laravel, configuring it viaconfig/services.phpto keep environment details tidy. Then we define abookscollection schema with fields liketitle,author, andaverage_rating, create that collection, and import all the book documents so we’re ready to start searching.
- Typesense Essentials (6)
Queries, Filters, And Sorting
We organize our routes into self-contained snippets for creating, importing, and searching collections, treating them like a playground rather than production endpoints. Then we dive into searching acollectionby specifying search fields, debugging import issues, and refining queries to search titles and authors. Finally, we explore sorting results by different criteria likeratings_countorpublication_year, handling tie-breakers, and limiting the number of results per page for flexible, real-world search behavior.Filters
We add search result highlighting by pulling the matched snippets from thehighlightsection of the response, flattening the arrays just one level, and plucking out the snippet text. Then we render the snippets as raw HTML so the matched terms get wrapped in<mark>tags, which we style with a yellow background for clear emphasis. Plus, thanks to TypeSense’s fuzzy matching, even misspelled queries still highlight relevant results nicely.Facets
We start by building a simple search UI with a Blade view that submits a form and displays results from a TypeSense search, reading the query from the URL. The lesson walks through fetching only the book titles from the search hits, passing them to the view, and rendering them in a styled list with Tailwind via CDN—plus adding a search input that keeps its value from the query string for a smooth user experience.Build A Search UI
We dig into adding faceted filters to the sidebar by explicitly requestingfacetByfields from our TypeSense search results, focusing on authors here. Then we map over the returned facet counts to extract filter data and pass it to the view, where we loop through and display these filters alongside the search results using a CSS grid layout. Next up, we’ll enhance this by adding checkboxes to filter results dynamically based on selected authors.Search Highlights
You build clickable author filters by turning each facet item into an anchor tag that merges the current query string with the selected author, ensuring the URL updates properly without losing existing search terms. On the backend, you detect the author filter in the request and adjust the TypeSense search parameters to narrow results accordingly. While this approach is simple and effective, it only supports single-author filtering, so next up is exploring a form-based method to enable multi-select filters with checkboxes.Facet Styling
We replace simple anchor-tag filters with a form containing checkboxes to allow selecting multiple authors for filtering search results. By dynamically generating checkbox IDs and names, submitting the form on change, and syncing the checked state using Laravel’s@checkeddirective, we keep the UI and query string in sync. Finally, we clean up the data structure to handle multiple authors properly and prepare for more flexible filter logic.
- Instant and Interactive Search (5)
Apply A Facet Using An Anchor Tag
We set up a Vue 3 and Inertia.js project with Vite to build a live search interface that tracks user input and fetches results instantly. By binding aqueryref to a search input and watching it, we trigger AJAX calls on input changes, then optimize performance with a simple debounce function to limit requests. This approach gives instant feedback without needing heavy dedicated search libraries—though those options come next.Apply Facets Using Form Checkboxes
We set up a search override that boosts a specific book to the top of results when someone searches exactly fortwilight, simulating a sponsorship scenario where a publisher pays to promote their title. The override targets the book by its string ID and uses an upsert method to insert or update the override in the collection. We also cover how to remove the override later, showing how flexible this approach can be for managing search result priorities.Instant Search From Scratch
We set upVue InstantSearchwith the TypeSense adapter to power our search UI, pulling in widgets likeSearchBoxandHighlightfor instant querying and result highlighting without manual debounce or request handling. Then we add a stats widget to show result counts and search speed, all seamlessly integrated and ready to extend with facets next.How To Use Instant Search With Typesense
Laravel Scout provides a driver-based, unified API for adding full-text search to Eloquent models, letting you swap out search engines like the database driver or TypeSense without changing your code. After installing Scout and configuring thescout.phpfile, you set up your model’s searchable schema, generate dummy data, and import it into TypeSense. Finally, you can perform searches easily using thesearchabletrait and Artisan commands likescout:importto sync your data.Instant Search Faceting
We level up the basic search UI by adding Algolia’s optionalinstantsearch.cssfor quick styling and ditching manual API calls sinceinstantsearchhandles them automatically. Then, we enhance the results with highlighted book titles, authors, and publication years, and introduce faceting using theRefinementListandNumericMenucomponents for filtering by authors and publication year ranges—all while keeping the layout clean with Tailwind and some simple CSS tweaks. The best part? Most of the heavy lifting happens behind the scenes, letting us focus on styling and UX without writing complex search logic.
- Laravel Scout (6)
Search Result Ranking Explained
You dig into how search ranking works by starting with a text match score, then using declared default sorting likeratings_countor publication year as tiebreakers when multiple results share the same match. You also learn how to override sorting withsortByto prioritize things like newest releases or popularity, and see why combining multiple tiebreakers—like text match, release year, then views—makes your results smarter and more relevant. Finally, you get a sneak peek at boosting or promoting specific results regardless of the algorithm, setting you up for customizing search rankings in real apps.Promoted Search Results Using Overrides
We set up a search override that boosts a less popular book to the top of results when someone searches exactly fortwilight, simulating a paid promotion scenario. The override uses a rule targeting the query and moves the specified document ID to position one via theupsertmethod on the collection. Along the way, we handle key details like ensuring IDs are strings for TypeSense and show how to create, apply, and delete these overrides programmatically.An Introduction To Laravel Scout
Laravel Scout offers a driver-based API to add full-text search to your Eloquent models, letting you easily swap search engines like the database driver or TypeSense without changing your code. After installing Scout and configuring thescout.phpfile, you create a model with dummy data, define a TypeSense schema in Scout’s config, and use theSearchabletrait to sync your data. Finally, you import your records into TypeSense and perform searches right from your routes, customizing the default query field as needed.Scout Querying Essentials
Running asearchwith Scout returns an Eloquent collection of model instances, so you can work directly with yourCoursemodels instead of raw data. You can also useraw()to get the original TypeSense response if you need to dig into the hits manually. Filtering works with a simplewheremethod to narrow results by attributes likeuser_idorcategory, and pagination feels just like Eloquent, thanks to Scout’s builder wrapping TypeSense queries.Sync Changed Data With Typesense
You see how using Eloquent model events with theSearchabletrait automatically syncs your database changes with a TypeSense collection—create, update, or delete a record, and Scout handles the rest behind the scenes. But if you insert or update records manually (outside Eloquent), you need to callsearchableorunsearchableyourself to keep things in sync. Plus, you can control which records get indexed by overridingshouldBeSearchableon your model, letting you exclude, say, archived courses from search results.Scout Advanced Configuration
You learn how to fine-tune yourTypeSensesearches by using theoptionsmethod to specify fields likecategoryordescriptionfor querying and filtering results. The lesson also shows how to customize search parameters on the fly—like changing highlight tags—and how to access raw search results for more control, all while keeping the API familiar and easy to work with.
- Going Pro (2)
Semantic Search
Semantic search lets you find results that match the general vibe of a query, not just exact keywords, by converting both documents and queries into numericembeddings. Usingcosine similarity, you compare these embeddings to surface conceptually related matches, and with tools like TypeSense, you can automate embedding generation and vector search without manually handling the math. This approach helps you build smarter searches that understand context, even when the exact terms don’t appear in your data.Typesense Cloud
TypeSense Cloud takes the hassle out of managing your own search server by handling provisioning, scaling, and failover for you, plus it offers a handy dashboard for managing indices and analytics. You’ll see how to set up a cluster, generate API keys, and sync your Laravel app’s data seamlessly, then integrate with InstantSearch.js for lightning-fast, typo-tolerant search with faceting. Finally, it dives into tracking popular and zero-result queries via custom collections, giving you powerful insights to improve your search experience.
