Skip to content
PHP wrapper for the MeiliSearch API
PHP
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows Update tests.yml Jan 7, 2020
src
tests
.editorconfig
.gitignore
.php_cs.dist Add first version of PHP SDK Jan 7, 2020
LICENSE
README.md
composer.json

README.md

MeiliSearch PHP Client

Licence Actions Status Latest Stable Version

The PHP client for MeiliSearch API.

MeiliSearch provides an ultra relevant and instant full-text search. Our solution is open-source and you can check out our repository here.

Here is the MeiliSearch documentation 📖

Table of Contents

🔧 Installation

With composer:

$ composer require meilisearch/meilisearch-php

Run MeiliSearch

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 --api-key=apiKey

NB: you can also download MeiliSearch from Homebrew or APT.

🚀 Getting started

Add documents

use MeiliSearch\Client;

$client = new Client('http://localhost:7700', 'apiKey');
$index = $client->createIndex('Books', 'booksUid'); // If your index does not exist
$index = $client->getIndex('booksUid');             // If you already created your index

$documents = [
    ['id' => 123,  'title' => 'Pride and Prejudice'],
    ['id' => 456,  'title' => 'Le Petit Prince'],
    ['id' => 1,    'title' => 'Alice In Wonderland'],
    ['id' => 1344, 'title' => 'The Hobbit'],
    ['id' => 4,    'title' => 'Harry Potter and the Half-Blood Prince'],
    ['id' => 42,   'title' => 'The Hitchhiker\'s Guide to the Galaxy'],
];

$index->addOrReplaceDocuments($documents); // => { "updateId": 1 }

With the updateId, you can check the status of your documents addition thanks to this method.

Search in index

// MeiliSearch is typo-tolerant:
print_r($index->search('hary pottre'));

Output:

Array
(
    [hits] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [title] => Harry Potter and the Half-Blood Prince
                )

        )

    [offset] => 0
    [limit] => 20
    [processingTimeMs] => 1
    [query] => hary pottre
)

🎬 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 with a specific uid (uid must be unique)
$index = $client->createIndex('Books', 'booksUid');

List all indexes

$client->getAllIndexes();

Get an index object

$client->getIndex('booksUid');

Documents

Fetch documents

// Get one document
$index->getDocument(123);
// Get documents by batch
$index->getDocuments(['offset' => 10 , 'limit' => 20]);

Add documents

$index->addOrReplaceDocuments([['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 satus
$index->getAllUpdateStatus();

Search

Basic search

$index->search('prince');
{
    "hits": [
        {
            "id": 456,
            "title": "Le Petit Prince"
        },
        {
            "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": [
        {
            "id": 456,
            "title": "Le Petit Prince"
        }
    ],
    "offset": 0,
    "limit": 1,
    "processingTimeMs": 10,
    "query": "prince"
}

🤖 Compatibility with MeiliSearch

This package works for MeiliSearch v0.8.x.

You can’t perform that action at this time.