This library allows you to create Elastica queries using the specification pattern.
You can write specifications using gbprod/specification library.
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"]),
)
;
}
}
$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());
$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);
- PHP 7.0+
composer require gbprod/elastica-specification