Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/taq/torm-elastic
ElasticSearch integration for TORM
https://github.com/taq/torm-elastic
elasticsearch orm php
Last synced: 19 days ago
JSON representation
ElasticSearch integration for TORM
- Host: GitHub
- URL: https://github.com/taq/torm-elastic
- Owner: taq
- License: gpl-2.0
- Created: 2015-07-09T19:55:11.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-07-11T14:46:10.000Z (over 9 years ago)
- Last Synced: 2024-04-24T13:27:53.311Z (8 months ago)
- Topics: elasticsearch, orm, php
- Language: PHP
- Size: 168 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TORM-elasticsearch
This is a trait to insert ElasticSearch funcionality on
[TORM](https://github.com/taq/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(). 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()`
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 `_test`.If we want to avoid updating a document on the test enviroment, we can use
```
TORM\ElasticSearchConfigs::avoidOnTests(true);
```