anyitsolutions/elastic-adapter
Composer 安装命令:
composer require anyitsolutions/elastic-adapter
包简介
Adapter for official PHP Elasticsearch client
README 文档
README
Elastic Adapter is an adapter for official PHP Elasticsearch client. It's designed to simplify basic index and document operations.
Contents
Compatibility
The current version of Elastic Adapter has been tested with the following configuration:
- PHP 7.2-7.4
- Elasticsearch 7.x
Installation
The library can be installed via Composer:
composer require babenkoivan/elastic-adapter
Index Management
IndexManager can be used to manipulate indices. It uses Elasticsearch client as a dependency,
therefore you need to initiate the client before you create an IndexManager instance:
$client = \Elasticsearch\ClientBuilder::fromConfig([ 'hosts' => [ 'localhost:9200' ] ]); $indexManager = new \ElasticAdapter\Indices\IndexManager($client);
The manager provides a list of useful methods, which are listed below.
Create
Creates an index, either with the default settings and mapping:
$index = new \ElasticAdapter\Indices\Index('my_index'); $indexManager->create($index);
or configured accordingly to your needs:
$mapping = (new \ElasticAdapter\Indices\Mapping()) ->text('title', [ 'boost' => 2, ]) ->keyword('tag', [ 'null_value' => 'NULL' ]) ->geoPoint('location'); $settings = (new \ElasticAdapter\Indices\Settings()) ->index([ 'number_of_replicas' => 2, 'refresh_interval' => -1 ]); $index = new \ElasticAdapter\Indices\Index('my_index', $mapping, $settings); $indexManager->create($index);
Drop
Deletes an index:
$indexManager->drop('my_index');
Put Mapping
Updates an index mapping:
$mapping = (new \ElasticAdapter\Indices\Mapping()) ->text('title', [ 'boost' => 2, ]) ->keyword('tag', [ 'null_value' => 'NULL' ]) ->geoPoint('location'); $indexManager->putMapping('my_index', $mapping);
Put Settings
Updates an index settings:
$settings = (new \ElasticAdapter\Indices\Settings()) ->analysis([ 'analyzer' => [ 'content' => [ 'type' => 'custom', 'tokenizer' => 'whitespace' ] ] ]); $indexManager->putSettings('my_index', $settings);
Exists
Checks if an index exists:
$indexManager->exists('my_index');
Open
Opens an index:
$indexManager->open('my_index');
Close
Closes an index:
$indexManager->close('my_index');
Document Management
Similarly to IndexManager, the DocumentManager class also depends on Elasticsearch client:
$client = \Elasticsearch\ClientBuilder::fromConfig([ 'hosts' => [ 'localhost:9200' ] ]); $documentManager = new \ElasticAdapter\Documents\DocumentManager($client);
Index
Adds a document to an index:
$documents = [ new ElasticAdapter\Documents\Document('1', ['title' => 'foo']), new ElasticAdapter\Documents\Document('2', ['title' => 'bar']), ]; $documentManager->index('my_index', $documents);
There is also an option to refresh index immediately:
$documentManager->index('my_index', $documents, true);
Delete
Removes a document from index:
$documents = [ new ElasticAdapter\Documents\Document('1', ['title' => 'foo']), new ElasticAdapter\Documents\Document('2', ['title' => 'bar']), ]; $documentManager->delete('my_index', $documents);
If you want an index to be refreshed immediately pass true as the third argument:
$documentManager->delete('my_index', $documents, true);
You can also delete documents using query:
$documentManager->deleteByQuery('my_index', ['match_all' => new \stdClass()]);
Search
Finds documents in an index:
$request = new \ElasticAdapter\Search\SearchRequest([ 'match' => [ 'message' => 'test' ] ]); $request->setHighlight([ 'fields' => [ 'message' => [ 'type' => 'plain', 'fragment_size' => 15, 'number_of_fragments' => 3, 'fragmenter' => 'simple' ] ] ]); $request->setSuggest([ 'my_suggest' => [ 'text' => 'test', 'term' => [ 'field' => 'message' ] ] ]); $request->setSource(['message', 'post_date']); $request->setCollapse([ 'field' => 'user' ]); $request->setSort([ ['post_date' => ['order' => 'asc']], '_score' ]); $request->setFrom(0)->setSize(20); $response = $documentManager->search('my_index', $request); // total number of matched documents $total = $response->getHitsTotal(); // corresponding hits $hits = $response->getHits(); // document, highlight or raw representation of the hit foreach ($hits as $hit) { $document = $hit->getDocument(); $highlight = $hit->getHighlight(); $raw = $hit->getRaw(); } // suggestions $suggestions = $response->getSuggestions();
anyitsolutions/elastic-adapter 适用场景与选型建议
anyitsolutions/elastic-adapter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.46k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「client」 「adapter」 「elasticsearch」 「elastic」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 anyitsolutions/elastic-adapter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 anyitsolutions/elastic-adapter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 anyitsolutions/elastic-adapter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Adapter designed to add Framework Agnosticism for Caching within PHP Packages.
flysystem cache Adapter
Virtual Filesystem Storage Adapter for Laravel
A simple interface for composite Flysystem adapters
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
统计信息
- 总下载量: 9.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-02