<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
greensight / laravel-elastic-query-specification example snippets
use Greensight\LaravelElasticQuery\Declarative\Agregating\AllowedAggregate;
use Greensight\LaravelElasticQuery\Declarative\Filtering\AllowedFilter;
use Greensight\LaravelElasticQuery\Declarative\Sorting\AllowedSort;
use Greensight\LaravelElasticQuery\Declarative\Specification\CompositeSpecification;
use Greensight\LaravelElasticQuery\Declarative\Specification\Specification;
class ProductSpecification extends CompositeSpecification
{
public function __construct()
{
parent::__construct();
$this->allowedFilters([
'package',
'active',
AllowedFilter::exact('cashback', 'cashback.active')->default(true)
]);
$this->allowedSorts(['name', 'rating']);
$this->allowedAggregates([
'package',
AllowedAggregate::minmax('rating')
]);
$this->whereNotNull('package');
$this->nested('offers', function (Specification $spec) {
$spec->allowedFilters(['seller_id', 'active']);
$spec->allowedAggregates([
'seller_id',
AllowedAggregate::minmax('price')
]);
$spec->allowedSorts([
AllowedSort::field('price')->byMin()
]);
});
}
}
$this->nested('nested_field', function (Specification $spec) { ... })
$this->nested('nested_field', new SomeSpecificationImpl());
$this->allowedFilters([AllowedFilter::exact('name', 'field')->default(500)]);
// the following statements are equivalent
$this->allowedFilters(['name']);
$this->allowedFilters([AllowedFilter::exact('name', 'name')]);
AllowedFilter::exact('name', 'field'); // Значение поля проверяется на равенство одному из заданных
AllowedFilter::exists('name', 'field'); // Проверяется, что поле присутствует в документе и имеет ненулевое значение
$this->allowedSorts([AllowedSort::field('name', 'field')]);
// the following statements are equivalent
$this->allowedSorts(['name']);
$this->allowedSorts([AllowedSort::field('+name', 'name')]);
$this->allowedSorts([AllowedSort::field('+name', 'name')->byMin()]);
// set the sorting mode
$this->allowedSorts([AllowedSort::field('name', 'field')->byMin()]);
$this->allowedSorts([AllowedSort::field('name', 'field')->byMax()]);
$this->allowedSorts([AllowedSort::field('name', 'field')->byAvg()]);
$this->allowedSorts([AllowedSort::field('name', 'field')->bySum()]);
$this->allowedSorts([AllowedSort::field('name', 'field')->byMedian()]);
$this->allowedAggregates([AllowedAggregate::terms('name', 'field')]);
// the following statements are equivalent
$this->allowedAggregates(['name']);
$this->allowedAggregates([AllowedAggregate::terms('name', 'name')]);
AllowedAggregate::terms('name', 'field'); // Get all variants of attribute values
AllowedAggregate::minmax('name', 'field'); // Get min and max attribute values
use Greensight\LaravelElasticQuery\Declarative\SearchQueryBuilder;
use Greensight\LaravelElasticQuery\Declarative\QueryBuilderRequest;
class ProductsSearchQuery extends SearchQueryBuilder
{
public function __construct(QueryBuilderRequest $request)
{
parent::__construct(ProductsIndex::query(), new ProductSpecification(), $request);
}
}
class ProductsController
{
// ...
public function index(ProductsSearchQuery $query)
{
return ProductResource::collection($query->get());
}
}
use Greensight\LaravelElasticQuery\Declarative\AggregateQueryBuilder;
use Greensight\LaravelElasticQuery\Declarative\QueryBuilderRequest;
class ProductsAggregateQuery extends AggregateQueryBuilder
{
public function __construct(QueryBuilderRequest $request)
{
parent::__construct(ProductsIndex::aggregate(), new ProductSpecification(), $request);
}
}
class ProductsController
{
// ...
public function totals(ProductsAggregateQuery $query)
{
return new ProductAggregateResource($query->get());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.