使用Meilisearch

安装Meilisearch SDK

composer require meilisearch/meilisearch-php guzzlehttp/guzzle http-interop/http-factory-guzzle:^1.0


基本使用

<?php
namespace app\controller;


use app\BaseController;
use app\ToolLibrary\JsonUtil;
use app\ToolLibrary\RemoteCallTools;
use Meilisearch\Client;
use think\facade\Env;
use think\facade\View;


// Meilisearch搜索
// 详见PHP官方文档:https://github.com/meilisearch/meilisearch-php
//                https://www.meilisearch.com/docs/learn/self_hosted/getting_started_with_self_hosted_meilisearch


// 安装Meilisearch:composer require meilisearch/meilisearch-php guzzlehttp/guzzle http-interop/http-factory-guzzle:^1.0
class SearchController extends BaseController {


    // 使用示例
    public function index(){


        // 1.0> 通过Meilisearch访问地址和主密码来创建Meilisearch客户端(主密码是在启动Meilisearch时通过master-key选项设置的)
        $client = new Client('http://127.0.0.1:7700', 'masterKey');


        // 1.1> 获取或创建名为 "movies" 的索引
        // 在Meilisearch中,索引是用于组织和管理文档的一种逻辑结构,是一个专门存放特定类型文档的 “容器”;
        $index = $client->index('movies');


        // 1.2> 添加文档到索引
        $documents = [
            ['id' => 1, 'user_id' => 101,  'title' => 'Carol', 'genres' => ['Romance, Drama']],
            ['id' => 2, 'user_id' => 101,  'title' => 'Wonder Woman', 'genres' => ['Action, Adventure']],
            ['id' => 3, 'user_id' => 103,  'title' => 'Life of Pi', 'genres' => ['Adventure, Drama']],
            ['id' => 4, 'user_id' => 103,  'title' => 'Mad Max: Fury Road', 'genres' => ['Adventure, Science Fiction']],
            ['id' => 5, 'user_id' => 103,  'title' => 'Moana', 'genres' => ['Fantasy, Action']],
            ['id' => 6, 'user_id' => 103,  'title' => 'Philadelphia', 'genres' => ['Drama']],
        ];
        $index->addDocuments($documents); // => { "uid": 0 }



        // 2.0> 更新指定的文档内容
        $updatedDocuments = [
            ['id' => 1, 'title' => 'The Great Gatsby (Updated)', 'author' => 'F. Scott Fitzgerald', 'genre' => 'Literature']
        ];
        $index->updateDocuments($updatedDocuments);


        // 3.0> 删除 id 为 2 的文档,支持批量删除
        $index->deleteDocuments([2]); 


        // 4.0> 删除索引
        $client->deleteIndex('books');


        // 5.0> 列出所有索引
        $indexes = $client->getIndexes();


        // 6.0> 搜索 - 基本搜索
        $result = $index->search('inception');


        // 6.1> 搜索 - 从搜索结果获取对象数组
        $result->getHits();


        // 6.2> 搜索 - 搜索指定用户的数据
        $currentUserId = 101;
        $index->updateFilterableAttributes(['user_id']); // 声明需要过滤的属性(只需执行一次即可)
        $result = $index->search('note', [
            'filter' => 'user_id = ' . $currentUserId
        ]);



        // 6.3> 搜索 - 对搜索结果进行排序
        $index->updateSortableAttributes(['created_at']); // 声明需要排序的属性(只需执行一次即可)
        $result = $index->search('关键词', [
            'sort' => ['created_at:desc'] // 按时间降序(最新的在前)
        ]);


        // 6.4> 搜索 - 分页
        $page = 1;       // 当前页,从1开始
        $perPage = 2;    // 每页条数
        $offset = ($page - 1) * $perPage;
        $searchResult = $index->search('the', [
            'limit' => $perPage,
            'offset' => $offset,
            'sort' => ['created_at:desc'],     // 按时间降序
            'filter' => 'genre = Action OR genre = Sci-Fi'
        ]);
        // 总匹配数
        echo "总匹配数: " . $searchResult->getTotalHits() . PHP_EOL;



        // 6.5> 高亮和截断搜索结果
        $result = $index->search('inception', [
            'attributesToHighlight' => ['title'], // 高亮指定字段中的搜索关键字
            'attributesToCrop' => ['description'], // 截断description字段的内容
            'cropLength' => 20 // 设置截断字段 description 被截断后的长度为最多 20 个字符(为了保证输出结果的完整性,此长度设置可能并不准确)
        ]);
        print_r($result->getHits());



        // 6.6> 搜索权重设置
        // 越靠前的字段权重越高,匹配这个字段的结果排名越靠前
        $index->updateSearchableAttributes(['title', 'description', 'content']);
    }


}



举报

© 著作权归作者所有


0