Skip to main content

Elastic Document Models

This kind of model was created to use Index as table. For using this feature, you must extend your class to \Baka\Elasticsearch\Objects\Documents.

This class have these methods:

  • initialize
  • setData
  • structure

Method initialize#

Following the same patterns as Phalcon models, on initialize, we set up the relationships (nested fields) this document has in order for the query params to work.

public function initialize(): void
$this->addRelation('authors', ['alias' => 'authors', 'elasticAlias' => 'author', 'elasticIndex' => 1]);

Method structure#

public function structure() : array
return [
'id' => $this->integer,
'name' => $this->text,
'authors' => [
'id' => $this->integer,
'name' => $this->text,
'address' => [
'street' => $this->text,
'state' => $this->text,
'city' => $this->text

Gists Example#

Create Index#

For creating the index, run the following command

php cli/cli.php elastic createDocuments "Gewaer\ElasticDocuments\Books" 3 1000
  • The first param is the class for model
  • The second is the max depth
  • The third represents max fields in that index

Insert Documents#

In order to insert data to Elastic, you will just need to initialize your Elastic document object, and pass the Phalcon model to the insert data function.

$books = \Gewaer\Models\Books::find('is_deleted = 0');
foreach($books as $book){
$document = new \Gewaer\ElasticModel\Books();

If you want to insert data from CLI, use the following command

php cli/cli.php elastic createDocumentsElastic "Gewaer\Models\Books" "Gewaer\ElasticModel\Books" 3 1000