PHP系列 | MeiliSearch 轻量搜索引擎入门介绍
介绍
MeiliSearch是一个功能强大,快速,开源,易于使用和部署的搜索引擎。搜索和索引都是高度可定制的。允许输入、过滤器和同义词等特性都是开箱即用的。是近两年开源的项目,同样也支持中文分词,在小数据规模下可以实现比ElasticSearch更加快速和易用的搜索体验。
第 1 步:设置和安装
我们将从下载和安装 Meilisearch 开始。您可以选择在本地安装 Meilisearch 或通过云服务部署
Docker部署
# 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:latest
运行Meilisearch
成功运行 Meilisearch 后,您应该会看到以下响应:
恭喜!您已准备好继续下一步!
第 2 步:添加文档
对于这个快速入门,我们将使用PHP作为演示案例
安装PHP的SDK
1 2 3 | composer require meilisearch/meilisearch-php \ guzzlehttp/guzzle \ http-interop/http-factory-guzzle:^1.0 |
SDK 地址 https://github.com/meilisearch/meilisearch-php/
添加索引文档
require_once __DIR__ . '/vendor/autoload.php';
use MeiliSearch\Client;
$client = new Client('http://192.168.3.12:7700');
# An index is where the documents are stored.
$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']],
];
# If the index 'movies' does not exist, Meilisearch creates it when you first add the documents.
$index->addDocuments($documents); // => { "uid": 0 }
基础搜索
1 2 3 4 5 6 7 | $client = new Client( 'http://192.168.3.12:7700' ); # An index is where the documents are stored. $index = $client ->index( 'movies' ); // Meilisearch is typo-tolerant: $hits = $index ->search( 'wondre woman' )->getHits(); print_r( $hits ); |
打印信息
自定义搜索
1 2 3 4 5 6 | $index ->search( 'phil' , [ 'attributesToHighlight' => [ '*' ], ] )->getRaw(); // Return in Array format |
输出结果
{
"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"
}
标签:
php7
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2018-03-13 Golang入门教程(十二)安装注意事项
2018-03-13 Golang入门教程(十一)beego 框架之RESTful Controller 路由