An Elastic Search datasource and data mapper for CakePHP 3.0

1.0.0 2017-09-03 08:14 UTC


Build Status License

This is a pre-alpha version of an alternative ORM for CakePHP 3.0 using Elastic Search as its backend. It is currently under development and is only being used to test the interfaces exposed in CakePHP 3.0.

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 add the following to your composer.json file:

"require": {
    "cakephp/elastic-search": "^1.0"

And run php composer.phar update

Connecting the Plugin to your Application

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

// in config/bootstrap.php

// If you want the plugin to automatically configure the Elastic model provider
// and FormHelper do the following:
Plugin::load('Cake/ElasticSearch', ['bootstrap' => true]);

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',
            'host' => '',
            'port' => 9200,
            'index' => 'my_apps_index',

You can enable request logging by setting the log config option to true. By default, Elastica\Log will be used, which logs via error_log. 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 Type object

Type objects are the equivalent of ORM\Table instances in elastic search. You can use the TypeRegistry factory to get instances, much like TableRegistry:

use Cake\ElasticSearch\TypeRegistry;

$comments = TypeRegistry::get('Comments');

Delete by Query Plugin (ElasticSearch 1.x => 2.x)

To be able to delete records via criteria other than _id you need the ElasticSearch delete-by-query plugin.

NOTE: Without the plugin you will get at least 2 test failures

Running tests

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