gbprod/elastica-specification

This library allows you to create Elastica queries using the Specification pattern

v2.0.0 2017-06-26 13:14 UTC

README

Build Status codecov Scrutinizer Code Quality Dependency Status

Latest Stable Version Total Downloads Latest Unstable Version License

This library allows you to create Elastica queries using the specification pattern.

Usage

You can write specifications using gbprod/specification library.

Creates a elastica specification filter

namespace GBProd\Acme\Elastica\SpecificationFactory;

use GBProd\ElasticaSpecification\QueryFactory\Factory;
use GBProd\Specification\Specification;
use Elastica\QueryBuilder;

class IsAvailableFactory implements Factory
{
    public function create(Specification $spec, QueryBuilder $qb)
    {
        return $qb->query()->bool()
            ->addMust(
                $qb->query()->term(['available' => "0"]),
            )
        ;
    }
}

Configure

$registry = new GBProd\ElasticaSpecification\Registry();
$qb = new \Elastica\QueryBuilder();

$handler = new GBProd\ElasticaSpecification\Handler($registry, $qb);
$handler->registerFactory(IsAvailable::class, new IsAvailableFactory());
$handler->registerFactory(StockGreaterThan::class, new StockGreaterThanFactory());

Use it

$available = new IsAvailable();
$hightStock = new StockGreaterThan(4);

$availableWithLowStock = $available
    ->andX(
        $hightStock->not()
    )
;

$type = $this->elasticaClient
    ->getIndex('my_index')
    ->getType('my_type')
;

$query = $handler->handle($availableWithLowStock)

$results = $type->search($query);

Requirements

  • PHP 7.0+

Installation

Using composer

composer require gbprod/elastica-specification