Skip to content

Commit

Permalink
Making to be possible search with null in findBy (exists query) just …
Browse files Browse the repository at this point in the history
…like is in querybuilder
  • Loading branch information
andsalves committed Nov 7, 2017
1 parent 8976032 commit 89febb4
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Persister/ElasticEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use DoctrineElastic\Mapping\MetaField;
use DoctrineElastic\Mapping\Type;
use DoctrineElastic\Query\ElasticQueryExecutor;
use DoctrineElastic\Query\Walker\Helper\WalkerHelper;
use DoctrineElastic\Query\Walker\OperatorsMap;

/**
* Entity Persister for this doctrine elastic extension
Expand Down Expand Up @@ -48,13 +50,17 @@ class ElasticEntityPersister {
/** @var ElasticEntityManager */
private $em;

/** @var WalkerHelper */
private $walkerHelper;

public function __construct(ElasticEntityManager $em, $className) {
$this->className = $className;
$this->annotationReader = new AnnotationReader();
$this->queryExecutor = new ElasticQueryExecutor($em);
$this->hydrator = new AnnotationEntityHydrator();
$this->validateEntity();
$this->em = $em;
$this->walkerHelper = new WalkerHelper();
}

public function getReflectionClass() {
Expand Down Expand Up @@ -90,8 +96,8 @@ private function validateEntity() {

public function loadAll(array $criteria = [], array $orderBy = null, $limit = null, $offset = null) {
$type = $this->getEntityType();
$sort = $must = [];
$body = ['query' => ['bool' => ['must' => $must]]];
$sort = [];
$body = ['query' => ['bool' => ['must' => []]]];
/** @var Field $annotationProperty */
$fieldAnnotations = $this->hydrator->extractSpecAnnotations($this->className, Field::class);
/** @var MetaField[] $metaFieldAnnotations */
Expand All @@ -115,10 +121,8 @@ public function loadAll(array $criteria = [], array $orderBy = null, $limit = nu
if ($annotation->name === '_parent') {
$searchParams->setParent($criteria[$columnName]);
} else {
$must[] = array(
'match' => array(
$annotation->name => $criteria[$columnName],
)
$this->walkerHelper->addBodyStatement(
$annotation->name, OperatorsMap::EQ, $criteria[$columnName], $body
);
}
}
Expand All @@ -133,8 +137,6 @@ public function loadAll(array $criteria = [], array $orderBy = null, $limit = nu
}
}

$body['query']['bool']['must'] = $must;

$searchParams->setIndex($type->getIndex());
$searchParams->setType($type->getName());
$searchParams->setBody($body);
Expand Down Expand Up @@ -195,6 +197,7 @@ public function executeInserts() {
* @param Type $type
* @param string $className
* @throws ElasticConstraintException
* @throws ElasticOperationException
*/
private function createTypeIfNotExists(Type $type, $className) {
foreach ($type->getChildClasses() as $childClass) {
Expand Down

0 comments on commit 89febb4

Please sign in to comment.