PHP code example of michaeljennings / laralastica

1. Go to this page and download the library: Download michaeljennings/laralastica 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/ */

    

michaeljennings / laralastica example snippets


// This will return an instance of Michaeljennings\Laralastica\Eloquent\ResultCollection
Model::search(function(Builder $builder) {
  $builder->matchAll();
})->get();

/**
 * Create a new database notification collection instance.
 *
 * @param array $models
 * @return ResultCollection
 */
public function newCollection(array $models = [])
{
    return new Illuminate\Database\Eloquent\Collection($models);
}

"michaeljennings/laralastica": "^3.1"

'providers' => [

  'Michaeljennings\Laralastica\LaralasticaServiceProvider'

];

'aliases' => [

  'Laralastica' => 'Michaeljennings\Laralastica\Facades\Laralastica',

];

'driver' => 'elastica',

'index_prefix' => 'testing_',

'drivers' => [
  'elastica' => [
    'servers' => [
      [
        'host' => 'localhost',
        'port' => 9200
      ],
      [
        'url' => 'https://user:[email protected]/'
      ]
    ],
  ]
]

use Illuminate\Database\Eloquent\Model;
use Michaeljennings\Laralastica\Searchable;

class Foo extends Model
{
	use Searchable;
}

public function getIndex()
{
	return 'foo';
}

public function getSearchKey()
{
	return $this->key;
}

public function getIndexableAttributes()
{
	return [
		'foo' => $this->bar,
	];
}

protected $casts = [
	'price' => 'float',
	'active' => 'bool',
	'quantity' => 'int',
	'name' => 'string'
];

protected $casts = [
	'price' => 'float',
];

protected $laralasticaCasts = [
	'price' => 'string',
];

'indexable' => [
    'users' => \App\User::class,
]

'indexable' => [
    'users' => \App\User::class,
    'products' => \App\Product::class,
]

'indexable' => [
    'users' => \App\User::class,
    'products' => [
        'model' => \App\Product::class,
        'with' => [
            'category' => function($query) {
                $query->with('subcategories');
            }
        ]
    ]
]

Foo::search(function(Builder $query) {

	$query->matchAll();

}, 'foo', true)->get();

Foo::search(function(Builder $query) {

	$query->match('foo', 'bar')->must();
	$query->terms('bar', ['baz'])->should();
	$query->wildcard('baz', 'qux*', 1.0)->mustNot();

})->get();

Foo::where('foo', 'bar')->search(function(Builder $query) {

	$query->match('foo', 'bar');

})->orderBy('baz')->get();

use Illuminate\Database\Eloquent\Model;
use Michaeljennings\Laralastica\SearchSoftDeletes;

class Foo extends Model
{
	use SearchSoftDeletes;
}

class Foo
{
	public function __construct(Michaeljennings\Laralastica\Contracts\Laralastica $laralastica)
	{
		$this->laralastica = $laralastica;
	}

	public function foo()
	{
		$laralastica = Laralastica::search();
		$laralastica = laralastica();
	}
}

$laralastica->search('foo', function($query) {
	$query->matchAll();
});

$laralastica->search(['foo', 'bar], function($query) {
	$query->matchAll();
});

$laralastica->paginate('foo', function($query) {
	$query->matchAll();
}, 15);

$laralastica->search('foo', function($query) {
	$query->size(50);
});

$laralastica->search('foo', function($query) {
	$query->from(10);
});

$laralastica->search('foo', function($query) {
    // Sort by id in ascending order
    $query->sort('id');
    // Sort by id in descending order
    $query->sort('id', 'desc');
    // Sort by multiple fields
    $query->sort([
        '_score',
        'id' => 'desc'
    ]);
});

$laralastica->search('foo', function($query) {

    $query->bool(function($query) {
        $query->match('foo', 'bar');
    });

});

$laralastica->search('foo', function($query) {

    $query->common('baz', 'qux', 1.0);
    $query->common('baz', 'qux', 1.0, function($commonQuery) {
        $commonQuery->setMinimumShouldMatch(5);
    });

});

$laralastica->search('foo', function($query) {

    $query->exists('baz');

});

$laralastica->search('foo', function($query) {

    $query->fuzzy('baz', 'qux');
    $query->fuzzy('baz', 'qux', function($fuzzyQuery) {
        $fuzzyQuery->setFieldOption('fuzziness', 2);
    });

});

$laralastica->search('foo', function($query) {

    $query->match('baz', 'qux');
    $query->match('baz', 'qux', function($matchQuery) {
        $matchQuery->setFieldBoost('foo');
    });

});

$laralastica->search('foo', function($query) {

    $query->matchAll();

});

$laralastica->search('foo', function($query) {

    $query->queryString('testing');
    $query->queryString('testing', function($queryStringQuery) {
        $queryStringQuery->setDefaultField('foo');
    });

});

$laralastica->search('foo', function($query) {

    $query->queryString('foo', ['gte' => 1, 'lte' => 20]);
    $query->queryString('foo', ['gte' => 1, 'lte' => 20], function($rangeQuery) {
        $rangeQuery->setParam('foo', ['gte' => 1, 'lte' => 20, 'boost' => 1]);
    });

});

$laralastica->search('foo', function($query) {

    $query->regexp('foo', 'testing');

});

$laralastica->search('foo', function($query) {

    $query->term(['foo' => 'bar']);
    $query->term(['foo' => 'bar'], function($termQuery) {
        $termQuery->setTerm('baz', 'qux', 2.0);
    });

});

$laralastica->search('foo', function($query) {

    $query->terms('foo', ['bar', 'baz']);
    $query->terms('foo', ['bar', 'baz'], function($query) {
        $query->setMinimumMatch(5);
    });

});

$laralastica->search('foo', function($query) {

    $query->wildcard('foo', 'bar');

});

$laralastica->search('foo', function($query) {
    $query->matchAll()
          ->filter(function($query) {
            $query->exists('due_date');
          });
});

$laralastica->paginate('foo', function($query) {

    $query->matchAll();

}, 15);

$laralastica->search('foo', function($query) {

    $match = new \Elastica\Query\Match();

    $query->query($match);

});

$results = $laralastica->search('foo', function($query) { $query->matchAll() });

$results->totalHits();

$results = $laralastica->search('foo', function($query) { $query->matchAll() });

$results->maxScore();

$results = $laralastica->search('foo', function($query) { $query->matchAll() });

$results->totalTime();

php artisan laralastica:index

php artisan laralastica:index --queue