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.
- 🔧 Installation
- 🚀 Getting started
- 🤖 Compatibility with MeiliSearch
- 🎬 Examples
- 🧰 Replace HTTP client
- ⚙️ Development Workflow and Contributing
With composer:
$ composer require meilisearch/meilisearch-php
There are many easy ways to download and run a MeiliSearch instance.
For example, if you use Docker:
$ docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey
NB: you can also download MeiliSearch from Homebrew or APT.
<?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.
// 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
)
This package is compatible with the following MeiliSearch versions:
v0.12.X
v0.11.X
All HTTP routes of MeiliSearch are accessible via methods in this SDK.
You can check out the API documentation.
// Create an index
$index = $client->createIndex('books');
// Create an index and give the primary-key
$index = $client->createIndex(
'books',
['primaryKey' => 'book_id']
);
// 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 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']
);
$client->getAllIndexes();
$client->getIndex('books');
// Get one document
$index->getDocument(123);
// Get documents by batch
$index->getDocuments(['offset' => 10 , 'limit' => 20]);
$index->addDocuments([['book_id' => 2, 'title' => 'Madame Bovary']])
Response:
{
"updateId": 1
}
With this updateId
you can track your operation update.
// Delete one document
$index->deleteDocument(2);
// Delete several documents
$index->deleteDocuments([1, 42]);
// Delete all documents /!\
$index->deleteAllDocuments();
// Get one update status
// Parameter: the updateId got after an asynchronous request (e.g. documents addition)
$index->getUpdateStatus(1);
// Get all update satus
$index->getAllUpdateStatus();
$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"
}
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"']);
By default Guzzle is used for all HTTP requests.
But you can replace the used HTTP client with any PSR-18 compatible client.
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 HttpClient, use the following code:
$ composer require symfony/http-client
$ composer require nyholm/psr7
use Symfony\Component\HttpClient\HttplugClient;
use MeiliSearch\Client;
$httpClient = new HttplugClient();
$client = new Client('http://127.0.0.1:7700', 'masterKey', $httpClient);
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.