spatie/laravel-site-search
Crawl and index your Laravel site for fast full-text search—like a private Google. Highly customizable crawling and indexing, with concurrent requests. Uses SQLite FTS5 by default (no external services), or Meilisearch for advanced features.
The Meilisearch driver provides blazing fast search with advanced features like synonyms and custom ranking rules. It requires a running Meilisearch instance.
First, update your config/site-search.php to use the Meilisearch driver:
'default_driver' => Spatie\SiteSearch\Drivers\MeiliSearchDriver::class,
Or set the driver_class on a specific SiteSearchConfig model to use Meilisearch for individual indexes.
Next, require the Meilisearch PHP client:
composer require meilisearch/meilisearch-php
Then, install Meilisearch. Head over to the Meilisearch docs to learn how to install it on your system.
Here are the steps for installing it on a Forge provisioned server. You must first download the stable release:
curl -L https://install.meilisearch.com | sh
Next, you must change the ownership and modify permission:
chmod 755 meilisearch
chown root:root meilisearch
After that, move the binary to a system-wide available path:
sudo mv meilisearch /usr/bin/
Finally, you can run the binary and make sure it keeps running. In the Forge Dashboard, click on "Daemons" under "Server Details". Fill out the following for a new daemon:
meilisearch --master-key=SOME_MASTER_KEY --env=production --http-addr 0.0.0.0:7700 --db-path ./home/forge/meilifilesforge1These instructions were taken from this gist by Jose Soto.
Meilisearch exposes its API to create indexes, update settings, and retrieve results on HTTP port 7700. If you publicly expose that port, we highly recommend using Meilisearch built-in authentication features to prevent unauthorized persons making requests against your Meilisearch installation.
To avoid unauthorized access, either block Meilisearch's default port (7700) in your firewall, or use authentication.
In the Meilisearch docs you'll find how to start Meilisearch with a master password and how to retrieve the API Keys.
Here's how you can create a new API key for a given master key:
curl -X POST 'http://localhost:7700/keys' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <MASTER-KEY>' \
--data-binary '{
"description": "Site search key",
"actions": ["*"],
"indexes": ["*"],
"expiresAt": null
}'
You can view all previously created keys with this command:
curl -X GET 'http://localhost:7700/keys' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <MASTER-KEY>'
You can specify the API Key (can be public or private key) that this package should use by adding a meilisearch.apiKey JSON value to the extra attribute in the site_search_configs table:
{"meilisearch": {"apiKey": "your-private-or-public-api-key"}}
By default, the Meilisearch driver connects to http://localhost:7700.
You can customize this by adding a meilisearch.url JSON value to the extra attribute in the site_search_configs table:
{"meilisearch": {"url": "https://your-custom-domain-and-port.com:1234"}}
A Meilisearch index has various interesting settings that allow you to specify which fields are searchable, ranking rules, and even synonyms.
Every time a site is crawled, a new index is created. You can customize the settings that are used for this index in two ways.
The first way would be by adding a meilisearch.indexSettings JSON value to the extra attribute in the site_search_configs. In meilisearch.indexSettings you can put any of the list settings that Meilisearch provides.
Here's the value you would put in extra if you only want results based on the url and description fields in the index.
{
"meilisearch": {
"indexSettings": {
"searchableAttributes": [
"url",
"description"
]
}
}
}
Here's another example where we are going to add a synonym for the word "computer". You can read more about how synonyms can be configured in the Meilisearch docs.
{
"meilisearch": {
"indexSettings": {
"synonyms": {
"Macintosh": [
"computer"
]
}
}
}
}
The second way to customize index settings would be by leveraging the Spatie\SiteSearch\Events\NewIndexCreatedEvent. This event is fired whenever a new index is created. It has two properties:
Spatie\SiteSearch\Models\SiteSearchConfigYou can use these properties to make an API call of your own to Meilisearch to customize any of the available settings.
How can I help you explore Laravel packages today?