Package Data | |
---|---|
Maintainer Username: | rtamizh |
Maintainer Contact: | tamizharasanmts@gmail.com (Tamizh) |
Package Create Date: | 2017-04-22 |
Package Last Update: | 2019-03-24 |
Home Page: | |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-17 03:01:44 |
Package Statistics | |
---|---|
Total Downloads: | 255 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 4 |
The basic orm package for elasticsearch (search and CRUD functionalities) - for elasticsearch 5.0 and lesser versions
composer require tamizh/laravel-es
or add the following line in composer.json line
"tamizh/laravel-es" : "dev-master" and run composer update
Add the service provider to your config/app.php file:
'providers' => [
//...
Tamizh\LaravelEs\ElasticsearchServiceProvider::class,
],
Add the facade to your config/app.php file:
'aliases' => [
//...
'Elasticsearch' => Tamizh\LaravelEs\Elasticsearch::class,
],
Publish config file using php artisan vendor:publish
Modifiy the config/elasticsearch.php.
Example
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.
class Log extends Elasticsearch
{
public $_index = 'logs*';
public $_type = 'log';
}
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)
Log::aggs(function($query){
$query->terms('field')->size(10)->minDocCount(10);
}, 'top_logs')
Multiple aggregation can be achieved by
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
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
sort - Sort the query result
Log::sort('field', 'desc')
or
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
$log = Log::queryString(function($query){
$query->query('tech*')
->fields(['errors', 'content']);
})->get();
in bool functions
Log::boolMust(function($query){
$query->queryString(function($query){
$query->query('tech*')
->fields(['errors', 'content']);
})
})->get();
exists - exists condition functionality
$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
Log::script("doc['errors'].value > 10")->count()
range - get the result set between a range
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
Log::match('field', 'text')->fromType('warinings')->get()
find - Find the document by its ID
Log::find("AWAMbhhhXkO5BRWWZAw6");
updateByQuery (experimental) - Update the documents using query (Not production ready)
Log::match('field', 'test')->script("doc['update_field'].value = update_value")->updateByQuery()