Package Data | |
---|---|
Maintainer Username: | pomirleanu |
Maintainer Contact: | pomirleanu.florentin@gmail.com (Pomirleanu Florentin Cristinel) |
Package Create Date: | 2016-08-25 |
Package Last Update: | 2017-07-17 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-12 15:01:19 |
Package Statistics | |
---|---|
Total Downloads: | 156 |
Monthly Downloads: | 2 |
Daily Downloads: | 0 |
Total Stars: | 4 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Elodex provides an easy way to implement synchronization of your Laravel Eloquent models with an Elasticsearch index.
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.
Elodex requires Elasticsearch 2.0 or higher, PHP v5.6+ and Laravel 5.1+.
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.
This project uses the Gitflow branching model:
Elodex can be directly added to your project via Composer:
$ composer require "elodex/elodex=~2.0"
Or you can manually add the required entry to your composer.json file in the require
section :
"require": {
"elodex/elodex": "~2.0"
}
To integrate Elodex into your Laravel application you first need to add the IndexServiceProvider
to the list of service providers in the application configuration.
This can be done by editing the app.php
file in the config
folder. Search for the providers
section and add a new entry:
/*
|--------------------------------------------------------------------------
| Autoloaded Service Providers
|--------------------------------------------------------------------------
|
*/
'providers' => [
...
\Elodex\IndexServiceProvider::class,
],
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:
$ 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.
/*
|--------------------------------------------------------------------------
| Default Index Name
|--------------------------------------------------------------------------
|
*/
'default_index' => 'my_app_index',
There are two possibilities to add indexing capabilities to your Eloquent model classes.
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.
<?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.
The IndexedModel
trait does three things for you:
Contracts\IndexedModel
interface and thus makes the model capable of being added to an index repository.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.
A common use case would be if you want to change the document creation of your model.
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.
This means you can't share an index repository with different model classes, trying to do so will result in exceptions during runtime.
The default index repository used for a class can be accessed through the getClassIndexRepository
static method.
$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.
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.
$client = app('elodex.client');
You may also use dependency injection with the ElasticsearchClientManager
class to get a client instance.
Can be enabled from the config file as needed. Only when using:
->getModels()
Caching the eloquent query from the mysql, will serve the desired info from cache.
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:
protected $elastic_cache_prefix = 'cache_prefix_';
A detailed Elodex documentation can be found here.
The changelog can be found here.
Elodex is an open source project licensed under the the MIT license. Please see the License File for further information.