romanstruk/manticore-scout-engine

Laravel Manticore Scout Engine

6.1.0 2024-07-04 17:39 UTC

README

Release

"Buy Me A Coffee"

Manticore Engine for Laravel Scout

安装

通过 Composer

$ composer require romanstruk/manticore-scout-engine

配置

安装 Manticore Scout Engine 后,您应使用 vendor:publish Artisan 命令发布 Manticore 配置文件。此命令将 manticore.php 配置文件发布到您应用程序的配置目录中

php artisan vendor:publish --provider="RomanStruk\ManticoreScoutEngine\ManticoreServiceProvider"
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

配置搜索驱动

在 .env 文件中设置您的搜索驱动 manticore

SCOUT_DRIVER=manticore

连接到 manticore 有两种方式可供选择

  • http-client - \Manticoresearch\Client github
  • mysql-builder - \RomanStruk\ManticoreScoutEngine\Builder 使用 mysql 连接

在 .env 文件中设置您的引擎

MANTICORE_ENGINE=http-client

配置驱动连接

在 .env 文件中的 http-client

MANTICORE_HOST=127.0.0.1
MANTICORE_PORT=9308

在 .env 文件中的 mysql-builder

MANTICORE_MYSQL_HOST=127.0.0.1
MANTICORE_MYSQL_PORT=9306

配置模型迁移

要创建迁移,请在可搜索模型中指定所需的字段

public function scoutIndexMigration(): array
{
    return [
        'fields' => [
            'id' => ['type' => 'bigint'],
            'name' => ['type' => 'text'],
            'category' => ['type' => 'string stored indexed'],// string|text [stored|attribute] [indexed]
        ],
        'settings' => [
            'min_prefix_len' => '3',
            'min_infix_len' => '3',
            'prefix_fields' => 'name',
            'expand_keywords' => '1',
            //'engine' => 'columnar', // [default] row-wise - traditional storage available in Manticore Search out of the box; columnar - provided by Manticore Columnar Library
        ],
    ];
}

配置查询选项

max_matches - 服务器为每个索引保留在 RAM 中的最大匹配数量,可以返回给客户端。默认值是 1000。

对于分页查询,您可以在 manticore.php 配置文件中设置自动参数计算 max_matches

'paginate_max_matches' => 1000,

设置 null 以计算偏移量 + 限制

由于某些字符在查询字符串中用作操作符,因此应转义以避免查询错误或不需要的匹配条件。在 manticore.php 配置文件中设置 auto_escape_search_phrase

'auto_escape_search_phrase' => true,

设置 false 以禁用自动转义特殊符号 ! " $ ' ( ) - / < @ \ ^ | ~

查询的其他参数可以在模型中指定

public function scoutMetadata(): array
{
    return [
        'cutoff' => 0,
        'max_matches' => 1000,
    ];
}

配置 paginate_max_matchesscoutMetadata max_matches 选项具有更高的优先级

使用

Scout 的文档可以在 Laravel 网站上找到。

运行 artisan 命令以创建 Manticore 索引

php artisan manticore:index "App\Models\Product"

Manticore 允许您将 "whereRaw" 方法添加到您的搜索查询中。

use RomanStruk\ManticoreScoutEngine\Mysql\Builder;

$products = Product::search('Brand Name', function (Builder $builder) {
    return $builder
        ->whereAny('category_id', ['1', '2', '3'])
        ->where('column', '=', 'value')
//        ->whereIn('column', ['1', '2'])
//        ->whereNotIn('column', ['3', '4'])
//        ->whereAll('column', ['3', '4'])
//        ->whereNotAll('column', ['5', '6'])
//        ->whereAllMva('column', 'in', ['1', '2'])
//        ->whereAnyMva('column', 'not in', ['1', '2'])
        ->facet('category_id')
        ->inRandomOrder();
})->get();

Quorum 匹配操作符

Quorum 匹配操作符引入了一种模糊匹配。它将仅匹配通过给定单词阈值的所有文档。上面的示例 ("the world is a wonderful place"/3) 将匹配至少包含 3 个 6 个指定单词的所有文档。

use RomanStruk\ManticoreScoutEngine\Mysql\Builder;

$products = Product::search('the world is a wonderful place', function (Builder $builder) {
    return $builder->setQuorumMatchingOperator(3);
})->get();

邻近距离以单词为单位指定,并针对单词计数进行调整,适用于所有引号内的单词。例如,"cat dog mouse"~5 查询表示必须少于 8 个单词的跨度,其中包含所有 3 个单词。

use RomanStruk\ManticoreScoutEngine\Mysql\Builder;

$products = Product::search('cat dog mouse', function (Builder $builder) {
    return $builder->setProximitySearchOperator(5);
})->get();

自动补全

自动补全(或单词完成)是一种功能,其中应用程序预测用户正在输入的单词的其余部分。在网站上,它用于搜索框,当用户开始键入单词时,会出现一个下拉菜单,用户可以从列表中选择结束。

use RomanStruk\ManticoreScoutEngine\Mysql\Builder;

//[doc] My cat loves my dog. The cat (Felis catus) is a domestic species of small carnivorous mammal.

$autocomplete = Product::search('my*',function (Builder $builder) {
    return $builder->autocomplete(['"','^'], true); // "" ^ * allow full-text operators; stats - Show statistics of keywords, default is 0
})->raw();
// $autocomplete<array> "my", "my cat", "my dog"

拼写纠正

use RomanStruk\ManticoreScoutEngine\Mysql\Builder;

//[doc] Crossbody Bag with Tassel
//[doc] microfiber sheet set
//[doc] Pet Hair Remover Glove

$result = Product::search('bagg with tasel',function (Builder $builder) {
    return $builder->spellCorrection(true) // correct first word
})->raw();
// $result<array> 0 => ['suggest' => "bag"]

$result = Product::search('bagg with tasel',function (Builder $builder) {
    return $builder->spellCorrection() // correct last word
})->raw();
// $result<array> 0 => ['suggest' => "tassel"]

$result = Product::search('bagg with tasel',function (Builder $builder) {
    return $builder->spellCorrection(false, true) // correct last word and return sentence
})->raw();
// $result<array> 0 => ['suggest' => "bagg with tassel"]

高亮显示

高亮显示功能允许您从包含匹配关键字的文章中获取高亮显示的文本片段(称为片段)。

use RomanStruk\ManticoreScoutEngine\Mysql\Builder;

//[doc] My cat loves my dogs.

$highlight = Product::search('dogs',
    fn(Builder $builder) => $builder->highlight()->select(['id', 'name'])
)->raw();
// $highlight['hits']<array> [id => 1, name => 'My cat loves my dogs.', 'highlight' => 'My cat loves my <b>dogs</b>.']

use RomanStruk\ManticoreScoutEngine\Mysql\Builder;

//[doc] title => My cat loves my dogs. id => 1000

$highlight = Product::search('dogs',
    fn(Builder $builder) => $builder->highlight()->select(['id', 'title'])
)->get();
// $highlight->getHighlight()[1000] => 'My cat loves my <b>dogs</b>.'

Percolate 查询

要创建迁移,请在可搜索模型中指定所需的字段

public function scoutIndexMigration(): array
{
    return [
            'fields' => [
                'title' => ['type' => 'text'],
                'color' => ['type' => 'string'],
            ],
            'settings' => [
                'type' => 'pq'
            ],
    ];
}

public function toSearchableArray(): array
{
    return array_filter([
        'id' => $this->name,
        'query' => "@title {$this->title}",
        'filters' => $this->color ? "color='{$this->color}'" : null,
    ]);
}

Percolate 查询也称为持久查询、预期搜索、文档路由、反向搜索和逆搜索。 https://manual.manticoresearch.com/Searching/Percolate_query#Percolate-Query

use RomanStruk\ManticoreScoutEngine\Mysql\Builder;

$products = PercolateProduct::search(json_encode(['title' =>'Beautiful shoes']),
    fn(Builder $builder) => $builder->percolateQuery(docs: true, docsJson: true)
)->get();

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

测试

$ composer test

贡献

请参阅贡献.md以获取详细信息和待办事项列表。

安全性

如果您发现任何与安全相关的问题,请通过电子邮件romanuch4@gmail.com联系,而不是使用问题跟踪器。

许可证

MIT。请参阅许可证文件以获取更多信息。