1. Go to this page and download the library: Download rennokki/elasticscout 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/ */
// Get all indexes
ElasticScout::indices()->get(['index' => '*']);
namespace App\Indexes;
use Rennokki\ElasticScout\Index;
use Rennokki\ElasticScout\Migratable;
class PostIndex extends Index
{
use Migratable;
/**
* The settings applied to this index.
*
* @var array
*/
protected $settings = [
//
];
/**
* The mapping for this index.
*
* @var array
*/
protected $mapping = [
//
];
}
class PostIndex extends Index
{
protected $name = 'posts_index_2';
}
use Rennokki\ElasticScout\Contracts\HasElasticScoutIndex;
use Rennokki\ElasticScout\Searchable;
class Post extends Model implements HasElasticScoutIndex
{
use Searchable;
}
use App\Indexes\PostIndex;
use Rennokki\ElasticScout\Contracts\HasElasticScoutIndex;
use Rennokki\ElasticScout\Index;
use Rennokki\ElasticScout\Searchable;
class Post extends Model implements HasElasticScoutIndex
{
use Searchable;
/**
* Get the index instance class for Elasticsearch.
*
* @return \Rennokki\ElasticScout\Index
*/
public function getElasticScoutIndex(): Index
{
return new PostIndex($this);
}
}
namespace App\SearchRules;
use Rennokki\ElasticScout\Builders\SearchQueryBuilder;
use Rennokki\ElasticScout\SearchRule;
class NameRule extends SearchRule
{
/**
* Initialize the rule.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Build the highlight payload.
*
* @param SearchQueryBuilder $builder
* @return array
*/
public function buildHighlightPayload(SearchQueryBuilder $builder)
{
return [
//
];
}
/**
* Build the query payload.
*
* @param SearchQueryBuilder $builder
* @return array
*/
public function buildQueryPayload(SearchQueryBuilder $builder)
{
return [
//
];
}
}
class NameRule extends SearchRule
{
public function buildQueryPayload(SearchQueryBuilder $builder)
{
return [
'must' => [
'match' => [
// access the search phrase from the $builder
'name' => $builder->query,
],
],
];
}
}
use App\SearchRules\NameRule;
class Restaurant extends Model
{
/**
* Get the search rules for Elasticsearch.
*
* @return array
*/
public function getElasticScoutSearchRules(): array
{
return [
new NameRule,
];
}
}
use App\SearchRules\NameRule;
Restaurant::search('Dominos')
->addRule(new NameRule)
->get();
use App\SearchRules\NameRule;
use App\SearchRules\LocationRule;
Restaurant::search('Dominos')
->addRules([
new NameRule,
new LocationRule($lat, $lon),
])->get();
// The rule that will be aplied will be only LocationRule
Restaurant::search('Dominos')
->addRule(new NameRule)
->setRules([
new LocationRule($lat, $lon),
])->get();
class NameRule extends SearchRule
{
public function buildHighlightPayload(SearchQueryBuilder $builder)
{
return [
'fields' => [
'name' => [
'type' => 'plain',
],
],
];
}
public function buildQueryPayload(SearchQueryBuilder $builder)
{
return [
'should' => [
'match' => [
'name' => $builder->query,
],
],
];
}
}