Skip to content

Commit

Permalink
Add remaining skeleton to elasticsearch docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jun 27, 2015
1 parent ac5e241 commit 08c6438
Showing 1 changed file with 180 additions and 7 deletions.
187 changes: 180 additions & 7 deletions en/elasticsearch.rst
Expand Up @@ -135,11 +135,184 @@ The `FilterBuilder source
<https://github.com/cakephp/elastic-search/blob/master/src/FilterBuilder.php>`_
has the complete list of methods with examples for many commonly used methods.

Validating Data & Using Application Rules
=========================================

Like the ORM, the ElasticSearch plugin lets you validate data when marshalling
documents. Validating request data, and applying application rules works the
same as it does with the relational ORM. See the :ref:`validating-request-data`
and :ref:`application-rules` sections for more information.

.. comment::
Other sections to add:
* Validation and Application Rules
* Saving Documents
* Deleting Documents
* Embedded Documents
* Using the TypeRegistry
* Test fixtures
Need information on nested validators.

Saving New Documents
====================

When you're ready to index some data into elasticsearch, you'll first need to
convert your data into a ``Document`` that can be indexed::

$article = $this->Articles->newEntity($data);
if ($this->Articles->save($article)) {
// Document was indexed
}

When marshalling a document, you can specify which embedded documents you wish
to marshall using the ``associated`` key::

$article = $this->Articles->newEntity($data, ['associated' => ['Comments']]);

Saving a document will trigger the following events:

* ``Model.beforeSave`` - Fired before the document is saved. By stopping this
event you can prevent the save operation from happening.
* ``Model.buildRules`` - Fired when the rules checker is built for the first
time.
* ``Model.afterSave`` - Fired after the document is saved.

.. note::
There are no events for embedded documents, as the parent document and all
of its embedded documents are saved as one operation.


Updating Existing Documents
===========================

When you need to re-index data, you can patch existing entities and re-save
them::

$query = $this->Articles->find()->where(['user.name' => 'jill']);
foreach ($query as $doc) {
$doc->set($newProperties);
$this->Articles->save($doc);
}

Deleting Documents
==================

After retrieving a document you can delete it::

$doc = $this->Articles->get($id);
$this->Articles->delete($doc);

You can also delete documents matching specific conditions::

$this->Articles->deleteAll(['user.name' => 'bob']);

Embedding Documents
===================

By defining embedded documents, you can attach entity classes to specific
property paths in your documents. This allows you to provide custom behavior to
the documents within a parent document. For example, you may want the comments
embedded in an article to have specific application specific methods. You can
use ``embedOne`` and ``embedMany`` to define embedded documents::

// in src/Model/Type/ArticlesType.php
namespace App\Model\Type;

use Cake\ElasticSearch\Type;

class ArticlesType extends Type
{
public function initialize()
{
$this->embedOne('User');
$this->embedMany('Comments', [
'entityClass' => 'MyComment'
]);
}
}

The above would create two embedded documents on the ``Article`` document. The
``User`` embed will convert the ``user`` property to instances of
``App\\Model\\Document\\User``. To get the Comments embed to use a class name
that does not match the property name, we can use the ``entityClass`` option to
configure a custom class name.

Once we've setup our embedded documents, the results of ``find()`` and ``get()``
will return objects with the correct embedded document classes::

$article = $this->Articles->get($id);
// Instance of App\Model\Document\User
$article->user;

// Array of App\Model\Document\Comment instances
$article->comments;

Getting Type Instances
======================

Like the ORM, the elasticsearch plugin provides a factory/registry for getting
Type instances::

use Cake\ElasticSearch\TypeRegistry;

$articles = TypeRegistry::get('Articles');

Flushing the Registry
---------------------

During test cases you may want to flush the registry. Doing so is often useful
when you are using mock objects, or modifying a type's dependencies::

TypeRegistry::flush();

Test Fixtures
=============

The elasticsearch plugin provides seamless test suite integration. Just like
database fixtures, you can create test fixtures for elasticsearch. We could
define a test fixture for our Articles type with the following::

namespace App\Test\Fixture;

use Cake\ElasticSearch\TestSuite\TestFixture;

/**
* Articles fixture
*/
class ArticlesFixture extends TestFixture
{
/**
* The table/type for this fixture.
*
* @var string
*/
public $table = 'articles';

/**
* The mapping data.
*
* @var array
*/
public $schema = [
'id' => ['type' => 'integer'],
'user' => [
'type' => 'nested',
'properties' => [
'username' => ['type' => 'string'],
]
]
'title' => ['type' => 'string'],
'body' => ['type' => 'string'],
];

public $records = [
[
'user' => [
'username' => 'billy'
],
'title' => 'First Post',
'body' => 'Some content'
]
];
}

The ``schema`` property uses the `native elasticsearch mapping format
<https://www.elastic.co/guide/en/elasticsearch/reference/1.5/mapping.html>`_.
You can safely omit the type name and top level ``properties`` key. Once your
fixtures are created you can use them in your test cases by including them in
your test's ``fixtures`` properties::

public $fixtures = ['app.articles'];

0 comments on commit 08c6438

Please sign in to comment.