MeiliSearch
一、快速入门
1. 安装
1.1. cURL
shell
# Install Meilisearch
curl -L https://install.meilisearch.com | sh
# Launch Meilisearch
./meilisearch
1.2. brew
shell
# Update brew and install Meilisearch
brew update && brew install meilisearch
# Launch Meilisearch
meilisearch
1.3. docker
shell
# Fetch the latest version of Meilisearch image from DockerHub
docker pull getmeili/meilisearch:v0.30
# Launch Meilisearch in development mode with a master key
docker run -it --rm \
-p 7700:7700 \
-e MEILI_MASTER_KEY='MASTER_KEY'\
-v $(pwd)/meili_data:/meili_data \
getmeili/meilisearch:v0.30 \
meilisearch --env="development"
2. 批量导入文档
2.1. cURL
shell
curl \
-X POST 'http://localhost:7700/indexes/movies/documents?primaryKey=id' \
-H 'Content-Type: application/json' \
--data-binary @movies.json
2.2. PHP
添加依赖:
shell
composer require meilisearch/meilisearch-php \
guzzlehttp/guzzle \
http-interop/http-factory-guzzle:^1.0
导入文档:
php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Meilisearch\Client;
$client = new Client('http://localhost:7700');
$movies_json = file_get_contents('movies.json');
$movies = json_decode($movies_json);
$client->index('movies')->addDocuments($movies);
2.3. Go
添加依赖:
shell
go get -u github.com/meilisearch/meilisearch-go
导入文档:
go
package main
import (
"os"
"encoding/json"
"io"
"github.com/meilisearch/meilisearch-go"
)
func main() {
client := meilisearch.NewClient(meilisearch.ClientConfig{
Host: "http://localhost:7700",
})
jsonFile, _ := os.Open("movies.json")
defer jsonFile.Close()
byteValue, _ := io.ReadAll(jsonFile)
var movies []map[string]interface{}
json.Unmarshal(byteValue, &movies)
_, err := client.Index("movies").AddDocuments(movies)
if err != nil {
panic(err)
}
}
默认情况下,Meilisearch 将连续的文件请求合并为一个批次,并一起处理它们。这个过程被称为自动批处理,它大大加快了索引的速度。在添加文档后,你应该收到这样的响应:
json
{
"taskUid": 0,
"indexUid": "movies",
"status": "enqueued",
"type": "documentAdditionOrUpdate",
"enqueuedAt": "2021-08-11T09:25:53.000000Z"
}
Meilisearch 的大多数数据库操作是异步的。这意味着 API 请求不是被即时处理,而是被添加到一个队列中,一次一个地处理。
使用返回的 taskUid
来检查你的文件的状态:
cURL
shell
curl \
-X GET 'http://localhost:7700/tasks/0'
PHP
php
$client->getTask(0);
Go
go
client.GetTask(0)