designmynight / laravel-elasticsearch
使用 Elasticsearch 作为 Laravel 的数据库,以检索 Eloquent 模型并执行聚合操作。
Requires
- php: ^8.0
- ext-json: *
- elasticsearch/elasticsearch: ^7.17
- laravel/framework: ^9.0
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ^7.2.0
- phpunit/phpunit: ^9.5.19
- dev-master
- 7.0.x-dev
- v7.0.1
- v7.0
- v6.3.2
- v6.3.1
- v6.3.0
- v6.2.4
- v6.2.3
- 6.2.2
- 6.2.1
- v6.2.0
- 6.1.4
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- v6.0.2
- v6.0.1
- v6.0.0
- 5.0.x-dev
- v5.0.0
- v4.5
- 4.4.x-dev
- v4.3.11
- 4.3.10.x-dev
- v4.3.10
- 4.3.9.x-dev
- v4.3.9
- 4.3.8.x-dev
- v4.3.8
- v4.3.7
- v4.3.6
- v4.3.5
- 4.3.4
- v4.3.3
- v4.3.2
- v4.3.1
- 4.3.0.x-dev
- v4.3.0
- 4.2.4.x-dev
- v4.2.4
- v4.2.3
- v4.2.2.x-dev
- v4.2.2
- v4.2.1
- 4.2.0.x-dev
- v4.2.0
- 4.1.0.x-dev
- v4.1.0
- 4.0.x-dev
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- 3.0.x-dev
- v3.0.15
- v3.0.14
- v3.0.13
- v3.0.12
- v3.0.11
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.10
- v2.1.9
- v2.1.8
- v2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.1.1
- 1.1.0
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.2.1-beta
- v0.2.0-beta
- v0.1.0-beta
- v0.1.0-alpha.3
- v0.1.0-alpha.2
- v0.1.0-alpha1
- v0.1.0-alpha
- dev-shifting_tests
- dev-fix_apply_grammar
- dev-extended_methods_no_types
- dev-patching_docs
- dev-laravel_9_support
- dev-4.3.0-change-array-check
- dev-4.2-merge
- dev-dependabot/composer/league/flysystem-1.1.4
- dev-alasdairmackenzie-patch-1
- dev-fixClauseOptions
- dev-Bump-laravel-version-reqs
- dev-v4.1.0-branch
- dev-2.1-fixes
This package is auto-updated.
Last update: 2024-09-05 18:38:52 UTC
README
使用 Elasticsearch 作为 Laravel 的数据库,以检索 Eloquent 模型并执行聚合操作。
使用 Eloquent 建立查询,就像您习惯的那样,并获取模型实例,还有一些额外的功能
- 使用
query
、filter
和postFilter
查询类型 - 执行地理搜索
- 构建和执行数据上的复杂聚合
- 使用 Elasticsearch 滚动 API 获取大量结果
版本
根据您的 Elasticsearch 版本,您可以使用以下版本的此包
设置
将 Elasticsearch 连接配置添加到 database.php
'elasticsearch' => [
'driver' => 'elasticsearch',
'host' => 'localhost',
'port' => 9200,
'database' => 'your_es_index',
'username' => 'optional_es_username',
'password' => 'optional_es_username',
'suffix' => 'optional_es_index_suffix',
]
创建或更新您的 base Model.php 类以覆盖 newEloquentBuilder()
和 newBaseQueryBuilder()
/** * Create a new Eloquent builder for the model. * * @param \Illuminate\Database\Query\Builder $query * @return \Illuminate\Database\Eloquent\Builder|static */ public function newEloquentBuilder($query) { switch ($this->getConnectionName()) { case static::getElasticsearchConnectionName(): $builder = new ElasticsearchEloquentBuilder($query); break; default: $builder = new Illuminate\Database\Eloquent\Builder($query); } return $builder; } /** * Get a new query builder instance for the connection. * * @return \Illuminate\Database\Query\Builder */ protected function newBaseQueryBuilder() { $connection = $this->getConnection(); switch ($this->getConnectionName()) { case static::getElasticsearchConnectionName(): $builder = new ElasticsearchQueryBuilder($connection, $connection->getQueryGrammar(), $connection->getPostProcessor()); break; default: $builder = new Illuminate\Database\Query\Builder($connection, $connection->getPostProcessor()); } return $builder; }
搜索
现在您可以开始对数据进行搜索了。查询将寻找与您模型所在的数据库表同名的 Elasticsearch 索引。
$documents = MyModel::newElasticsearchQuery() ->where('date', '>', Carbon\Carbon::now()) ->get();
聚合
可以通过类似于直接查询 Elasticsearch 的方法向查询中添加聚合,使用嵌套函数而不是嵌套数组。`aggregation()` 方法接受三个或四个参数
- 用于聚合的键
- 聚合类型,如 'filter' 或 'terms'
- (可选) 提供聚合选项的回调或数组
- (可选) 允许您提供进一步子聚合的函数
$myQuery = MyModel::newElasticsearchQuery() ->aggregation( // The key of the aggregation (used in the Elasticsearch response) 'my_filter_aggregation', // The type of the aggregation 'filter', // A callback providing options to the aggregation, in this case adding filter criteria to a query builder function ($query) { $query->where('lost', '!=', true); $query->where('concierge', true); }, // A callback specifying a sub-aggregation function ($builder) { // A simpler aggregation, counting terms in the 'status' field $builder->aggregation('my_terms_aggregation', 'terms', ['field' => 'status']); } ); $results = $myQuery->get(); $aggregations = $myQuery->getQuery()->getAggregationResults();
地理查询
您可以通过距离地理点的距离来过滤搜索结果,或者仅包括给定边界内的结果,传递您用于直接查询 Elasticsearch 的格式相同的参数。
$withinDistance = MyModel::newElasticsearchQuery() ->whereGeoDistance('geo_field', [$lat, $lon], $distance); $withinBounds= MyModel::newElasticsearchQuery() ->whereGeoBoundsIn('geo_field', $boundingBox);
滚动 API
您可以使用滚动搜索检索大量结果。而不是返回 Collection,您将得到一个 PHP 生成器 函数,您可以遍历它,其中每个值都是来自 Elasticsearch 的单个结果的模型。
$documents = MyModel::newElasticsearchQuery() ->limit(100000) ->usingScroll() ->get(); // $documents is a Generator foreach ($documents as $document){ echo $document->id; }
控制台
此包附带以下命令,用作实用程序或作为您的部署过程的一部分。
映射和别名
在执行 migrate:mappings
期间创建新索引时,该命令将自动根据迁移名称创建一个别名(通过删除日期字符串)。例如,迁移 2018_08_03_095804_users.json
将创建别名 users
。
在第一次迁移中,migrate:mappings
命令还将别名切换到最新的索引映射。上述操作仅发生在别名不存在时。
未来的迁移将需要您使用 --swap
选项。