own3d/laravel-scout-elasticsearch

此包已弃用且不再维护。未建议替代包。

使用 ElasticSearch 和 Laravel Scout 在多个模型中进行搜索

5.0.3 2023-02-22 22:37 UTC

README

Scout ElasticSearch Import progress report

Build Status Code quality Coverage Total Downloads Latest Version License

为支持 PHP8,请使用 php8 分支

为 Laravel Framework < 6.0.0,请使用 3.x 分支

该包为将 ElasticSearch 集成到您的 Laravel 应用程序提供了完美的起点。它经过精心设计,以简化在 Laravel 框架 中使用 ElasticSearch。

它建立在最新的 Laravel Scout 版本之上,这是官方的 Laravel 搜索包。使用此包,您可以充分利用 Laravel Scout 的所有出色功能,同时利用 ElasticSearch 的完整搜索体验。

如果您需要任何帮助,Stack Overflow 是首选和推荐的支持提问方式。

💕 特点

如果您喜欢这个包,别忘了⭐它。🙏

⚠️ 要求

  • PHP 版本 >= 8.0
  • Laravel 框架版本 >= 8.0.0
Elasticsearch 版本 ElasticsearchDSL 版本
>= 7.0 >= 3.0.0
>= 6.0, < 7.0 < 3.0.0

🚀 安装

使用 composer 安装包

composer require matchish/laravel-scout-elasticsearch

设置环境变量

SCOUT_DRIVER=Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine

该包使用官方包中的 \ElasticSearch\Client,但不会尝试配置它,因此请随意在您的应用程序服务提供者中配置。但如果您现在不想这样做,您可以使用包中的 Matchish\ElasticSearchServiceProvider
注册提供者,添加到 config/app.php

'providers' => [
    // Other Service Providers

    \Matchish\ScoutElasticSearch\ElasticSearchServiceProvider::class
],

设置 ELASTICSEARCH_HOST 环境变量

ELASTICSEARCH_HOST=host:port

并发布 elasticsearch 的配置示例
php artisan vendor:publish --tag config

💡 使用

注意:本包为Laravel Scout添加功能,因此我们建议您首先阅读Scout文档。Scout文档可以在Laravel网站上找到。

索引设置和映射

在创建索引时定义映射非常重要——不合适的初步定义和映射可能导致搜索结果错误。

要定义索引的映射或设置,请使用正确的值设置配置。

例如,如果方法searchableAs返回字符串products

映射的配置键应为
elasticsearch.indices.mappings.products
或者您可以使用配置键elasticsearch.indices.mappings.default指定默认映射

同样,您也可以定义设置

对于索引products,它将是
elasticsearch.indices.settings.products

对于默认设置
elasticsearch.indices.settings.default

预加载

为了加快导入速度,您可以在导入时使用全局范围预加载关系。

您应该在服务提供者中配置ImportSourceFactory(在register方法中)

use Matchish\ScoutElasticSearch\Searchable\ImportSourceFactory;
...
public function register(): void
{
$this->app->bind(ImportSourceFactory::class, MyImportSourceFactory::class);

以下是一个MyImportSourceFactory的示例

namespace Matchish\ScoutElasticSearch\Searchable;

final class MyImportSourceFactory implements ImportSourceFactory
{
    public static function from(string $className): ImportSource
    {
        //Add all required scopes
        return new DefaultImportSource($className, [new WithCommentsScope()]);
    }
}

class WithCommentsScope implements Scope {

    /**
     * Apply the scope to a given Eloquent query builder.
     *
     * @param \Illuminate\Database\Eloquent\Builder $builder
     * @param \Illuminate\Database\Eloquent\Model $model
     * @return void
     */
    public function apply(Builder $builder, Model $model)
    {
        $builder->with('comments');
    }
}

零停机重新导入

在生产环境中工作,为了在重新导入数据时保持现有的搜索体验,您也可以使用scout:import Artisan命令

php artisan scout:import

该命令创建一个新的临时索引,将所有模型导入到其中,然后切换到该索引并删除旧索引。

搜索

为了与原始scout包完全兼容,本包不添加新方法。
那么我们如何构建复杂的查询呢?有两种方法。
默认情况下,当您将查询传递给search方法时,引擎构建一个query_string查询,因此您可以构建如下查询

Product::search('title:this OR description:this) AND (title:that OR description:that')`

如果不足以满足您的需求,您可以将回调传递给查询构建器

$results = Product::search('zonga', function($client, $body) {

    $minPriceAggregation = new MinAggregation('min_price');
    $minPriceAggregation->setField('price');
    
    $maxPriceAggregation = new MaxAggregation('max_price');
    $maxPriceAggregation->setField('price');
    
    $brandTermAggregation = new TermsAggregation('brand');
    $brandTermAggregation->setField('brand');

    $body->addAggregation($minPriceAggregation);
    $body->addAggregation($brandTermAggregation);
    
    return $client->search(['index' => 'products', 'body' => $body->toArray()]);
})->raw();

$client是来自elasticsearch/elasticsearch包的\ElasticSearch\Client对象
$body是来自ongr/elasticsearch-dsl包的ONGR\ElasticsearchDSL\Search

在多个模型之间搜索

您可以使用MixedSearch类来实现,只需将逗号分隔的索引名称传递给within方法。

MixedSearch::search('title:Barcelona or to:Barcelona')
    within(implode(',', [
        (new Ticket())->searchableAs(),
        (new Book())->searchableAs(),
    ]))
->get();

在这个例子中,您将获得包含TicketBook模型的集合,其中票的到达城市或书籍标题为Barcelona

处理结果

通常您的响应不是模型集合,而是聚合或带有高亮显示的模型等。在这种情况下,您需要实现自己的HitsIteratorAggregate实现,并将其绑定到您的服务提供者

以下是一个例子

🆓 许可证

Scout ElasticSearch是一个开源软件,遵循MIT许可证