Using the database driver | laravel-site-search | Spatie

 SPATIE

  Laravel Site Search
======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-site-search](https://spatie.be/docs/laravel-site-search/v3)  Advanced-usage  Using the database driver

 Version   v3   v1

 Other versions for crawler [v3](https://spatie.be/docs/laravel-site-search/v3) [v1](https://spatie.be/docs/laravel-site-search/v1)

- [ Introduction ](https://spatie.be/docs/laravel-site-search/v3/introduction)
- [ Support us ](https://spatie.be/docs/laravel-site-search/v3/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-site-search/v3/requirements)
- [ Installation and setup ](https://spatie.be/docs/laravel-site-search/v3/installation-setup)
- [ About us ](https://spatie.be/docs/laravel-site-search/v3/about-us)
- [ Questions and issues ](https://spatie.be/docs/laravel-site-search/v3/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-site-search/v3/changelog)

Basic usage
-----------

- [ High level overview ](https://spatie.be/docs/laravel-site-search/v3/basic-usage/high-level-overview)
- [ Indexing your first site ](https://spatie.be/docs/laravel-site-search/v3/basic-usage/indexing-your-first-site)
- [ Retrieving results ](https://spatie.be/docs/laravel-site-search/v3/basic-usage/retrieving-results)
- [ Preventing content from being indexed ](https://spatie.be/docs/laravel-site-search/v3/basic-usage/preventing-content-from-being-indexed)
- [ Using a search profile ](https://spatie.be/docs/laravel-site-search/v3/basic-usage/using-a-search-profile)
- [ Listing indexes ](https://spatie.be/docs/laravel-site-search/v3/basic-usage/listing-indexes)
- [ Troubleshooting ](https://spatie.be/docs/laravel-site-search/v3/basic-usage/troubleshooting)

Advanced usage
--------------

- [ Creating multiple search indexes ](https://spatie.be/docs/laravel-site-search/v3/advanced-usage/creating-multiple-search-indexes)
- [ Using a custom indexer ](https://spatie.be/docs/laravel-site-search/v3/advanced-usage/using-a-custom-indexer)
- [ Indexing extra properties ](https://spatie.be/docs/laravel-site-search/v3/advanced-usage/indexing-extra-properties)
- [ Available events ](https://spatie.be/docs/laravel-site-search/v3/advanced-usage/available-events)
- [ Using the database driver ](https://spatie.be/docs/laravel-site-search/v3/advanced-usage/using-the-database-driver)
- [ Using the Meilisearch driver ](https://spatie.be/docs/laravel-site-search/v3/advanced-usage/using-the-meilisearch-driver)
- [ Building a Filament integration ](https://spatie.be/docs/laravel-site-search/v3/advanced-usage/filament-integration)

 Using the database driver
=========================

###  On this page

1. [ How it works ](#content-how-it-works)
2. [ Using a different database connection ](#content-using-a-different-database-connection)
3. [ Differences from the Meilisearch driver ](#content-differences-from-the-meilisearch-driver)

The database driver is the default driver. It stores search documents in your application's database and uses the database's native full-text search capabilities. SQLite (FTS5), MySQL (FULLTEXT), and PostgreSQL (tsvector) are all supported.

How it works
--------------------------------------------------------------------------------------------

The database driver stores all indexed documents in a single `site_search_documents` table. Each document is associated with an `index_name`, which allows multiple search indexes to coexist in the same table.

Full-text search infrastructure (virtual tables, indexes, triggers) is created automatically at runtime based on which database you are using. The migration itself is database-agnostic.

### Database-specific behavior

**SQLite** uses FTS5 virtual tables with porter stemming and unicode support. BM25 ranking weights matches in headings higher than body content. Highlighted snippets are generated natively by FTS5.

**MySQL** uses FULLTEXT indexes with boolean mode search. Highlighting is done in PHP after the query.

**PostgreSQL** uses tsvector columns with GIN indexes and weighted vectors. `ts_rank()` is used for ranking and `ts_headline()` for highlighting.

### Search features

All three databases provide:

- Full-text search with stemming (e.g. "running" matches "run")
- Prefix matching (e.g. "auth" matches "authentication")
- Relevance ranking with field-specific weights
- Highlighted snippets with matching terms wrapped in `` tags
- Deep linking with anchor links to specific sections (see [Retrieving results](/docs/laravel-site-search/v1/basic-usage/retrieving-results#deep-linking-to-sections))

Using a different database connection
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

By default, the database driver uses your application's default database connection. You can use a different connection by setting a `database.connection` value in the `extra` attribute of the `site_search_configs` table:

```
{"database": {"connection": "mysql"}}
```

This lets you store search data in a separate database from your application data.

Differences from the Meilisearch driver
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

- No external service required, uses your existing database
- No support for synonyms or custom ranking rules
- Indexing is synchronous (no background processing)
