laravel-es
The basic orm package for elasticsearch (search and CRUD functionalities) - for elasticsearch 5.0 and lesser versions, (*1)
Installation
composer require tamizh/laravel-es
or add the following line in composer.json line, (*2)
"tamizh/laravel-es" : "dev-master" and run composer update
, (*3)
Configuration
Add the service provider to your config/app.php file:, (*4)
'providers' => [
//...
Tamizh\LaravelEs\ElasticsearchServiceProvider::class,
],
Add the facade to your config/app.php file:, (*5)
'aliases' => [
//...
'Elasticsearch' => Tamizh\LaravelEs\Elasticsearch::class,
],
Publish config file using php artisan vendor:publish
, (*6)
Modifiy the config/elasticsearch.php., (*7)
Example, (*8)
return [
'hosts' => [
env('ES_HOSTS', 'localhost:9200')
],
'log_path' => 'storage/logs/',
];
Instead of extends the Model class in your models extend the Elasticsearch to use the following functions., (*9)
class Log extends Elasticsearch
{
public $_index = 'logs*';
public $_type = 'log';
}
Available Functions
- match & match_phrase - Returns the results that matches the text
Log::match('field', 'text')->get()
Log::matchPhrase('field', 'hello world')->get()
Log::matchPhrasePrefix('field', 'hello')->get()
- boolMust, boolMustNot, boolShould, boolShouldNot - Boolean queries (Equal to AND and OR in mysql)
Log::boolMust(function($query){
$query->match('field', 'text');
})->get()
- terms - Return the result that matches terms array
Log::terms('field', array)->get()
-
aggs - Aggregate the result (sub aggregation not yet supported), (*10)
Log::aggs(function($query){
$query->terms('field')->size(10)->minDocCount(10);
}, 'top_logs')
Multiple aggregation can be achieved by, (*11)
Log::aggs(function($query){
$query->terms('field')->size(10)->minDocCount(10);
}, 'top_logs')
->aggs(function($query){
$query->sum('field')
});
Sub Aggregation can be achieved by, (*12)
Log::aggs(function($query){
$query->terms('field')
->aggs(function($sub_query){
$sub_query->sum('field');
});
}, 'top_logs')
available functions
a) terms
b) cardinality
c) max
d) min
e) sum_bucket
f) sum
d) date_histogram (with interval option [default - month])
e) avg, (*13)
-
sort - Sort the query result, (*14)
Log::sort('field', 'desc')
or, (*15)
Log::sort(function($query){
$query->script('return doc['error'].value + doc['success'].value')
})
- scroll - Get the Iterator Object to scroll.
$results = Log::match('field', 'text')->size(100)->scroll();
foreach($results as $result){
// logic goes here
}
- size - Size of the result collection
Log::match('field', 'text')->size(100)->get()
- highlight - To highlight the selected text
Log::match('field', $text)->highlight('field')->get()
- first - To get first model
Log::match('field', $text)->first()
- save - To save the current model to the ES
$log = Log::match('field', $text)->first();
$log->count = $log->count + 1;
$log->save();
- delete - To delete the current model from ES
$log = Log::match('field', $text)->first();
$log->delete();
or
Log::delete(1);
-
query_string - query string function in ES, (*16)
$log = Log::queryString(function($query){
$query->query('tech*')
->fields(['errors', 'content']);
})->get();
in bool functions, (*17)
Log::boolMust(function($query){
$query->queryString(function($query){
$query->query('tech*')
->fields(['errors', 'content']);
})
})->get();
-
exists - exists condition functionality, (*18)
$log = Log::exists('field')->get();
- index - index document in ES
Log::index(['key' => 'value', 'key1' => 'value1'], id);
- update - update document in ES
Log::update(['new_key' => 'new_value', 'old_key' => 'new_value'], id);
- removeKey - remove unwanted key from ES
Log::removeKey('unwanted_key', id);
- script - script functionality
Log::script("doc['errors'].value > 10")->get()
-
count - get count of documents for current result, (*19)
Log::script("doc['errors'].value > 10")->count()
-
range - get the result set between a range, (*20)
Log::range('error_rate', ['gte'=>20, 'lte'=>80])->get();
- rangeBetween - get the result set between a range (Simplified version of range)
Log::rangeBetween('error_rate', 20, 30)->get();
20 -> gte
30 -> lte
- from - pagination option in elasticsearch [MySQL offset]
[only applicable for 10000 result window, scroll is encouraged for bigger pagination]
Log::match('field', 'text')->size(100)->from(200)->get()
- paginate - pagination added [LengthAwarePaginator]
Using this api will give you the complete function access of paginator in laravel
Log::match('field', 'text')->paginate($perPage, $page)
Note - This can be used in the space of first 10000 result set for the current query. So if you intend to use the whole result set, then the better option is to use the scroller.
- search or searchRaw - To search by the raw array query
Log::search([
'body'=>[
"query"=>[
"match" => [
"field" => "text"
]
]
]
])->get()
Note - For query format check the official documentation elasticsearch PHP package
- topHits - to get the top hits of a aggregation
Log::aggs(function(){
$query->terms('user.id')-aggs(function(){
$query->topHits()->size(5); // get top 5 hits of the user
}, 'hits');
}, 'users')
- multiMatch - To search on multiple fields
Log::multiMatch(['user','error'], 'query', 'phrase_prefix');
-
fromType - To search on specific type, (*21)
Log::match('field', 'text')->fromType('warinings')->get()
-
find - Find the document by its ID, (*22)
Log::find("AWAMbhhhXkO5BRWWZAw6");
-
updateByQuery (experimental) - Update the documents using query (Not production ready), (*23)
Log::match('field', 'test')->script("doc['update_field'].value = update_value")->updateByQuery()
Notes
- Following field names are reserved - _id, _type, _index, _highlight
TODO
- Write test cases (stable version)
- Adding More functionalities
- Indexing functionalities
- Mysql query format support