PHP Classes

PHP Torm ORM for ElasticSearch: Adapter for Torm ORM to access Elastic

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 181 All time: 8,707 This week: 455Up
Version License PHP version Categories
torm-elastic 0.0.2GNU General Publi...5.5PHP 5, Databases, Searching, Design P..., T...
Description 

Author

This package is an adapter for Torm ORM to access ElasticSearch.

It provides a PHP trait that provides functions to adapt Object-Relational Mapping operations to store and retrieve objects from an ElasticSearch database.

Applications can extend the Torm model classes and use the Torm Elastic Search trait to make the object persistence and retrieval functions be done with an ElasticSearch instance.

Innovation Award
PHP Programming Innovation award nominee
July 2015
Number 7
ElasticSearch is popular search engine software that can index and search documents in almost real time.

Torm is a PHP object-relational mapping package based on the the ActiveRecord design pattern.

This package provides a trait that can be used by objects mapped to databases with Torm, so the objects can be indexed and searched using ElasticSearch.

Manuel Lemos
Picture of Eustaquio Rangel de Oliveira Jr.
  Performance   Level  
Name: Eustaquio Rangel de ... is available for providing paid consulting. Contact Eustaquio Rangel de ... .
Classes: 6 packages by
Country: Brazil Brazil
Age: 53
All time rank: 27520 in Brazil Brazil
Week rank: 180 Up12 in Brazil Brazil Up
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Documentation

TORM-elasticsearch

This is a trait to insert ElasticSearch funcionality on TORM objects.

Installation

Change (or create, if needed) your Composer file to include it:

{
    "require": {
        "taq/torm-elastic": ">=0"
    }
}

Usage

Just open your model and insert the trait, like:

class User extends TORM\Model {
    use TORM\ElasticSearch;
}
User::setElasticSearchIndex("myapp");

and, after every object saving, it will be send for ElasticSearch indexing, using some rules:

  • Need to insert the trait using `use TORM\ElasticSearch` on the model;
  • Need to specify the app name using setElasticSearchIndex(<name>). This will be the ElasticSearch index.
  • After inserting the trait, a new `afterInitialize` method will be added on the model. If the model already has a `afterInitialize` method, the `TORM\ElasticSearch` `afterInitialize` method must be called explicity on its end. This is because of the way PHP traits works.
  • If not specified, all the model attributes will be indexed. To define just
    some key attributes, we can use the `setElasticSearchValues(<attributes>)`
    method, sending an array with the attributes, like:
    User::setElasticSearchValues(["name"]);
    
    then only the `name` attribute will be indexed.

Searching

Then we can search using something like:

$rtn = ElasticUser::elasticSearch("name", "john");
var_dump($rtn);

resulting in something like

array(2) {
  'id' =>
  string(1) "1"
  'name' =>
  string(12) "John Doe Jr."
}

Importing

When importing a new data collection, we can use the import method, like:

User::elasticImport();

Getting document count

User:;elasticCount();

Updating a document

We can explicity update a document using:

$obj->updateElasticSearch();

Deleting a document

We can explicity delete a document using:

$obj->deleteElasticSearch();

Disabling

We can disable updating documents using

TORM\ElasticSearchConfigs::disabled(true);

and enable again sending false on the same method.

Testing

If using a TORM test enviroment (defined setting the enviroment var TORM_ENV to test), the index name will be automatically changed to <index_name>_test.

If we want to avoid updating a document on the test enviroment, we can use

TORM\ElasticSearchConfigs::avoidOnTests(true);

  Files folder image Files (12)  
File Role Description
Files folder imagemodels (2 files)
Files folder imagesrc (2 files)
Files folder imagetest (2 files, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (12)  /  models  
File Role Description
  Plain text file elastic_user.php Class Class source
  Plain text file user.php Class Class source

  Files folder image Files (12)  /  src  
File Role Description
  Plain text file ElasticSearch.php Class Class source
  Plain text file ElasticSearchConfigs.php Class Class source

  Files folder image Files (12)  /  test  
File Role Description
Files folder imagefactories (3 files)
  Plain text file elasticTest.php Class Class source
  Accessible without login Plain text file run Data Auxiliary data

  Files folder image Files (12)  /  test  /  factories  
File Role Description
  Accessible without login Plain text file account.php Test Unit test script
  Accessible without login Plain text file ticket.php Test Unit test script
  Accessible without login Plain text file user.php Test Unit test script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:181
This week:0
All time:8,707
This week:455Up