PHP code example of jackardios / elastic-query-wizard
1. Go to this page and download the library: Download jackardios/elastic-query-wizard 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/ */
jackardios / elastic-query-wizard example snippets
use Jackardios\EsScoutDriver\Searchable;
class Post extends Model
{
use Searchable;
// ...
}
use Jackardios\ElasticQueryWizard\ElasticQueryWizard;
use Jackardios\ElasticQueryWizard\ElasticFilter;
use Jackardios\ElasticQueryWizard\ElasticSort;
// GET /posts?filter[status]=published&filter[title]=laravel&sort=-created_at&ElasticSort::field('created_at'),
ElasticSort::field('title'),
])
->allowedIncludes(['author', 'comments'])
->build()
->execute()
->models();
use Jackardios\QueryWizard\Schema\ResourceSchema;
use Jackardios\QueryWizard\Contracts\QueryWizardInterface;
class PostSchema extends ResourceSchema
{
public function model(): string
{
return Post::class;
}
public function filters(QueryWizardInterface $wizard): array
{
return [
'status',
ElasticFilter::match('title'),
ElasticFilter::multiMatch(['title^2', 'body'], 'search'),
];
}
public function sorts(QueryWizardInterface $wizard): array
{
return ['created_at', 'title'];
}
public function
// Create wizard from schema
$posts = ElasticQueryWizard::forSchema(PostSchema::class)
->build()
->execute()
->models();
// Override schema settings for specific endpoints
ElasticQueryWizard::forSchema(PostSchema::class)
->disallowedFilters('status') // Remove filter
->disallowedIncludes('comments') // Remove
use Jackardios\ElasticQueryWizard\ElasticQueryWizard;
use Jackardios\ElasticQueryWizard\ElasticQuery;
use Jackardios\ElasticQueryWizard\ElasticAggregation;
$results = ElasticQueryWizard::for(Article::class)
->allowedFilters([
ElasticFilter::term('status'),
ElasticFilter::multiMatch(['title^3', 'body'], 'search'),
])
->query(ElasticQuery::match('language', 'en'))
->must(ElasticQuery::range('published_at')->gte('2024-01-01'))
->highlight('title')
->aggregate('by_author', ElasticAggregation::terms('author')->size(10))
->trackTotalHits(true)
->build()
->execute();
// GET /posts?filter[trashed]=with ((only trashed)
// GET /posts?filter[trashed]=without (exclude trashed)
// GET /posts?filter[trashed]=true|false (aliases)
ElasticQueryWizard::for(Post::class)
->allowedFilters([
ElasticFilter::trashed(),
])
->build()
->execute()
->models();
use Jackardios\ElasticQueryWizard\ElasticGroup;
// GET /posts?filter[status]=published&filter[priority]=high
// Matches posts where status=published OR priority=high
ElasticQueryWizard::for(Post::class)
->allowedFilters([
ElasticFilter::term('category'),
// OR condition: match at least one
ElasticGroup::bool('status_or_priority')
->minimumShouldMatch(1)
->children([
ElasticFilter::term('status', 'status')->inShould(),
ElasticFilter::term('priority', 'priority')->inShould(),
]),
])
->build()
->execute()
->models();
// GET /posts?filter[comment_author]=john&filter[comment_text]=great
// Matches posts with comments where author=john AND body contains "great"
ElasticQueryWizard::for(Post::class)
->allowedFilters([
ElasticFilter::term('status'),
// Query nested "comments" documents
ElasticGroup::nested('comments')
->scoreMode('avg')
->children([
ElasticFilter::term('comments.author', 'comment_author'),
ElasticFilter::match('comments.body', 'comment_text'),
]),
])
->build()
->execute()
->models();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.