一、简介
MeiliSearch是一个功能强大、快速、开源、易于使用和部署的搜索引擎,并且MeiliSearch的搜索和索引都是高度可定制的,提供开箱即用的功能属性,如错字容忍、过滤器和同义词。而最重要的一点是,它【支持中文搜索】,而不需要添加额外的配置。
MeiliSearch使用RUST语言进行编写 ,RUST语音最重要的特点就是并发安全,同时它还是支持函数式和命令式以及泛型等编程范式的多范式语言。并且在性能方面,MeiliSearch也是媲美C++语音的存在,所以使用RUST语音编写的MeiliSearch搜索引擎,可以说在性能上也是非常的优秀。
特性:
查询速度快,不到50毫秒可以查询出来
拼写错误容忍度低,也能根据语义查询出结果
支持语言,中文、日文、英文等多种语言
过滤和分面搜索,增强用户搜索体验
突出显示,突出显示文档中的结果
多租户和租户令牌。管理复杂的多用户应用程序,租户令牌可帮助您决定每个用户可以搜索哪些文档
地理位置搜索,根据地理位置过滤和排序结果
向量搜索
二、安装及多语言支持
# 安装 Meilisearch
curl -L https://install.meilisearch.com | sh
### 启动 Meilisearch
./meilisearch
支持多语言SDK。
三、laravel 安装
说明:在原有的框架中安装扩展.
1、安装guzzlehttp依赖
composer require meilisearch/meilisearch-php guzzlehttp/guzzle http-interop/http-factory-guzzle:^1.0
2、安装扩展
composer require meilisearch/meilisearch-php symfony/http-client nyholm/psr7:^1.0
3、laravel/scout 安装
composer require laravel/scout
composer require algolia/algoliasearch-client-php
发布
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
修改模型
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Post extends Model {
use Searchable;
}
开启队列
'queue' => true,
队列配置
'queue' => [
'connection' => 'redis',
'queue' => 'scout'
],
队列命令
php artisan queue:work redis --queue=scout
配置
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://127.0.0.1:7700
MEILISEARCH_KEY=masterKey
内容较多,就不一一讲解,可参考文档:https://laravel.com/docs/12.x/scout
4、文档结构
文档是由一个或多个字段组成的对象。每个字段由一个属性及其相关值组成。文档充当组织数据的容器,是 Meilisearch 数据库的基本构建块。要搜索文档,您必须先将其添加到索引中。
数组数据格式:
[
{
"id": 1,
"title": "Diary of a Wimpy Kid: Rodrick Rules",
"author": "Jeff Kinney",
"genres": ["comedy","humor"],
"price": 5.00
},
{
"id": 2,
"title": "Black Leopard, Red Wolf",
"author": "Marlon James",
"genres": ["fantasy","drama"],
"price": 5.00
}
]
单数组格式:
{
"id": 1564
"title": "Kung Fu Panda",
"genres": "Children's Animation",
"release-year": 2008,
"cast": [
{ "Jack Black": "Po" },
{ "Jackie Chan": "Monkey" }
]
}
四、使用操作
1、连接数据库
官方注册个账号,拿到 masterKey
use Meilisearch\Client;
$client = new Client('http://127.0.0.1:7700', 'masterKey');
2、生成文档
生成 movies 的文档 ,并插入数据$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 }
3、搜索内容
// Meilisearch is typo-tolerant:
$hits = $index->search('wondre woman')->getHits();
print_r($hits);
// 输出结果:
Array
(
[0] => Array
(
[id] => 2
[title] => Wonder Woman
[genres] => Array
(
[0] => Action, Adventure
)
)
)
// 搜索结果的几个方法
$facetsDistribution = $searchResult->getFacetsDistribution();
$offset = $searchResult->getOffset();
$hitsCount = $searchResult->getHitsCount();
$hitsCount = $searchResult->count();
$response = $searchResult->getRaw();
$jsonResponse $searchResult->toJson();
$searchResultArray = $searchResult->toArray();
$searchResult->removeZeroFacets();
$searchResult->transformFacetsDistribution($callable);
$searchResult->transformHits($callable);
4、laravel中的搜索,写法有些不同
$movies = Movies::search('Wonder Woman')->paginate(15);
目前美丽搜索的操作有以下几种:
- 创建索引
- 更新索引
- 交换索引
- 删除索引
- 更新索引设置
- 将文档添加到索引
- 更新索引中的文档
- 从索引中删除文档
- 取消任务
- 删除任务
- 创建转储
- 创建快照
五、总结
官网:
https://www.meilisearch.com官网文档
https://www.meilisearch.com/docs/learn/getting_started/indexes