MeiliSearch PHP
MeiliSearch | Website | Blog | Twitter | Documentation | FAQ
⚡ Lightning Fast, Ultra Relevant, and Typo-Tolerant Search Engine MeiliSearch client written in PHP
MeiliSearch PHP is a client for MeiliSearch written in PHP. MeiliSearch is a powerful, fast, open-source, easy to use and deploy search engine. Both searching and indexing are highly customizable. Features such as typo-tolerance, filters, and synonyms are provided out-of-the-box.
Table of Contents
- 🔧 Installation
- 🚀 Getting started
- 🤖 Compatibility with MeiliSearch
- 🎬 Examples
- 🧰 Use of other HTTP clients
- ⚙️ Development Workflow and Contributing
🔧 Installation
You will have to install meilisearch-php sdk and a PSR-18 compatible http client to use the sdk. Since we do not ship a default http client with the package to avoid package collisions with existing libraries or projects. A list with compatible HTTP clients and client adapters can be found at php-http.org.
Most users probably want the following.
With composer:
$ composer require meilisearch/meilisearch-php php-http/guzzle6-adapter:^2.0
Or with symfony/http-client
$ composer require meilisearch/meilisearch-php symfony/http-client
More use cases can be found under Use of other HTTP clients
Run MeiliSearch
There are many easy ways to download and run a MeiliSearch instance.
For example, if you use Docker:
$ docker pull getmeili/meilisearch:latest # Fetch the latest version of MeiliSearch image from Docker Hub
$ docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey
NB: you can also download MeiliSearch from Homebrew or APT.
🚀 Getting started
Add documents
<?php
require_once __DIR__ . '/vendor/autoload.php';
use MeiliSearch\Client;
$client = new Client('http://127.0.0.1:7700', 'masterKey');
$index = $client->createIndex('books'); // If your index does not exist
$index = $client->getIndex('books'); // If you already created your index
$documents = [
['book_id' => 123, 'title' => 'Pride and Prejudice', 'author' => 'Jane Austen'],
['book_id' => 456, 'title' => 'Le Petit Prince', 'author' => 'Antoine de Saint-Exupéry'],
['book_id' => 1, 'title' => 'Alice In Wonderland', 'author' => 'Lewis Carroll'],
['book_id' => 1344, 'title' => 'The Hobbit', 'author' => 'J. R. R. Tolkien'],
['book_id' => 4, 'title' => 'Harry Potter and the Half-Blood Prince', 'author' => 'J. K. Rowling'],
['book_id' => 42, 'title' => 'The Hitchhiker\'s Guide to the Galaxy', 'author' => 'Douglas Adams, Eoin Colfer, Thomas Tidholm'],
];
$index->addDocuments($documents); // => { "updateId": 0 }
With the updateId
, you can check the status (processed
or failed
) of your documents addition thanks to this method.
Search in index
// MeiliSearch is typo-tolerant:
print_r($index->search('harry pottre'));
Output:
Array
(
[hits] => Array
(
[0] => Array
(
[id] => 4
[title] => Harry Potter and the Half-Blood Prince
)
)
[offset] => 0
[limit] => 20
[processingTimeMs] => 1
[query] => harry pottre
)
🤖 Compatibility with MeiliSearch
This package is compatible with the following MeiliSearch versions:
v0.14.X
v0.13.X
🎬 Examples
All HTTP routes of MeiliSearch are accessible via methods in this SDK.
You can check out the API documentation.
Indexes
Create an index
// Create an index
$index = $client->createIndex('books');
// Create an index and give the primary-key
$index = $client->createIndex(
'books',
['primaryKey' => 'book_id']
);
Get an index or create it if it doesn't exist
// Get or create an index
$index = $client->getOrCreateIndex('books');
// Get or create an index and give the primary-key
$index = $client->getOrCreateIndex(
'books',
['primaryKey' => 'book_id']
);
Get an index or create it if it doesn't exist
// Get or create an index
$index = $client->getOrCreateIndex('books');
// Get or create an index and give the primary-key
$index = $client->getOrCreateIndex(
'books',
['primaryKey' => 'book_id']
);
List all indexes
$client->getAllIndexes();
Get an index object
$client->getIndex('books');
Documents
Fetch documents
// Get one document
$index->getDocument(123);
// Get documents by batch
$index->getDocuments(['offset' => 10 , 'limit' => 20]);
Add documents
$index->addDocuments([['book_id' => 2, 'title' => 'Madame Bovary']])
Response:
{
"updateId": 1
}
With this updateId
you can track your operation update.
Delete documents
// Delete one document
$index->deleteDocument(2);
// Delete several documents
$index->deleteDocuments([1, 42]);
// Delete all documents /!\
$index->deleteAllDocuments();
Update status
// Get one update status
// Parameter: the updateId got after an asynchronous request (e.g. documents addition)
$index->getUpdateStatus(1);
// Get all update status
$index->getAllUpdateStatus();
Search
Basic search
$index->search('prince');
{
"hits": [
{
"book_id": 456,
"title": "Le Petit Prince"
},
{
"book_id": 4,
"title": "Harry Potter and the Half-Blood Prince"
}
],
"offset": 0,
"limit": 20,
"processingTimeMs": 13,
"query": "prince"
}
Custom search
All the supported options are described in this documentation section.
$index->search('prince', ['limit' => 1]);
{
"hits": [
{
"book_id": 456,
"title": "Le Petit Prince"
}
],
"offset": 0,
"limit": 1,
"processingTimeMs": 10,
"query": "prince"
}
With limit and filter, both single and double quotes are supported.
// Enclosing with double quotes
$index->search('prince', ['limit' => 2, 'filters' => "title = 'Le Petit Prince' OR author = 'J. R. R. Tolkien'"]);
// Enclosing with single quotes
$index->search('hobbit', ['limit' => 2, 'filters' => 'title = "The Hitchhiker\'s Guide to the Galaxy" OR author = "J. R. R. Tolkien"']);
🧰 Use a different http client
you could use any PSR-18 compatible client to use with meilisearch. No additional configurations are required
A list with compatible HTTP clients and client adapters can be found at php-http.org.
For example, if you want to use the symfony/http-client
$ composer require meilisearch/meilisearch-php symfony/http-client
- Or with
php-http/curl-client
$ composer require meilisearch/meilisearch-php php-http/curl-client
- Or with
kriswallsmith/buzz
$ composer require meilisearch/meilisearch-php kriswallsmith/buzz
Note: Guzzle 7 is not yet supported, but we don't prevent you from installing Guzzle 7 in your project.
⚙️ Development Workflow and Contributing
Any new contribution is more than welcome in this project!
If you want to know more about the development workflow or want to contribute, please visit our contributing guidelines for detailed instructions!
MeiliSearch provides and maintains many SDKs and Integration tools like this one. We want to provide everyone with an amazing search experience for any kind of project. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the integration-guides repository.