承接 cakephp/elastic-search 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cakephp/elastic-search

Composer 安装命令:

composer require cakephp/elastic-search

包简介

An Elastic Search datasource and data mapper for CakePHP

README 文档

README

Build Status Latest Stable Version Total Downloads codecov PHPStan Software License

Use Elastic Search as an alternative ORM backend in CakePHP 5.2+.

You can find the documentation for the plugin in the Cake Book.

Installing Elasticsearch via composer

You can install Elasticsearch into your project using composer. For existing applications you can run:

composer require cakephp/elastic-search:^5.0

Versions Table

Cake\ElasticSearch CakePHP ElasticSearch Elastica
1.x 3.0 - 3.5 2.x - 5.x 5.x - 6.x
2.x 3.6+ 6.x 6.x
>3, <3.4.0 4.0+ 6.x 6.x
>=3.4.0 4.0+ 7.x 7.x
4.x 5.0+ 7.x 7.x
5.x 5.2+ 9.x 9.x

You are seeing the 5.x version.

Connecting the Plugin to your Application

After installing, you should tell your application to load the plugin:

use Cake\ElasticSearch\Plugin as ElasticSearchPlugin;

class Application extends BaseApplication
{
    public function bootstrap()
    {
        $this->addPlugin(ElasticSearchPlugin::class);

        // If you want to disable to automatically configure the Elastic model provider
        // and FormHelper do the following:
        // $this->addPlugin(ElasticSearchPlugin::class, [ 'bootstrap' => false ]);
    }
}

Defining a connection

Before you can do any work with Elasticsearch models, you'll need to define a connection:

// in config/app.php
'Datasources' => [
    // other datasources
    'elastic' => [
        'className' => 'Cake\ElasticSearch\Datasource\Connection',
        'driver' => 'Cake\ElasticSearch\Datasource\Connection',
        'hosts' => ['127.0.0.1:9200']
    ],
]

As an alternative you could use a link format if you like to use enviroment variables for example.

// in config/app.php
'Datasources' => [
    // other datasources
    'elastic' => [
        'url' => env('ELASTIC_URL', null)
    ]
]

// and make sure the folowing env variable is available:
// ELASTIC_URL="Cake\ElasticSearch\Datasource\Connection://127.0.0.1:9200?driver=Cake\ElasticSearch\Datasource\Connection"

You can enable request logging by setting the log config option to true. By default the debug Log profile will be used. You can also define an elasticsearch log profile in Cake\Log\Log to customize where Elasticsearch query logs will go. Query logging is done at a 'debug' level.

Getting a Index object

Index objects are the equivalent of ORM\Table instances in elastic search. You can use the IndexLocatorAwareTrait to get instances in your classes:

use Cake\ElasticSearch\Datasource\IndexLocatorAwareTrait;

class MyClass
{
    use IndexLocatorAwareTrait;

    public function someMethod()
    {
        $comments = $this->fetchIndex('Comments');
    }
}

Alternatively, you can use the IndexLocator directly:

use Cake\ElasticSearch\Datasource\IndexLocator;

$locator = new IndexLocator();
$comments = $locator->get('Comments');

Note for upgrading users: The IndexRegistry class has been deprecated since version 3.4.3. If you're upgrading from an older version, replace IndexRegistry::get('Comments') with the IndexLocatorAwareTrait approach shown above or use IndexLocator directly.

Each Index object needs a correspondent Elasticsearch index, just like most of ORM\Table needs a database table.

In the above example, if you have defined a class as CommentsIndex and the IndexLocator can find it, the $comments will receive an initialized object with inner configurations of connection and index. But if you don't have that class, a default one will be initialized and the index name on Elasticsearch mapped to the class.

The Index class

You must create your own Index class to define the name of internal index for Elasticsearch, as well as to define the mapping type and define any entity properties you need like virtual properties. As you have to use only one mapping type for each index, you can use the same name for both (the default behavior when type is undefined is use singular version of index name). Index types were removed in ElasticSearch 7.

use Cake\ElasticSearch\Index;

class CommentsIndex extends Index
{
    /**
     * The name of index in Elasticsearch
     *
     * @return  string
     */
    public function getName()
    {
        return 'comments';
    }
}

Running tests

We recommend using the included docker-compose.yml for doing local development. The Dockerfile contains the development environment, and an Elasticsearch container will be downloaded and started on port 9200.

# Start elasticsearch
docker-compose up -d

Once inside the container you can install dependencies and run tests.

composer install
composer test

Warning: Please, be very carefully when running tests as the Fixture will create and drop Elasticsearch indexes for its internal structure. Don't run tests in production or development machines where you have important data into your Elasticsearch instance.

Assuming you have PHPUnit installed system wide using one of the methods stated here, you can run the tests for CakePHP by doing the following:

  1. Copy phpunit.xml.dist to phpunit.xml
  2. Run phpunit

cakephp/elastic-search 适用场景与选型建议

cakephp/elastic-search 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 854.38k 次下载、GitHub Stars 达 85, 最近一次更新时间为 2014 年 07 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cakephp」 「elasticsearch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cakephp/elastic-search 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cakephp/elastic-search 我们能提供哪些服务?
定制开发 / 二次开发

基于 cakephp/elastic-search 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 854.38k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 86
  • 点击次数: 16
  • 依赖项目数: 7
  • 推荐数: 4

GitHub 信息

  • Stars: 85
  • Watchers: 29
  • Forks: 52
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-07-12