PHPackages                             dkullmann/cakephp-elastic-search-datasource - 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. [Database &amp; ORM](/categories/database)
4. /
5. dkullmann/cakephp-elastic-search-datasource

AbandonedArchivedCakephp-plugin[Database &amp; ORM](/categories/database)

dkullmann/cakephp-elastic-search-datasource
===========================================

CakePHP Datasource and Utilities for connecting models to Elastic Search types

8879431[1 issues](https://github.com/dkullmann/CakePHP-Elastic-Search-DataSource/issues)PHP

Since Dec 22Pushed 9y ago3 watchersCompare

[ Source](https://github.com/dkullmann/CakePHP-Elastic-Search-DataSource)[ Packagist](https://packagist.org/packages/dkullmann/cakephp-elastic-search-datasource)[ RSS](/packages/dkullmann-cakephp-elastic-search-datasource/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![Build Status](https://camo.githubusercontent.com/3dfc71e5f84069d495f2a85042eff267629e464ef5d5dfbbf9297dc231e2f34d/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a6f7365676f6e7a616c657a2f63616b657068702d656c61737469632d7365617263682d64617461736f757263652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/josegonzalez/cakephp-elastic-search-datasource)[![Coverage Status](https://camo.githubusercontent.com/5e07fea686d255a8d18b9fecbe740157f2b24bbe9ff9d34e29e42a4d88bbed28/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6a6f7365676f6e7a616c657a2f63616b657068702d656c61737469632d7365617263682d64617461736f757263652e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/josegonzalez/cakephp-elastic-search-datasource?branch=master)[![Total Downloads](https://camo.githubusercontent.com/7f0236d8053edf024ecf9e2c48da24089fbf27a4af08d2696c178b2cfd02add4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6f7365676f6e7a616c657a2f63616b657068702d656c61737469632d7365617263682d64617461736f757263652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/josegonzalez/cakephp-elastic-search-datasource)[![Latest Stable Version](https://camo.githubusercontent.com/c16d0813c62d7e1e2041577d047c573fb0688a436bbba26e5dbfb6f1333da9f4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6f7365676f6e7a616c657a2f63616b657068702d656c61737469632d7365617263682d64617461736f757263652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/josegonzalez/cakephp-elastic-search-datasource)[![Documentation Status](https://camo.githubusercontent.com/f21abb14e40dd7be2e611d22a423b239ca6bbded9ad31a6630e0420d4ee8d9c7/68747470733a2f2f72656164746865646f63732e6f72672f70726f6a656374732f63616b657068702d656c61737469632d7365617263682d64617461736f757263652f62616467652f3f76657273696f6e3d6c6174657374267374796c653d666c61742d737175617265)](https://readthedocs.org/projects/cakephp-elastic-search-datasource/?badge=latest)[![Gratipay](https://camo.githubusercontent.com/40a48c7b3fc02e9d39dd715fe09e404c4cb2e89288c6ace0773d2aab6913b46f/68747470733a2f2f696d672e736869656c64732e696f2f67726174697061792f6a6f7365676f6e7a616c657a2e7376673f7374796c653d666c61742d737175617265)](https://gratipay.com/~josegonzalez/)

CakePHP Elastic Plugin &amp; ElasticSearch "ElasticSource" Datasource
=====================================================================

[](#cakephp-elastic-plugin--elasticsearch-elasticsource-datasource)

Conveniently index and access records in ElasticSearch.

Background
----------

[](#background)

Seamlessly transition from MySQL or another SQL/DBO-backed data source into the amazing ElasticSearch NoSQL indexer. Conforms to CakePHP's ORM but also provides access to facets and queries.

Patches and issues welcome. Please include unit tests.

Requirements
------------

[](#requirements)

- PHP &gt;= 5.3
- CakePHP 2.x
- ElasticSearch ()
- A basic understanding of ElasticSearch.

Installation
------------

[](#installation)

*\[Manual\]*

- Download this:
- Unzip that download.
- Copy the resulting folder to app/Plugin
- Rename the folder you just copied to Elastic

*\[GIT Submodule\]*

In your app directory type:

```
git submodule add git://github.com/josegonzalez/cakephp-elastic-search-datasource.git Plugin/Elastic
git submodule update --init

```

*\[GIT Clone\]*

In your app directory type

```
git clone git://github.com/josegonzalez/cakephp-elastic-search-datasource.git Plugin/Elastic

```

### Enable plugin

[](#enable-plugin)

Enable the plugin your `app/Config/bootstrap.php` file:

```
CakePlugin::load('Elastic');

```

If you are already using `CakePlugin::loadAll();`, then this is not necessary.

Usage
-----

[](#usage)

### Setup ElasticSearch

[](#setup-elasticsearch)

Setup an instance of ElasticSearch to use if you don't have one already.

Open `Config/database.php` and add a datasource called `index`:

```
public $index = array(
	'datasource' => 'Elastic.ElasticSource',
	'index' => 'people',
	'port' => 9200
);

```

And create your model:

```
class Contact extends AppModel {

	public $useDbConfig = 'index';

}

```

This will store your `Contact` model in the `people` index as the type `contacts`. By default the ElasticSearch "type" is the same as the table name you would use for your model. If you'd like to change the ElasticSearch type then add the variable `useType` to your model:

```
class Contact extends AppModel {

	public $useDbConfig = 'index';

	public $useType = 'mytype';

}

```

### Map your model

[](#map-your-model)

Elastic Plugin comes with a shell to help you with managing indexes, creating mappings, and indexing records:

```
Davids-MacBook-Pro:app dkullmann$ Console/cake Elastic.elastic
ElasticSearch Plugin Console Commands. Map and index data

Usage:
cake elastic.elastic [subcommand] [-h] [-v] [-q]

Subcommands:

create_index  Create or alias an index
mapping       Map a model to ElasticSearch
index         Index a model into ElasticSearch
list_sources  Display output from listSources

To see help on a subcommand use `cake elastic.elastic [subcommand] --help`

Options:

--help, -h     Display this help.
--verbose, -v  Enable verbose output.
--quiet, -q    Enable quiet output.

```

To start, create your index

```
Console/cake Elastic.elastic create_index test

```

#### Case 1: Your model is already in the 'default' datasource

[](#case-1-your-model-is-already-in-the-default-datasource)

You can copy the schema with this command:

```
Console/cake Elastic.elastic mapping Contact

```

#### Case 2: Your model is in another datasource that responds to `describe`:

[](#case-2-your-model-is-in-another-datasource-that-responds-to-describe)

```
Console/cake Elastic.elastic mapping Contact -d

```

#### Case 3: Your model is not yet mapped

[](#case-3-your-model-is-not-yet-mapped)

You can add a method to your model called elasticMapping to generate the mapping.

Special ElasticSearch types such as geopoint and multi\_field are supported.

```
class Contact extends AppModel {

	public $useDbConfig = 'index';

	public $_mapping = array(
		'id' => array('type' => 'integer'),
		'name' => array('type' => 'string'),
		'number' => array('type' => 'string'),
		'special_type' => array(
			'type' => 'multi_field',
			'fields' => array(
				'not_analyzed' => array('type' => 'string', 'index' => 'not_analyzed'),
				'analyzed' => array('type' => 'string', 'index' => 'analyzed')
			)
		),
		'created' => array('type' => 'datetime'),
		'modified' => array('type' => 'datetime')
	);

	public function elasticMapping() {
		return $this->_mapping;
	}
}

```

### Index a few records

[](#index-a-few-records)

If you do not yet have data in MySQL and you are following along with this tutorial you should create your MySQL tables and data:

```
CREATE TABLE `contacts` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `number` varchar(255) DEFAULT NULL,
  `special_type` varchar(255) DEFAULT NULL,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

INSERT INTO `contacts` (`id`, `name`, `number`, `special_type`, `created`, `modified`)
VALUES
	(1, 'David', '555-888-1212', 'Multifield', '2012-07-19 20:31:29', '2012-07-19 20:31:29');

```

Otherwise simple start indexing.

To start indexing records you can use this command:

```
Console/cake Elastic.elastic index Contact

```

The ElasticShell will add the IndexableBehavior to your model if it's not already added. To add it permanently add it in your model:

```
public $actsAs = array('Elastic.Indexable');

```

By default IndexableBehavior will declare your "modified" field as the field which tracks when each record was updated or created in order to synchronize it with ElasticSearch.

Test using this command

```
curl -XGET 'http://localhost:9200/people/contacts/1

```

The output should include:

```
{
  "_index" : "test",
  "_type" : "contacts",
  "_id" : "1",
  "_version" : 2,
  "exists" : true, "_source" : {"Contact":{"created":"2012-07-19 20:31:29","id":"1","modified":"2012-07-19 20:31:29","name":"David","number":"555-888-1212","special_type":"Multifield"}}
}

```

CRUD Operations
---------------

[](#crud-operations)

Because ElasticSource conforms to the CakePHP ORM CRUD operations are easy:

```
// Create
$record = array(
	'id' => 1,
	'name' => 'David',
);

$this->Model->create($data);
$this->Model->save();

// Read
$this->Model->findById(1);

// Update
$this->Model->save($data);

// Delete
$this->Model->delete(1);

```

Querying
--------

[](#querying)

Querying with Elastic Search using this plugin is very simple and you will find it is extremely similar to what you are already used to for querying relataional databases:

### Simple conditions

[](#simple-conditions)

Use the Model.field notation followed by an operator:

```
