VictorD11 / scout-elasticsearch-driver
Laravel Scout 的 Elasticsearch 驱动,支持 PHP 8 和 Laravel 10
Requires
- php: ^7.1|^8.0.2
- elasticsearch/elasticsearch: >=7.0 <=7.17.0
- laravel/scout: ^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^7.0
- dev-master
- 5.2
- v5.1
- 5.0.2
- 5.0.1
- v5.0.0
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.0
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0.x-dev
- v4.0.0
- v3.13.0
- v3.12.0.x-dev
- v3.12.0
- v3.11.0
- v3.10.0
- v3.9.1
- v3.9.0
- v3.8.2
- v3.8.1
- v3.8.0
- v3.7.1
- v3.7.0
- v3.6.1
- v3.6.0
- v3.5.0
- v3.4.1
- v3.4.0
- v3.3.2
- v3.3.1
- v3.3.0
- v3.2.1
- v3.2.0
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- v1.3.1
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dev
- dev-development
- dev-analysis-d09j2v
This package is auto-updated.
Last update: 2024-09-08 17:28:17 UTC
README
💥 推出一个针对 Laravel 的新 Elasticsearch 生态系统。 💥
此软件包为 Elasticsearch 中的数据搜索和过滤提供高级功能。查看其 功能!
💥 支持 PHP 8 和 Laravel 10 💥
内容
功能
- 一种简单的方法来 配置 和 创建 一个 Elasticsearch 索引。
- 为每个 模型 提供完全可配置的映射。
- 自动或在 Artisan 命令 的帮助下将新字段添加到现有映射中的可能性。
- 使用 搜索规则 或 原始搜索 的多种方式来实现您的搜索算法。
- 各种过滤器类型 来使搜索查询更具体。
- 从旧索引到新索引的零停机迁移。
- 批量索引,请参阅 配置部分。
要求
该软件包在以下配置中进行了测试
- PHP 版本 >=7.1.3, >=8.0.2
- Laravel 框架版本 >=5.8, >=6, >=7, >=8, >=9, >=10
- Elasticsearch 版本 >=7
安装
使用 composer 安装软件包
composer require victord11/scout-elasticsearch-driver
如果您正在使用 Laravel 版本 <= 5.4 或 禁用了包发现,请在 config/app.php
中添加以下提供者
'providers' => [ Laravel\Scout\ScoutServiceProvider::class, ScoutElastic\ScoutElasticServiceProvider::class, ]
配置
要配置软件包,您需要首先发布设置
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
php artisan vendor:publish --provider="ScoutElastic\ScoutElasticServiceProvider"
然后,在 config/scout.php
文件中将驱动设置设置为 elastic
,并在 config/scout_elastic.php
文件中配置驱动本身。可用选项包括
注意,如果您使用批量文档索引,您可能想更改块大小,您可以在 config/scout.php
文件中这样做。
索引配置器
使用以下 artisan 命令创建索引配置器类来设置 Elasticsearch 索引的设置。要创建新的索引配置器,请使用以下 artisan 命令
php artisan make:index-configurator MyIndexConfigurator
它将在您项目的 app
文件夹中创建 MyIndexConfigurator.php
文件。您可以根据以下示例指定索引名称和设置
<?php namespace App; use ScoutElastic\IndexConfigurator; class MyIndexConfigurator extends IndexConfigurator { // It's not obligatory to determine name. By default it'll be a snaked class name without `IndexConfigurator` part. protected $name = 'my_index'; // You can specify any settings you want, for example, analyzers. protected $settings = [ 'analysis' => [ 'analyzer' => [ 'es_std' => [ 'type' => 'standard', 'stopwords' => '_spanish_' ] ] ] ]; }
有关索引设置的其他信息,请参阅 Elasticsearch 文档的 索引管理部分。
要创建索引,只需运行 artisan 命令
php artisan elastic:create-index "App\MyIndexConfigurator"
注意,每个可搜索模型都需要自己的索引配置器。
在Elasticsearch 6.0.0或更高版本中创建的索引只能包含一个映射类型。在5.x版本中创建包含多个映射类型的索引将在Elasticsearch 6.x版本中继续按以前的方式工作。在Elasticsearch 7.0.0中将完全移除映射类型。
更多信息请访问这里。
可搜索模型
要创建一个具有在Elasticsearch索引中执行搜索请求能力的模型,请使用以下命令
php artisan make:searchable-model MyModel --index-configurator=MyIndexConfigurator
执行命令后,您会在您的app
文件夹中找到名为MyModel.php
的文件
<?php namespace App; use ScoutElastic\Searchable; use Illuminate\Database\Eloquent\Model; class MyModel extends Model { use Searchable; protected $indexConfigurator = MyIndexConfigurator::class; protected $searchRules = [ // ]; // Here you can specify a mapping for model fields protected $mapping = [ 'properties' => [ 'title' => [ 'type' => 'text', // Also you can configure multi-fields, more details you can find here https://elastic.ac.cn/guide/en/elasticsearch/reference/current/multi-fields.html 'fields' => [ 'raw' => [ 'type' => 'keyword', ] ] ], ] ]; }
每个可搜索模型代表一个Elasticsearch类型。默认情况下,类型名称与表名称相同,但您可以通过searchableAs
方法设置任何类型名称。您还可以通过toSearchableArray
方法指定将通过驱动程序索引的字段。关于这些选项的更多信息,您可以在scout官方文档中找到。
您可以在MyModel
类中设置的最后一个重要选项是$searchRules
属性。它允许您为模型设置不同的搜索算法。我们将在搜索规则部分中更详细地探讨它。
在您的模型中设置映射后,您可以更新Elasticsearch类型的映射
php artisan elastic:update-mapping "App\MyModel"
用法
一旦您创建了一个索引配置器、Elasticsearch索引本身和可搜索模型,您就可以开始使用了。现在您可以根据文档索引和搜索数据。
基本搜索使用示例
// set query string App\MyModel::search('phone') // specify columns to select ->select(['title', 'price']) // filter ->where('color', 'red') // sort ->orderBy('price', 'asc') // collapse by field ->collapse('brand') // set offset ->from(0) // set limit ->take(10) // get results ->get();
如果您只需要查询的匹配数量,请使用count
方法
App\MyModel::search('phone') ->count();
如果您需要加载关系,请使用with
方法
App\MyModel::search('phone') ->with('makers') ->get();
除了标准功能外,该包还为您提供了一种在Elasticsearch中过滤数据而不指定查询字符串的可能性
App\MyModel::search('*') ->where('id', 1) ->get();
您还可以覆盖模型的搜索规则
App\MyModel::search('Brazil') ->rule(App\MySearchRule::class) ->get();
您可以使用多种where
条件进行过滤
App\MyModel::search('*') ->whereRegexp('name.raw', 'A.+') ->where('age', '>=', 30) ->whereExists('unemployed') ->get();
并且可以过滤掉得分低于min_score的结果
App\MyModel::search('sales') ->minScore(1.0) ->get();
还可以添加更复杂的排序(例如geo_distance)
$model = App\MyModel::search('sales') ->orderRaw([ '_geo_distance' => [ 'coordinates' => [ 'lat' => 51.507351, 'lon' => -0.127758 ], 'order' => 'asc', 'unit' => 'm' ] ]) ->get(); // To retrieve sort result, use model `sortPayload` attribute: $model->sortPayload;
最后,如果您想发送自定义请求,可以使用searchRaw
方法
App\MyModel::searchRaw([ 'query' => [ 'bool' => [ 'must' => [ 'match' => [ '_all' => 'Brazil' ] ] ] ] ]);
此查询将返回原始响应。
控制台命令
以下列出了可用的Artisan命令
要获取详细描述和所有可用选项,请在命令行中运行php artisan help [command]
。
搜索规则
搜索规则是一个描述搜索查询如何执行的类。要创建搜索规则,请使用以下命令
php artisan make:search-rule MySearchRule
在文件app/MySearchRule.php
中,您将找到类定义
<?php namespace App; use ScoutElastic\SearchRule; class MySearch extends SearchRule { // This method returns an array, describes how to highlight the results. // If null is returned, no highlighting will be used. public function buildHighlightPayload() { return [ 'fields' => [ 'name' => [ 'type' => 'plain' ] ] ]; } // This method returns an array, that represents bool query. public function buildQueryPayload() { return [ 'must' => [ 'match' => [ 'name' => $this->builder->query ] ] ]; } }
有关bool查询的更多信息,请参阅这里,有关高亮的更多信息,请参阅这里。
默认搜索规则返回以下有效载荷
return [ 'must' => [ 'query_string' => [ 'query' => $this->builder->query ] ] ];
这意味着默认情况下,当您在模型上调用search
方法时,它会尝试在任何字段中查找查询字符串。
要确定模型的默认搜索规则,只需添加一个属性
<?php namespace App; use ScoutElastic\Searchable; use Illuminate\Database\Eloquent\Model; class MyModel extends Model { use Searchable; // You can set several rules for one model. In this case, the first not empty result will be returned. protected $searchRules = [ MySearchRule::class ]; }
您还可以在查询构建器中设置搜索规则
// You can set either a SearchRule class App\MyModel::search('Brazil') ->rule(App\MySearchRule::class) ->get(); // or a callable App\MyModel::search('Brazil') ->rule(function($builder) { return [ 'must' => [ 'match' => [ 'Country' => $builder->query ] ] ]; }) ->get();
要获取高亮,请使用模型的highlight
属性
// Let's say we highlight field `name` of `MyModel`. $model = App\MyModel::search('Brazil') ->rule(App\MySearchRule::class) ->first(); // Now you can get raw highlighted value: $model->highlight->name; // or string value: $model->highlight->nameAsString;
可用过滤器
您可以使用不同类型的过滤器
在大多数情况下,使用原始字段进行记录过滤更好,即不分析的字段。
零停机迁移
如您所知,您不能更改Elasticsearch中已创建字段的类型。在这种情况下,唯一的选择是创建一个新的索引并带有必要的映射,然后将模型导入到新的索引中。
迁移可能需要相当长的时间,因此为了避免在迁移过程中停机,驱动程序会从旧索引中读取并写入新索引。迁移完成后,它开始从新索引中读取并删除旧索引。这就是 artisan elastic:migrate-model
命令的工作方式。
在运行该命令之前,请确保您的索引配置器使用 ScoutElastic\Migratable
特性。如果不是,请添加该特性,并使用索引配置器类名作为参数运行 artisan elastic:update-index
命令。
php artisan elastic:update-index "App\MyIndexConfigurator"
准备好后,修改模型映射,并使用模型类作为第一个参数,所需的索引名称作为第二个参数运行 elastic:migrate-model
命令。
php artisan elastic:migrate-model "App\MyModel" my_index_v2
注意,如果您只需在映射中添加新字段,请使用 elastic:update-mapping
命令。
调试
有两种方法可以帮助您分析搜索查询的结果:
-
App\MyModel::search('Brazil') ->explain();
-
App\MyModel::search('Brazil') ->profile();
这两种方法都返回ES的原始数据。
此外,您可以通过调用 buildPayload
方法来获取将被发送到ES的查询有效负载。
App\MyModel::search('Brazil') ->buildPayload();
请注意,由于可能在一个查询中使用多个搜索规则,此方法返回一个有效负载集合。
替代方案
最近,我发布了一个新的Laravel Elasticsearch生态系统,它包括:
- Elastic Scout Driver - 为Laravel Scout提供的通用Elasticsearch驱动程序。如果您需要在Laravel应用程序中构建简单的搜索,它非常完美。
- Elastic Scout Driver Plus - Elastic Scout Driver的扩展。如果您想利用布尔查询、高亮显示等Elasticsearch功能,这是一个不错的选择。
- Elastic Migrations - 创建、删除或更新Elasticsearch索引模式并与其同事共享的简单方法。它具有与Laravel数据库迁移相当类似的界面。
如果您对其中任何一项感兴趣并想了解更多细节,请阅读 Laravel应用程序中Elasticsearch的终极指南。该文章对所提到的包进行了很好的概述,并提供了使用示例。
常见问题解答
- 为什么您创建了一个新包而不是新的
scout-elasticsearch-driver
版本? - 由于明显的原因,我不想创建另一个包含所有功能的包:没有关注点的分离,不兼容其他Scout驱动程序,难以测试和开发等。由于Elastic Scout Driver是一个通用驱动程序且没有实现所有scout-elasticsearch-driver
功能,因此将其称为新的scout-elasticsearch-driver
版本是不正确的。 - 这对 scout-elasticsearch-driver 有什么意义? - 目前,它由社区维护(感谢 @iget-esoares 和 @lucamichot 保持项目活跃 🎉)。我希望他们将继续为项目做出贡献,并在将来带来新的
scout-elasticsearch-driver
版本。