承接 needbrainz/scout-typesense-aggregator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

needbrainz/scout-typesense-aggregator

最新稳定版本:1.0.1

Composer 安装命令:

composer require needbrainz/scout-typesense-aggregator

包简介

Laravel Scout Aggregator for Typesense

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel Scout Aggregator for Typesense extends Laravel Scout with support for aggregated search across multiple models using Typesense. Based on Algolia's Scout Extended package, it adapts and extends the aggregator functionality for the open-source Typesense engine.

Installation

You can install the package via composer:

composer require needbrainz/scout-typesense-aggregator

Usage

To create a new aggregator, you can use the make:aggregator command:

php artisan make:typesense-aggregator MyAggregator
<?php

namespace App\Search;

use NeedBrainz\TypesenseAggregator\TypesenseAggregator;

class MyAggregator extends TypesenseAggregator
{
    /**
     * The names of the models that should be aggregated.
     *
     * @var string[]
     */
    protected $models = [
        App\Models\MyModel::class,
        App\Models\MyOtherModel::class,
    ];
}

Then configure the aggregator settings in your config/scout.php file:

'typesense' => [
    'model-settings' => [
        MyAggregator::class => [
           'collection-schema' => [
                'fields' => [
                    [
                        'name' => 'id',
                        'type' => 'string',
                    ],
                    // Your aggregated fields
                    [
                        'name' => 'name',
                        'type' => 'string',
                    ],
                    [
                        'name' => 'description',
                        'type' => 'string',
                    ],
                    [
                        'name' => 'created_at',
                        'type' => 'int64',
                    ],
                ],
                'default_sorting_field' => 'created_at',
           ],
           'search-parameters' => [
                'query_by' => 'name,description',
           ]
        ],
    ]

Register your Aggregator in an appropriate service provider, such as App\Providers\AppServiceProvider:

use App\Search\MyAggregator;

public function boot(): void
{
    MyAggregator::bootSearchable();
}

By default the Aggregator will merge the models toSearchableArrray and set the custom id coming from the Aggregator getScoutKey($model) which will create an id in the format ModelClassName::ModelScoutKey. This format can then be reverted with the extractScoutKey($key) for models retrieve. So if you change the syntax, don't forget to override the getScoutKey and extractScoutKey methods in your Aggregator class.

If the structure of the models is different, you can override the toSearchableArray method in your Aggregator class to customize the merging of the models.

public function toSearchableArray(): array
{
    $array = [
        'id' => (string )$this->getScoutKey(),
        'created_at' => $this->model->created_at->timestamp,
        // default empty values
        'name' => '',
        'description' => '',
    ];

    // Customize the merging of the models here
    if ($this->model instanceof MyModel) {
        $array['name'] = (string) $this->model->name;
        $array['description'] = (string) $this->model->description;
    } elseif ($this->model instanceof MyOtherModel) {
        $array['name'] = (string) $this->model->title;
        $array['description'] = (string) $this->model->summary;
    }

    return $array;
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 6
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-07