archerzdip/hyperf-elasticsearch
Composer 安装命令:
composer require archerzdip/hyperf-elasticsearch
包简介
The Elasticsearch Driver for Hyperf Scout
README 文档
README
根据 babenkoivan/scout-elasticsearch-driver 改造的基于Hyperf框架的ElasticSearch组件。
Requirements
- PHP version >=7.2.0
- Hyperf Framework version >2.0
- Elasticsearch version >=7
Installation
composer require archerzdip/hyperf-elasticsearch
Configuration
发布配置文件
php bin/hyperf.php vendor:publish archerzdip/hyperf-elasticsearch
- 配置文件在config/autoload/scout_elastic.php, 配置参数如下: *
| Option | Description |
|---|---|
| driver | 默认defalut |
| soft_delete | 是否软删除 |
| prefix | 前缀 |
| client.host | ElasticSearch client, default localhost:9200.如果存在密码: user:password@host:port |
| max_connections | 最大连接数,默认500 |
| timeout | 超时时间,默认10s |
| indexer | 索引方式,目前支持single和bulk |
| chunk.searchable | 批量处理的搜索块数量 |
| chuck.unsearchable | 批量处理的搜索块数量 |
| update_mapping | 是否自动更新,默认 true |
| document_refresh | This option controls when updated documents appear in the search results. Can be set to 'true', 'false', 'wait_for' or null. More details about this option you can find here. By default set to null. |
Index Configurator
所以配置器类用于设置ElasticSearch的索引,可使用以下方式创建新的索引配置器:
php bin/hyperf.php make:index-configurator MyIndexConfigurator
默认目录为App\ElasticIndexConfigurator\MyIndexConfigurator
<?php namespace App\ElasticIndexConfigurator; use ArcherZdip\ScoutElastic\IndexConfigurator; use ArcherZdip\ScoutElastic\Traits\Migratable; class MyIndexConfigurator extends IndexConfigurator { use Migratable; protected $name = 'my_index'; /** * @var array */ protected $settings = [ 'analysis' => [ 'analyzer' => [ 'es_std' => [ 'type' => 'standard', 'stopwords' => '_spanish_' ] ] ] ]; }
More about index settings you can find in the index management section of Elasticsearch documentation.
Searchable Model
php bin/hyperf.php make:searchable-model MyModel
Usage
Basic search usage example:
// 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();
If you only need the number of matches for a query, use the count method:
App\MyModel::search('phone') ->count();
If you need to load relations, use the with method:
App\MyModel::search('phone') ->with('makers') ->get();
In addition to standard functionality the package offers you the possibility to filter data in Elasticsearch without specifying a query string:
App\MyModel::search('*') ->where('id', 1) ->get();
Also you can override model search rules:
App\MyModel::search('Brazil') ->rule(App\MySearchRule::class) ->get();
And use variety of where conditions:
App\MyModel::search('*') ->whereRegexp('name.raw', 'A.+') ->where('age', '>=', 30) ->whereExists('unemployed') ->get();
And filter out results with a score less than min_score:
App\MyModel::search('sales') ->minScore(1.0) ->get();
And add more complex sorting (geo_distance eg.)
$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;
At last, if you want to send a custom request, you can use the searchRaw method:
App\MyModel::searchRaw([ 'query' => [ 'bool' => [ 'must' => [ 'match' => [ '_all' => 'Brazil' ] ] ] ] ]);
## 直接返回ES数据 App\MyModel::search("test")->raw();
This query will return raw response.
Console Commands
| Command | Arguments | Description |
|---|---|---|
| make:index-configurator | name - The name of the class |
Creates a new Elasticsearch index configurator. |
| make:searchable-model | name - The name of the class |
Creates a new searchable model. |
| make:search-rule | name - The name of the class |
Creates a new search rule. |
| elastic:create-index | index-configurator - The index configurator class |
Creates an Elasticsearch index. |
| elastic:update-index | index-configurator - The index configurator class |
Updates settings and mappings of an Elasticsearch index. |
| elastic:drop-index | index-configurator - The index configurator class |
Drops an Elasticsearch index. |
| elastic:update-mapping | model - The model class |
Updates a model mapping. |
| elastic:migrate-model | model - The model class, target-index - The index name to migrate |
Migrates model to another index. |
For detailed description and all available options run php bin/hyperf.php help [command] in the command line.
Search rules
php bin/hyperf.php make:search-rule MySearchRule
默认目录为App\ElasticSearchRule\MySearchRule
License
MIT
archerzdip/hyperf-elasticsearch 适用场景与选型建议
archerzdip/hyperf-elasticsearch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 422 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「search」 「elasticsearch」 「engine」 「driver」 「elastic」 「scout」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 archerzdip/hyperf-elasticsearch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 archerzdip/hyperf-elasticsearch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 archerzdip/hyperf-elasticsearch 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel package to retrieve data from Google Search Console
Indexed Search Autocomplete - Extends the TYPO3 Core Extension Indexed_Search searchform with an autocomplete feature.
A SilverStripe module to optimise the Meta, crawling, indexing, and sharing of your website content (forked from Cyber-Duck/Silverstripe-SEO)
Abstraction Layer to index and search entities
simple api library.
A Processor for Markup written in PHP. Allows extraction of Markup into a data structure, orchestrated nested manipulation of said structure, and output as (optimized) Markup.
统计信息
- 总下载量: 422
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-17