Elodex - Eloquent Indexing Library
, (*1)
Development branch: , (*2)
Elodex provides an easy way to implement synchronization of your Laravel Eloquent models with an Elasticsearch index., (*3)
Your Eloquent database will remain your main data source while you can use the full capacity of Elasticsearch for any index based search on your models., (*4)
Table of Contents
Requirements
Elodex requires Elasticsearch 2.0 or higher, PHP v5.6+ and Laravel 5.1+., (*5)
Besides the technical requirements you should have a profound knowledge of Eloquent and you should be familiar with the basic Elasticsearch terms and how Elasticsearch works in general., (*6)
Branching Model
This project uses the Gitflow branching model:
- the master branch contains the latest stable version
- the develop branch contains the latest unstable development version
- all stable versions are tagged using semantic versioning, (*7)
Installation
Elodex can be directly added to your project via Composer:, (*8)
$ composer require "elodex/elodex=~2.0"
Or you can manually add the required entry to your composer.json file in the require
section :, (*9)
"require": {
"elodex/elodex": "~2.0"
}
Laravel Integration
To integrate Elodex into your Laravel application you first need to add the IndexServiceProvider
to the list of service providers in the application configuration., (*10)
This can be done by editing the app.php
file in the config
folder. Search for the providers
section and add a new entry:, (*11)
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
*/
'providers' => [
...
\Elodex\IndexServiceProvider::class,
],
Configuration
Even though Elodex does ship with a default configuration which should work for standard Elasticsearch installations you usually want to specify your own settings.
You can do so by publishing the standard config file to your application:, (*12)
$ php artisan vendor:publish --provider="Elodex\IndexServiceProvider"
This will copy a standard config to config/elodex.php
. Make sure your Elasticsearch host configuration is correct and that you specify a default index name for your application which will be used for all your indexed Eloquent models by default., (*13)
/*
|--------------------------------------------------------------------------
| Default Index Name
|--------------------------------------------------------------------------
|
*/
'default_index' => 'my_app_index',
Extending your Eloquent Model Classes
There are two possibilities to add indexing capabilities to your Eloquent model classes., (*14)
1. Using the IndexedModel Trait
To add the basic indexing functionality to your existing Eloquent models you can include the IndexedModel
trait which automatically implements the needed Contracts\IndexedModel
interface for you., (*15)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Elodex\Contracts\IndexedModel as IndexedModelContract;
use Elodex\IndexedModel as IndexedModelTrait;
class Model extends BaseModel implements IndexedModelContract
{
use IndexedModelTrait;
Note that the trait implements the newCollection
method. You must make sure your Eloquent class doesn't overwrite this method, otherwise you're going to lose the convenient way to use indexing operations on the collections returned by model queries., (*16)
The IndexedModel
trait does three things for you:, (*17)
- it implements the
Contracts\IndexedModel
interface and thus makes the model capable of being added to an index repository.
- it adds a convenient way to access the default index manager and the default index repository for your model class.
- it adds the methods to interact with the index repository. This includes adding your model instances to the index repository, removing them and performing an index based search.
2. Deriving from the Elodex Model Class
Deriving from the abstract Elodex Model
class is a better approach than the trait if your existing model directly inherits from the Eloquent base model class.
It gives you the possibility to override and thus extend the existing methods added by the IndexedModel
trait without having to rewrite them completely., (*18)
A common use case would be if you want to change the document creation of your model., (*19)
Index Repositories
All indexed model documents are managed in a repository with the type IndexRepository
. Each model class has its own default index repository using its own type in the index., (*20)
This means you can't share an index repository with different model classes, trying to do so will result in exceptions during runtime., (*21)
The default index repository used for a class can be accessed through the getClassIndexRepository
static method., (*22)
$repository = User::getClassIndexRepository();
There's usually no need to access the index repository directly since the indexed model classes provide a more convenient method to manage the repository entries., (*23)
Elasticsearch Client
Elodex uses the official PHP low-level client for Elasticsearch.
There's usually no need to access this client directly but Elodex makes a client instance globally available in case you need to perform some custom or raw queries., (*24)
$client = app('elodex.client');
You may also use dependency injection with the ElasticsearchClientManager
class to get a client instance., (*25)
Caching
Can be enabled from the config file as needed.
Only when using:
php
->getModels()
, (*26)
Caching the eloquent query from the mysql, will serve the desired info from cache., (*27)
You have to create sync for the modules need caching and set a caching key prefix in the model you need, won't cache if this is not being set:
php
protected $elastic_cache_prefix = 'cache_prefix_';
, (*28)
Documentation
A detailed Elodex documentation can be found here., (*29)
Changelog
The changelog can be found here., (*30)
License
Elodex is an open source project licensed under the the MIT license.
Please see the License File for further information., (*31)