<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ensi / laravel-elastic-query-specification example snippets
use Ensi\LaravelElasticQuerySpecification\Agregating\AllowedAggregate;
use Ensi\LaravelElasticQuerySpecification\Filtering\AllowedFilter;
use Ensi\LaravelElasticQuerySpecification\Sorting\AllowedSort;
use Ensi\LaravelElasticQuerySpecification\Specification\CompositeSpecification;
use Ensi\LaravelElasticQuerySpecification\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->allowedFacets([
'package'
]);
$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()
]);
$spec->allowedFacets([
AllowedFacet::terms('seller_id')
]);
});
}
}
$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'); // The field value is checked for equality to one of the specified
AllowedFilter::exists('name', 'field'); // There is a check that the field is in the document and has a non-zero value.
AllowedFilter::greater('name', 'field'); // The field value must be greater than the specified one.
AllowedFilter::greaterOrEqual('name', 'field'); // The field value must be greater than or equal to the specified one.
AllowedFilter::less('name', 'field'); // The field value must be less than the specified one.
AllowedFilter::lessOrEqual('name', 'field'); // The field value must be less than or equal to the specified one.
AllowedFilter::match('name', 'field'); // Full text search in the field
AllowedFilter::multiMatch('name', ['field1^3', 'field2']); // Full text search in the fields
$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 Ensi\LaravelElasticQuerySpecification\SearchQueryBuilder;
use Ensi\LaravelElasticQuerySpecification\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 Ensi\LaravelElasticQuerySpecification\AggregateQueryBuilder;
use Ensi\LaravelElasticQuerySpecification\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());
}
}
use Ensi\LaravelElasticQuerySpecification\FacetQueryBuilder;
use Ensi\LaravelElasticQuerySpecification\QueryBuilderRequest;
class ProductsFacetsQuery extends FacetQueryBuilder
{
public function __construct(QueryBuilderRequest $request)
{
parent::__construct(ProductsIndex::aggregate(), new ProductSpecification(), $request);
}
}
class ProductsController
{
// ...
public function facets(ProductsFacetsQuery $query)
{
return new ProductFacetsResource($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.