meilisearch/meilisearch-php

PHP wrapper for the MeiliSearch API

v0.14.1 2020-09-23 13:34 UTC

README

MeiliSearch-PHP

MeiliSearch PHP

MeiliSearch | Website | Blog | Twitter | Documentation | FAQ

Latest Stable Version Test License Slack

⚡ 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

To get started, simply require the project using Composer.
You will also need to install packages that "provide" psr/http-client-implementation and psr/http-factory-implementation.
A list with compatible HTTP clients and client adapters can be found at php-http.org.

If you don't know which HTTP client to use, we recommend using Guzzle with the php-http/guzzle6-adapter:

$ composer require meilisearch/meilisearch-php php-http/guzzle6-adapter:^2.0 http-interop/http-factory-guzzle:^1.0

Here is an example of installation with the symfony/http-client:

$ composer require meilisearch/meilisearch-php symfony/http-client nyholm/psr7:^1.0

More HTTP client installations compatible with this package can be found in this section.

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"']);

🧰 HTTP Client Compatibilities

You could use any PSR-18 compatible client to use with this SDK. No additional configurations are required.
A list of compatible HTTP clients and client adapters can be found at php-http.org.

If you want to use this meilisearch-php:

  • with symfony/http-client, run:
$ composer require meilisearch/meilisearch-php symfony/http-client nyholm/psr7:^1.0
  • with php-http/curl-client, run:
$ composer require meilisearch/meilisearch-php php-http/curl-client nyholm/psr7:^1.0
  • with kriswallsmith/buzz, run:
$ composer require meilisearch/meilisearch-php kriswallsmith/buzz nyholm/psr7:^1.0
  • with guzzlehttp/guzzle, run:
composer require meilisearch/meilisearch-php guzzlehttp/guzzle http-interop/http-factory-guzzle:^1.0

⚙️ 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.