devrck / meilisearch-php

PHP wrapper for the MeiliSearch API

Home Page:https://github.com/meilisearch/MeiliSearch

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MeiliSearch-PHP

MeiliSearch PHP

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

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 ./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.12.X
  • v0.11.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 satus
$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"']);

🧰 Replace HTTP client

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);

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

About

PHP wrapper for the MeiliSearch API

https://github.com/meilisearch/MeiliSearch

License:MIT License


Languages

Language:PHP 99.8%Language:Shell 0.2%