mfonte / php82-search
受Lucene启发的PHP搜索引擎库
v1.0.5
2023-02-21 11:17 UTC
Requires
- php: >=7.3
- wamania/php-stemmer: ^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.4
- phpstan/phpstan: ^1.9
This package is auto-updated.
Last update: 2024-09-21 15:04:59 UTC
README
受Lucene启发的PHP搜索引擎库。
原始包:https://github.com/VincentFoulon80/php-search。
内部使用Wamania PHP Snowball Stemmer https://github.com/wamania/php-stemmer
此包被分叉以实现对PHP 8.2的兼容性。
安装
使用Composer安装此库
composer require mfonte/php82-search
它能做什么?
简而言之
- 索引和搜索文档(带分数、模糊搜索和分词)
- 支持12种语言的词干提取和停用词处理
- 维度分析
- 自动完成
- 连接搜索
请参阅功能页面获取更完整的列表
快速入门
搜索引擎附带了一个示例模式,允许您快速上手该库。
首先,您需要加载搜索引擎。
use MFonte\Search\Engine; $engine = new Engine();
您可以将一个数组作为类的构造函数参数,有关更多信息,请参阅维基配置页面。
通过构建引擎,将出现一些目录,位于您的索引文件旁边
- var/engine/index
- var/engine/documents
- var/engine/cache
(所有这些目录都可以通过配置数组进行更改)
首先,您需要给引擎一些要搜索的内容。我们将创建一些文档并要求引擎索引它们。
$doc = [ "id" => 1, "type" => "example-post", "title" => "this is my first blog post !", "content" => "I am very happy to post this first post in my blog !", "categories" => [ "party", "misc." ], "date" => "2018/01/01", "comments" => [ [ "author" => "vincent", "date" => "2018/01/01", "message" => "Hello world!" ], [ "author" => "someone", "date" => "2018/01/02", "message" => "Welcome !" ] ] ]; $engine->update($doc); $doc = [ "id" => 2, "type" => "example-post", "title" => "This is the second blog post", "content" => "a second one for fun", "date" => "2018/01/05", "categories" => [ "misc." ], "comments" => [ [ "author" => "someone", "date" => "2018/01/05", "message" => "Another one ?!" ] ] ]; $engine->update($doc);
注意:您也可以将这两个文档放在一个数组中,并使用updateMultiple()函数一次性索引多个文档。
现在您的文档已索引,您可以使用搜索功能并获取结果
$response = $engine->search('second post'); var_dump($response); $response = $engine->search('post'); var_dump($response);
有关此库的更多信息,例如使用更多高级功能,请访问此存储库的维基页面
管理面板
⚠️ 警告:此面板本身不处理任何安全性。如果您使用它,您需要防止公众访问!
管理面板是一个需要实例化然后运行的类。它不是一个可调用的文件,因此您需要通过常规php文件来调用它
<?php use MFonte\Search\AdminPanel; // include the composer autoload file, modify the path if needed require "vendor/autoload.php"; // securize your file access or directly here // if($_SERVER['REMOTE_ADDR'] != "127.0.0.1") exit('unauthorized'); // instantiate the panel and then run it $admin = new AdminPanel(); echo $admin->run();
AdminPanel的构造函数接受与您用于实例化引擎相同的配置数组,如果您已自定义模式,则需要将其传递(否则面板将无法正常工作)
更多信息请参阅管理面板手册
许可
此库采用MIT许可。请参阅完整许可