技术饭

PHP搜索引擎MeiliSearch实现全文检索

copylian    0 评论    9340 浏览    2023.06.01

PHP搜索引擎MeiliSearch实现全文检索,Meilisearch 搜索引擎使用 Rust 语言开发,轻量,对中文搜索非常友好,几乎做到了零配置,零学习成本,部署即用,非常方便。建立在 LMDB 键值存储之上,安装在 Ubuntu 或 MacOS 上时,它以 35 MB 的二进制文件形式存在。 MeiliSearch 带有内置的客户端、服务器和 WebUI。 词干提取、停用词、同义词、排名、过滤器和分面等功能都是开箱即用的,使用合理的默认值并且可以轻松定制。

Meilisearch 官网:https://www.meilisearch.com/

一、安装:

下载https://github.com/meilisearch/meilisearch/releases/tag/v1.1.1

windows下载meilisearch-windows-amd64.exe,放到E:\meilisearch

配置环境变量、常量:用户变量主要是针对 meilisearch 一些配置,基本以 MEILI 为前缀开头,我们先配置 MEILI_DB_PATH 的值为 E:\meilisearch\data.ms,有需要接口认证的,可以配置 MEILI_MASTER_KEY 为 你要设置的 key,其他环境变量

微信图片_20230601120847.png

微信图片_20230601120921.png

打开 powershell 或 cmd 执行:meilisearch -V 查看版本

微信图片_20230601121613.png

访问:http://localhost:7700

微信图片_20230601121750.png


二、使用:

执行结果可以通过:http://localhost:7700/tasks/返回的taskid,来查看

Meilisearch PHP SDK:https://github.com/meilisearch/meilisearch-php/

1、SDK方式:composer 安装之后

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Meilisearch\Client;

const MEILISEARCH_SCRET = 123456;

const MEILISEARCH_HOST = "http://localhost:7700";


//实例化

$client = new Client(MEILISEARCH_HOST, MEILISEARCH_SCRET);


//添加index

$index = $client->index('movies');

$documents = [

        ['id' => 1,  'title' => 'Carol2', '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']],

];


//不存在则创建

$res = $index->addDocuments($documents); // => { "uid": 0 }

print_r($res);


//搜索:获取hits

$hits = $index->search('Fantasy')->getHits();

print_r($hits);


//搜索:返回原始数组

$res = $index->search(

    'phil',

[

    'attributesToHighlight' => ['*'],

])->getRaw();

print_r($res);


//如果想通过属性来筛选则需要设置筛选属性

$res = $index->updateFilterableAttributes([

        'id',

        'genres'

]);

print_r($res);


//搜索:通过属性条件筛选

$res = $index->search(

    'wonder',

[

    'filter' => ['id > 1 AND genres = Action']

]);

print_r($res);


2、基础curl方式

<?php

const MEILISEARCH_SCRET = 123456;

const MEILISEARCH_HOST = "http://localhost:7700";

const INDEX_NAME = "order";

const INDEXS = [

        'searchableAttributes' => ['name', 'age'], // 属性可以进行搜索,默认全部

        'sortableAttributes' => ['created_time'], // 排序属性

        'filterableAttributes' => [ // 过滤属性

                'id',

                'name',

                'sex',

                'age',

                'created_time',

        ]

];


/**

* 处理curl

* @param $url  // url地址

* @param $type // GET、POST、PUT、DELETE

* @param $data // 发送数据

* @return mixed

*/

function dealCurl(string $url = '', string $type = '', array $data = []):mixed

{

        //初始化curl

        $handle = curl_init();

        curl_setopt_array(

                $handle, [

                        CURLOPT_URL => $url,

                        CURLOPT_MAXREDIRS => 5,

                        CURLOPT_RETURNTRANSFER => true,

                        CURLOPT_FOLLOWLOCATION => true,

                        CURLOPT_SSL_VERIFYPEER => true,

                        CURLOPT_SSL_VERIFYHOST => 2,

                        CURLOPT_TIMEOUT => 60,

                        CURLOPT_CUSTOMREQUEST => $type,

                        CURLOPT_HTTPHEADER => [

                                'authorization: Bearer '.MEILISEARCH_SCRET,

                                'content-type: application/json'

                       ],

                        CURLOPT_POSTFIELDS => json_encode($data)

                ]

        );


        // 执行

        $result = curl_exec($handle);


        //关闭

        curl_close($handle);


        //返回

        return json_decode($result,true);

}


//设置ID为主键

$url = MEILISEARCH_HOST."/indexes/";

$res = dealCurl(url:$url, type:'POST', data:['uid'=> INDEX_NAME,'primaryKey'=> 'id']);

print_r($res);


//设置索引

$url = MEILISEARCH_HOST."/indexes/".INDEX_NAME."/settings";

$res = dealCurl(url:$url, type:'PATCH', data:INDEXS);

print_r($res);


//写入、更新数据

$i = 4;

$data  = [

        'id' => $i, //id是主键,存在则更新不存在则新增

        'name' => 'copylian'.$i,

        'age' => $i+1,

        'sex' => $i+2,

        'created_time' => $i+5,

        'aaaaa' => 6666 //不存在的字段也可以写入,如果不在搜索范围内则不能搜索

];

$url = MEILISEARCH_HOST."/indexes/".INDEX_NAME."/documents";

$res = dealCurl(url:$url, type:'PUT', data:$data);

print_r($res);


//删除数据

$id = 2;

$url = MEILISEARCH_HOST."/indexes/".INDEX_NAME."/documents/".$id; //删除某个索引

$url = MEILISEARCH_HOST."/indexes/".INDEX_NAME; //删除整个索引

$res = dealCurl(url:$url, type:'DELETE', data:[]);

print_r($res);


//搜索数据

$keyword = "啤酒";

$page = 1;

$limit = 200;

$sort = ["created_datetime:asc"];

$filter = ["name=Hello"];

$data = [

        'offset'=> $page ? --$page * $limit : 0,

        'limit'=> $limit,

        'filter'=> $filter,

        'sort'=> $sort,

        'q'=> $keyword

];

$url = MEILISEARCH_HOST."/indexes/".INDEX_NAME."/search";

$res = dealCurl(url: $url, type:'POST', data:$data);

print_r($res);


参考:

Meilisearch 快速入门(Windows 环境):https://learnku.com/articles/65055

PHP使用Meilisearch实现全文检索:https://blog.csdn.net/jason19905/article/details/115055666

searchableAttributes,filterableAttributes,faceting之间的区别和关系:https://cloud.tencent.com/developer/ask/sof/107336258

PHP系列 | Meilisearch 轻量搜索引擎入门介绍:https://blog.51cto.com/tinywan/5142126

只袄早~~~
感谢你的支持,我会继续努力!
扫码打赏,感谢您的支持!

文明上网理性发言!

  • 还没有评论,沙发等你来抢