Getting Started with MeiliSearch: Docker Setup, PHP SDK, and Custom Queries
Learn how to install MeiliSearch via Docker, add documents using the PHP SDK, and perform both basic and custom searches, including typo tolerance and highlighted results, with step‑by‑step commands and code examples.
Introduction
MeiliSearch is a fast, open‑source search engine that provides customizable indexing and search features such as filters, synonyms, typo‑tolerance, and Chinese tokenization. It can outperform ElasticSearch on small datasets.
Supported Features
Open source
Second most popular open‑source search engine
Chinese tokenization
Synonyms
Typo‑tolerance
Highlighting
Full‑text response
Advanced search
Stop‑words and stop‑fields
Weighting and de‑ranking
Logical search
Unique field aggregation
Pagination
Re‑indexing
Usage
Step 1: Install MeiliSearch with Docker
Pull the latest Docker image and run the container, exposing port 7700 and mounting a data volume.
# Fetch the latest version of Meilisearch image from DockerHub
docker pull getmeili/meilisearch:latest
# Launch Meilisearch
docker run -it --rm \
-p 7700:7700 \
-v d:/work/meilisearch/data.ms:/data.ms \
getmeili/meilisearch:latestWhen the container starts, a JSON response confirms the server is ready.
Step 2: Add Documents Using the PHP SDK
Install the MeiliSearch PHP client via Composer.
composer require meilisearch/meilisearch-php \
guzzlehttp/guzzle \
http-interop/http-factory-guzzle:^1.0Repository: https://github.com/meilisearch/meilisearch-php/
Create an index and add a set of movie documents.
require_once __DIR__ . '/vendor/autoload.php';
use MeiliSearch\Client;
$client = new Client('http://192.168.3.12:7700');
$index = $client->index('movies');
$documents = [
['id'=>1, 'title'=>'Carol', 'genres'=>['Romance, Drama']],
['id'=>2, 'title'=>'Wonder Woman', 'genres'=>['Action, Adventure']],
['id'=>3, 'title'=>'Life of Pi', 'genres'=>['Adventure, Drama']],
['id'=>4, 'title'=>'Mad Max: Fury Road', 'genres'=>['Adventure, Science Fiction']],
['id'=>5, 'title'=>'Moana', 'genres'=>['Fantasy, Action']],
['id'=>6, 'title'=>'Philadelphia', 'genres'=>['Drama']],
];
$index->addDocuments($documents); // => { "uid": 0 }Step 3: Search Documents
Basic typo‑tolerant search
$client = new Client('http://192.168.3.12:7700');
$index = $client->index('movies');
$hits = $index->search('wondre woman')->getHits();
print_r($hits);Custom search with highlighted results
require_once __DIR__ . '/vendor/autoload.php';
use MeiliSearch\Client;
$client = new Client('http://192.168.3.12:7700');
$index->search(
'phil',
[
'attributesToHighlight' => ['*'],
]
)->getRaw(); // returns array formatThe response includes highlighted matches:
{
"hits": [
{
"id": 6,
"title": "Philadelphia",
"genre": ["Drama"],
"_formatted": {
"id": 6,
"title": "<em>Phil</em>adelphia",
"genre": ["Drama"]
}
}
],
"offset": 0,
"limit": 20,
"processingTimeMs": 0,
"query": "phil"
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
