gbprod/elastica-specification

此软件包已被废弃,不再维护。未建议替代软件包。

此库允许您使用规范模式创建Elastica查询

v2.1.0 2018-02-08 06:45 UTC

This package is auto-updated.

Last update: 2021-08-07 10:30:25 UTC


README

Build Status codecov Scrutinizer Code Quality Dependency Status

Latest Stable Version Total Downloads Latest Unstable Version License

此库允许您使用规范模式创建Elastica查询。

使用方法

您可以使用gbprod/specification库编写规范。

创建Elastica规范过滤器

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

composer require gbprod/elastica-specification