1. Go to this page and download the library: Download hilsonxhero/elasticvision 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/ */
use App\Models\User;
$results = User::search('fugiat')->must(new RegExp('username', 'k.*y','ALL',false))->get();
use App\Models\User;
$users = User::search()
->should(new Wildcard('username','ki*y'))
->get();
use App\Models\Article;
$articles = Article::search()
->must(new Matching('title','ipsum'))
->get();
use App\Models\User;
$users = User::search()
->should(new MatchPhrasePrefix('message','quick brown f'))
->get();
use App\Models\User;
$users = User::search()
->should(new MatchPhrase('message','this is a test'))
->get();
use App\Models\Product;
$products = Product::search()
->must(new Nested('category', new Term('category.id', 2)))
->get();
use App\Models\Product;
$search = Product::search("lorem");
// $feature_ids = array([4 => [1,2], 5 => [1,2]])
foreach (request()->feature_id as $key => $value) {
$query = new BoolQuery();
$query->must(new Term('features.feature_id', $key));
$query->must(new Terms('features.feature_value_id', $value));
$boolQuery->add('must', new Nested('features', $query));
}
$search->newCompound($boolQuery);
$products = $search->paginate(15);
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Hilsonxhero\ElasticVision\Application\Explored;
use Hilsonxhero\ElasticVision\Application\IndexSettings;
use Laravel\Scout\Searchable;
class Post extends Model implements Explored, IndexSettings
{
use HasFactory;
use Searchable;
protected $fillable = ['title', 'published'];
public function mappableAs(): array
{
return [
'id' => 'keyword',
'title' => 'text',
'published' => 'boolean',
'created_at' => 'date',
];
}
public function indexSettings(): array
{
return [
'analysis' => [
'analyzer' => [
'standard_lowercase' => [
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => ['lowercase'],
],
],
],
];
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Hilsonxhero\ElasticVision\Application\Explored;
use Hilsonxhero\ElasticVision\Application\IndexSettings;
use Hilsonxhero\ElasticVision\Domain\Analysis\Analysis;
use Hilsonxhero\ElasticVision\Domain\Analysis\Analyzer\StandardAnalyzer;
use Hilsonxhero\ElasticVision\Domain\Analysis\Filter\SynonymFilter;
use Laravel\Scout\Searchable;
class Post extends Model implements Explored, IndexSettings
{
use HasFactory;
use Searchable;
protected $fillable = ['title', 'published'];
public function mappableAs(): array
{
return [
'id' => 'keyword',
'title' => [
'type' => 'text',
'analyzer' => 'frameworks',
],
'published' => 'boolean',
'created_at' => 'date',
];
}
public function indexSettings(): array
{
$synonymFilter = new SynonymFilter();
$synonymFilter->setSynonyms(['vue => react']);
$synonymAnalyzer = new StandardAnalyzer('frameworks');
$synonymAnalyzer->setFilters(['lowercase', $synonymFilter]);
return (new Analysis())
->addAnalyzer($synonymAnalyzer)
->addFilter($synonymFilter)
->build();
}
}