PHPackages                             cakephp/elastic-search - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. cakephp/elastic-search

ActiveCakephp-plugin[Framework](/categories/framework)

cakephp/elastic-search
======================

An Elastic Search datasource and data mapper for CakePHP

5.0.0(7mo ago)86766.6k—5.7%537MITPHPCI passing

Since Jun 27Pushed 2mo ago29 watchersCompare

[ Source](https://github.com/cakephp/elastic-search)[ Packagist](https://packagist.org/packages/cakephp/elastic-search)[ Docs](https://github.com/cakephp/elastic-search)[ RSS](/packages/cakephp-elastic-search/feed)WikiDiscussions 5.x Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (42)Used By (7)

Elasticsearch Datasource for CakePHP
====================================

[](#elasticsearch-datasource-for-cakephp)

[![Build Status](https://github.com/cakephp/elastic-search/actions/workflows/ci.yml/badge.svg?branch=5.x)](https://github.com/cakephp/elastic-search/actions/workflows/ci.yml/badge.svg?branch=5.x)[![Latest Stable Version](https://camo.githubusercontent.com/52a96d9575cad1f6dc127312aa5dd3e6bc78817ea534e4b8a2d3c304b88afe24/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f63616b657068702f656c61737469632d7365617263683f736f72743d73656d766572267374796c653d666c61742d737175617265)](https://packagist.org/packages/cakephp/elastic-search)[![Total Downloads](https://camo.githubusercontent.com/495b02e63795ce65fa62d639fb86c59bab2abf446b88a29e30721c89e04980b9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63616b657068702f656c61737469632d7365617263683f7374796c653d666c61742d737175617265)](https://packagist.org/packages/cakephp/elastic-search/stats)[![codecov](https://camo.githubusercontent.com/95ba7a87e2a94e8017b3b54638c8a88f20d95b1acd21dbd2bff45ab29f893282/68747470733a2f2f636f6465636f762e696f2f67682f63616b657068702f656c61737469632d7365617263682f6272616e63682f352e782f67726170682f62616467652e7376673f746f6b656e3d47335463673131364f58)](https://app.codecov.io/gh/cakephp/elastic-search/tree/5.x)[![PHPStan](https://camo.githubusercontent.com/fff00cebb924e124a7335e6bd8ca8f8cf38869463c1654eff45d0939f1f21c57/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230382d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://phpstan.org/)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)

Use [Elastic Search](https://www.elastic.co/) as an alternative ORM backend in CakePHP 5.2+.

You can [find the documentation for the plugin in the Cake Book](https://book.cakephp.org/elasticsearch).

Installing Elasticsearch via composer
-------------------------------------

[](#installing-elasticsearch-via-composer)

You can install Elasticsearch into your project using [composer](https://getcomposer.org). For existing applications you can run:

```
composer require cakephp/elastic-search:^5.0
```

### Versions Table

[](#versions-table)

Cake\\ElasticSearchCakePHPElasticSearchElastica[1.x](https://github.com/cakephp/elastic-search/tree/1.0)3.0 - 3.52.x - 5.x5.x - 6.x[2.x](https://github.com/cakephp/elastic-search/tree/2.x)3.6+6.x6.x[&gt;3, &lt;3.4.0](https://github.com/cakephp/elastic-search/tree/3.3.0)4.0+6.x6.x[&gt;=3.4.0](https://github.com/cakephp/elastic-search/tree/3.x)4.0+7.x7.x[4.x](https://github.com/cakephp/elastic-search/tree/4.x)5.0+7.x7.x[5.x](https://github.com/cakephp/elastic-search/tree/5.x)5.2+9.x9.xYou are seeing the 5.x version.

Connecting the Plugin to your Application
-----------------------------------------

[](#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
---------------------

[](#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
----------------------

[](#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
---------------

[](#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*](https://www.elastic.co/guide/en/elasticsearch/reference/master/removal-of-types.html), 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
-------------

[](#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](https://phpunit.de/manual/current/en/installation.html), you can run the tests for CakePHP by doing the following:

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

###  Health Score

64

—

FairBetter than 99% of packages

Maintenance77

Regular maintenance activity

Popularity55

Moderate usage in the ecosystem

Community39

Small or concentrated contributor base

Maturity75

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~97 days

Recently: every ~229 days

Total

41

Last Release

64d ago

Major Versions

2.0.2 → 3.3.02021-06-21

1.0.x-dev → 2.x-dev2023-01-10

2.x-dev → 4.0.02023-09-05

3.x-dev → 4.x-dev2025-05-01

4.x-dev → 5.0.02025-10-17

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/23666?v=4)[CakePHP](/maintainers/cakephp)[@cakephp](https://github.com/cakephp)

---

Top Contributors

[![markstory](https://avatars.githubusercontent.com/u/24086?v=4)](https://github.com/markstory "markstory (291 commits)")[![lorenzo](https://avatars.githubusercontent.com/u/37621?v=4)](https://github.com/lorenzo "lorenzo (149 commits)")[![josbeir](https://avatars.githubusercontent.com/u/26058?v=4)](https://github.com/josbeir "josbeir (90 commits)")[![othercorey](https://avatars.githubusercontent.com/u/24221186?v=4)](https://github.com/othercorey "othercorey (38 commits)")[![bcrowe](https://avatars.githubusercontent.com/u/752603?v=4)](https://github.com/bcrowe "bcrowe (27 commits)")[![ADmad](https://avatars.githubusercontent.com/u/142658?v=4)](https://github.com/ADmad "ADmad (20 commits)")[![andrii-pukhalevych](https://avatars.githubusercontent.com/u/3767063?v=4)](https://github.com/andrii-pukhalevych "andrii-pukhalevych (16 commits)")[![lilHermit](https://avatars.githubusercontent.com/u/299424?v=4)](https://github.com/lilHermit "lilHermit (15 commits)")[![gaetansnl](https://avatars.githubusercontent.com/u/8348807?v=4)](https://github.com/gaetansnl "gaetansnl (14 commits)")[![LordSimal](https://avatars.githubusercontent.com/u/9105243?v=4)](https://github.com/LordSimal "LordSimal (11 commits)")[![damianoporta](https://avatars.githubusercontent.com/u/7131996?v=4)](https://github.com/damianoporta "damianoporta (11 commits)")[![dakota](https://avatars.githubusercontent.com/u/83255?v=4)](https://github.com/dakota "dakota (7 commits)")[![Arhell](https://avatars.githubusercontent.com/u/26163841?v=4)](https://github.com/Arhell "Arhell (6 commits)")[![cschomburg](https://avatars.githubusercontent.com/u/54525?v=4)](https://github.com/cschomburg "cschomburg (5 commits)")[![jippi](https://avatars.githubusercontent.com/u/22841?v=4)](https://github.com/jippi "jippi (5 commits)")[![HavokInspiration](https://avatars.githubusercontent.com/u/5243386?v=4)](https://github.com/HavokInspiration "HavokInspiration (4 commits)")[![burzum](https://avatars.githubusercontent.com/u/162789?v=4)](https://github.com/burzum "burzum (4 commits)")[![Iandenh](https://avatars.githubusercontent.com/u/2911923?v=4)](https://github.com/Iandenh "Iandenh (3 commits)")[![CauanCabral](https://avatars.githubusercontent.com/u/83092?v=4)](https://github.com/CauanCabral "CauanCabral (3 commits)")[![ndm2](https://avatars.githubusercontent.com/u/5031606?v=4)](https://github.com/ndm2 "ndm2 (3 commits)")

---

Tags

cakephpcakephp-pluginelasticsearchphpelasticsearchcakephp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/cakephp-elastic-search/health.svg)

```
[![Health](https://phpackages.com/badges/cakephp-elastic-search/health.svg)](https://phpackages.com/packages/cakephp-elastic-search)
```

###  Alternatives

[cakephp/debug_kit

CakePHP Debug Kit

86514.0M138](/packages/cakephp-debug-kit)[cakephp/bake

Bake plugin for CakePHP

11211.2M158](/packages/cakephp-bake)[friendsofcake/bootstrap-ui

Bootstrap front-end framework support for CakePHP

3492.1M32](/packages/friendsofcake-bootstrap-ui)[cakephp/localized

CakePHP Localized Plugin

218595.6k5](/packages/cakephp-localized)[cakephp/acl

Acl Plugin for CakePHP framework

109553.9k15](/packages/cakephp-acl)[cakephp/queue

Queue plugin for CakePHP

36257.9k12](/packages/cakephp-queue)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
