PHP code example of brokerexchange / elasticbuilder

1. Go to this page and download the library: Download brokerexchange/elasticbuilder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brokerexchange / elasticbuilder example snippets



$query = Eb::boolean()
    ->must(Eb::term('category_id',1))
    ->filter(Eb::range('published_at',['lte' => Carbon::now()->toIso8601String(),'gte' => Carbon::now()->subDay(10)->toIso8601String()]));
var_dump($query);


$query = \Eb::multi_match(['title^3','summary^1','body','userName^2','categoryName^2','tag_string^1'],'lorim ipsum','and','cross_fields');
var_dump($query);


    use ElasticBuilder\ElasticBuilderTrait;

    /**
     * Class Article
     * @package App
     */
    class Article extends Model
    {
        use ElasticBuilderTrait;


    Article::bool()->filter(Eb::term('category_id','1);


    Article::dis_max()->query(Eb::match('body',$keywords));



    //trait example
    $results = $article->boolean()
        ->must(Eb::match('body','keyword search string'))
        ->aggregate(Eb::agg()->terms('categories','category_id'))->get(); //returns Elasticquent Results Object
            
    var_dump($results);
       
       
    //trait exaple with paging
    $results = $article->boolean()
       ->must(Eb::match('body','keyword search string'))
       ->aggregate(Eb::agg()->terms('categories','category_id'))->paginate(20); //returns Elasticquent Paginator Object
       
    var_dump($results);


if($this->request->has('search')){
    $search = $this->request->get('search');
    $match = \Eb::multi_match(['title^3','summary^1','body','userName^2','categoryName^2','tag_string^1'],$search,'and','cross_fields');
} else {
    $match = \Eb::match_all();
}
$this->must($match);


$filter = \Eb::range('published_at',['lte' => Carbon::now()->toIso8601String()]);
$this->filter($filter);


$query = Article::agg()
    ->terms('categories','category_id');
var_dump($query);