Download the PHP package onurakman/laravel-meilisearch without Composer
On this page you can find all versions of the php package onurakman/laravel-meilisearch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-meilisearch
Laravel Meilisearch with QueryBuilder
When you want to use Meilisearch in your Laravel application, you can use Laravel Scout. This is an easy way to sync your models to Meilisearch and quickly search models using Meilisearch. However, sometimes Laravel Scout is not enough. For example if you want:
- More control over your Meilisearch database: do not only save models for example.
- Set searchable, filterable or sortable attributes.
- Perform more complex queries to Meilisearch, for example with multiple filters.
- Use a query builder to fetch data from the Meilisearch database.
- To use some functionalities that are not available in Meilisearch out-of-the-box. For example, displaying documents in random order or displaying facets that are not available in the filtered documents.
This package deals with these kind of situations. You decide which information to send to Meilisearch, and which information you want back. The query builder specifically built for Meilisearch helps to build more complex queries.
When using this package, you should determine yourself when and which data you sent to Meilisearch. So if you automatically want to sent models to Meilisearch after a model is saved or created, Laravel Scout might be a better solution.
Compatibility with Meilisearch
Currently, this package supports Meilisearch up to version 0.27. Version 0.28 of Meilisearch introduced some breaking changes. A new version of this package compatible with 0.28 will be released soon.
Installation
When using Meilisearch up to version 0.27, use the following command:
When using Meilisearch version 0.28:
Setup .env
Change your .env to include the following variables:
Publish assets
Getting started
Create an index
First you need to create an index to save documents to. For example, you need an index to save our products catalogue to. So the following command can be used:
This command will create a file database/meilisearch/products.php
. In this file, you can adjust settings for this index. This is not required, however it is highly recommended. If you leave the standard settings, Meilisearch will use all columns of your data to search on. To achieve this, Meilisearch must index all columns of your data. This will take a longer time, and uses more server resources. That's why it is recommended to specify which columns should be searchable, filterable and sortable.
Everytime you want to change something to the settings, simply change this file. After the changes, run the command below.
Migrate the index to the Meilisearch database
Now the index has to be actually created. To achieve this, run the following command:
Compare this to the database migrations of Laravel. First you have to create a database migration, next you have to run the migration to actually create the table, or make the adjustment.
Run this command every time you make changes to the database/meilisearch/products.php
file. Also, run this command on every deployment, so you have an up-to-date Meilisearch instance in production.
Master data in Meilisearch
All the functionalities that are mentioned in the Meilisearch docs are available in this package. The most important functionalities are listed below:
Insert data
To insert a document in the index products
, you can do 1 of the following:
You can also directly insert a model or collection. A model gets converted to an array. In order to do this, the package check if the following methods exists on the object, in the following order:
A Product
model for example, can look like the following:
The model can be inserted like this:
A collection can also be directly inserted:
Retrieve data
Documents of an index can be retrieved using the getDocuments
method. When you want to apply filters, the query builder can be used. The data is automaticly paginated.
Use the query builder
If you want to apply filtering or sorting, I recommend using the query builder. You can take a look in the tests folder to see some examples. A few simple examples are listed below.
Filter on attribute
Simple filtering can be done using the where
method:
Multiple wheres
can also be combined:
Filter with 'or'
Currently, it is not possible to filter with 'OR' on the top-level. If you want to filter with 'OR', you have to create a 'where-group' first. The following call will generate an error:
The following code will work however:
This is because of the way Meilisearch filters work, and how this package renders the filters. It also prevents possible issues when combining 'AND' and 'OR' statements. For example, the following query could return unexpected results:
Should this query be:
So for now, when using an 'OR' statement, you should start a where
-group first.
Where in
This works best with arrays. For example, you have a product with multiple categories:
This data can be queried:
The whereIn
method will check if at least 1 of the values is present on the model. So the query above, will return all documents.
Where matches
The whereIn
method will check if at least 1 of the values is present on the model. The whereMatches
method will check if ALL values are present on the model:
Using facets
Columns that are attributed as filterable
can be used in facets. The querybuilder will return these facets with a product-count attached to it. The facets can be defined by using the setFacets
or addFacet
methods:
Disjunctive facets distribution
In the current version of Meilisearch, facets of an attribute are not returned when you are filtering on an attribute. See the following discussion: https://github.com/meilisearch/product/discussions/187
For example, when you run the above query, the colors grey
, silver
, gold
, yellow
are returned. Next, you only want to display the products with a yellow
color. So you apply a filter:
However when you do this, the facet color
will now only return yellow
. That makes it more difficult to display all the possible colors to the end-user. Thats why this package has a keepFacetsInMetadata
method. You can apply filters inside this method, which will not be applied when fetching metadata.
When using the keepFacetsInMetadata
method, the package will create 2 Meilisearch queries. 1 query with all the filters applied to fetch the products, and 1 query with some of the filters to fetch the metadata (facets). So the above example can be changed to the following:
When doing this, the returned data will have all the facets that are available on the products in the category phones
. So you can easily display all the colors that are available, even when you are filtering on a color.
Be aware that this method will generate another query. Because most of the times the Meilisearch queries are very fast (< 10ms), I believe this will not cause any significant impact on the site speed.
Limits and offsets
Limits and offsets can easily be added to the query. The following query will return 10 results, starting from the 20th result:
Paginate the results
Just like using the Laravel querybuilder for the database, you can paginate the results coming from Meilisearch. Simply use the paginate
method. When using this method, earlier calls to limit
and offset
are ignored.
Optionally supply the name of the query-parameter to use to fetch the current page. 'page' is used by default.
Sort the results
In random order
Out of the box, Meilisearch does not offer the option to randomly order documents. However, sometimes you want to display a few random products. To make this possible, this package adds this functionality. Be aware that the package will make a query to your Meilisearch database for every random element, plus 1 extra query. So if you want to fetch 100 documents in random order, there will be 101 queries made. Meilisearch queries are very fast, however when you make this kind of number of queries, it can still become slow. So I recommend to use this method only with a low number of documents (less than 10), or for example cache the results.
All versions of laravel-meilisearch with dependencies
guzzlehttp/guzzle Version ^7.4
illuminate/http Version ^8|^9
illuminate/support Version ^8|^9