2017 © Pedro Peláez
 

package laravel-es

image

tamizh/laravel-es

  • Wednesday, March 21, 2018
  • by rtamizh
  • Repository
  • 1 Watchers
  • 0 Stars
  • 88 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 32 Versions
  • 1 % Grown

The README.md

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

  1. 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()
  2. boolMust, boolMustNot, boolShould, boolShouldNot - Boolean queries (Equal to AND and OR in mysql) Log::boolMust(function($query){ $query->match('field', 'text'); })->get()
  3. terms - Return the result that matches terms array Log::terms('field', array)->get()
  4. 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)

  5. 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')
    })
    
    1. scroll - Get the Iterator Object to scroll.
    $results = Log::match('field', 'text')->size(100)->scroll();
    foreach($results as $result){
        // logic goes here
    }
    
  6. size - Size of the result collection Log::match('field', 'text')->size(100)->get()
  7. highlight - To highlight the selected text Log::match('field', $text)->highlight('field')->get()
  8. first - To get first model Log::match('field', $text)->first()
  9. save - To save the current model to the ES $log = Log::match('field', $text)->first(); $log->count = $log->count + 1; $log->save();
  10. delete - To delete the current model from ES $log = Log::match('field', $text)->first(); $log->delete(); or Log::delete(1);
  11. 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();
    
  12. exists - exists condition functionality, (*18)

    $log = Log::exists('field')->get();
    
  13. index - index document in ES Log::index(['key' => 'value', 'key1' => 'value1'], id);
  14. update - update document in ES Log::update(['new_key' => 'new_value', 'old_key' => 'new_value'], id);
  15. removeKey - remove unwanted key from ES Log::removeKey('unwanted_key', id);
  16. script - script functionality Log::script("doc['errors'].value > 10")->get()
  17. count - get count of documents for current result, (*19)

    Log::script("doc['errors'].value > 10")->count()
    
  18. range - get the result set between a range, (*20)

    Log::range('error_rate', ['gte'=>20, 'lte'=>80])->get();
    
  19. rangeBetween - get the result set between a range (Simplified version of range) Log::rangeBetween('error_rate', 20, 30)->get(); 20 -> gte 30 -> lte
  20. 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()
  21. 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.
  22. 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
  23. 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')
  24. multiMatch - To search on multiple fields Log::multiMatch(['user','error'], 'query', 'phrase_prefix');
  25. fromType - To search on specific type, (*21)

    Log::match('field', 'text')->fromType('warinings')->get()
    
  26. find - Find the document by its ID, (*22)

    Log::find("AWAMbhhhXkO5BRWWZAw6");
    
  27. updateByQuery (experimental) - Update the documents using query (Not production ready), (*23)

    Log::match('field', 'test')->script("doc['update_field'].value = update_value")->updateByQuery()
    

Notes

  1. Following field names are reserved - _id, _type, _index, _highlight

TODO

  1. Write test cases (stable version)
  2. Adding More functionalities
  3. Indexing functionalities
  4. Mysql query format support

The Versions

21/03 2018

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

21/03 2018

v1.0.30

1.0.30.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

30/11 2017

v1.0.29

1.0.29.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

30/11 2017

v1.0.28

1.0.28.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

10/10 2017

v1.0.27

1.0.27.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

10/10 2017

v1.0.26

1.0.26.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

03/10 2017

v1.0.25

1.0.25.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

03/10 2017

v1.0.24

1.0.24.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

03/10 2017

v1.0.23

1.0.23.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

27/09 2017

v1.0.22

1.0.22.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

27/09 2017

v1.0.21

1.0.21.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

27/09 2017

v1.0.20

1.0.20.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

27/09 2017

v1.0.19

1.0.19.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

26/09 2017

v1.0.18

1.0.18.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

26/09 2017

v1.0.17

1.0.17.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

19/09 2017

v1.0.16

1.0.16.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

15/09 2017

v1.0.15

1.0.15.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

15/09 2017

v1.0.14

1.0.14.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

30/08 2017

v1.0.13

1.0.13.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

30/08 2017

v1.0.12

1.0.12.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

29/08 2017

v1.0.11

1.0.11.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

21/07 2017

v1.0.10

1.0.10.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

21/07 2017

v1.0.9

1.0.9.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

12/05 2017

v1.0.8

1.0.8.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

07/05 2017

v1.0.7

1.0.7.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

05/05 2017

v1.0.6

1.0.6.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

04/05 2017

v1.0.5

1.0.5.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

04/05 2017

v1.0.4

1.0.4.0

  Sources   Download

MIT

The Requires

 

The Development Requires

by tamizh

26/04 2017

v1.0.3

1.0.3.0

  Sources   Download

MIT

The Requires

 

by tamizh

25/04 2017

v1.0.2

1.0.2.0

  Sources   Download

MIT

The Requires

 

by tamizh

22/04 2017

1.0.1

1.0.1.0

  Sources   Download

MIT

The Requires

 

by tamizh

22/04 2017

1.0.0

1.0.0.0

  Sources   Download

MIT

The Requires

 

by tamizh