Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add current nested path in filter query builder to condition the gene… #785

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Smile\ElasticsuiteCore\Api\Index\Mapping\FieldInterface;
use Smile\ElasticsuiteCore\Search\Request\BucketInterface;
use Smile\ElasticsuiteCore\Api\Search\Request\ContainerConfigurationInterface;
use Smile\ElasticsuiteCore\Search\Request\Query\Builder as QueryBuilder;
use Smile\ElasticsuiteCore\Search\Request\Query\Filter\QueryBuilder;
use Smile\ElasticsuiteCore\Search\Request\QueryInterface;

/**
Expand Down Expand Up @@ -89,7 +89,11 @@ public function buildAggregations(
}

if (isset($bucketParams['nestedFilter'])) {
$nestedFilter = $this->createFilter($containerConfiguration, $bucketParams['nestedFilter']);
$nestedFilter = $this->createFilter(
$containerConfiguration,
$bucketParams['nestedFilter'],
$bucketParams['nestedPath']
);
$bucketParams['nestedFilter'] = $nestedFilter;
}

Expand All @@ -113,12 +117,13 @@ public function buildAggregations(
*
* @param ContainerConfigurationInterface $containerConfiguration Search container configuration
* @param array $filters Filters definition.
* @param string|null $currentPath Current nested path or null.
*
* @return QueryInterface
*/
private function createFilter(ContainerConfigurationInterface $containerConfiguration, array $filters)
private function createFilter(ContainerConfigurationInterface $containerConfiguration, array $filters, $currentPath = null)
{
return $this->queryBuilder->createFilterQuery($containerConfiguration, $filters);
return $this->queryBuilder->create($containerConfiguration, $filters, $currentPath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ public function __construct(QueryFactory $queryFactory)
*
* @param ContainerConfigurationInterface $containerConfig Search request container configuration.
* @param array $filters Filters to be built.
* @param string|null $currentPath Current nested path or null.
*
* @return QueryInterface
*/
public function create(ContainerConfigurationInterface $containerConfig, array $filters)
public function create(ContainerConfigurationInterface $containerConfig, array $filters, $currentPath = null)
{
$queries = [];

Expand All @@ -86,7 +87,7 @@ public function create(ContainerConfigurationInterface $containerConfig, array $
$queries[] = $condition;
} else {
$mappingField = $mapping->getField($fieldName);
$queries[] = $this->prepareFieldCondition($mappingField, $condition);
$queries[] = $this->prepareFieldCondition($mappingField, $condition, $currentPath);
}
}

Expand All @@ -102,12 +103,13 @@ public function create(ContainerConfigurationInterface $containerConfig, array $
/**
* Transform the condition into a search request query object.
*
* @param FieldInterface $field Filter field.
* @param array|string $condition Filter condition.
* @param FieldInterface $field Filter field.
* @param array|string $condition Filter condition.
* @param string|null $currentPath Current nested path or null.
*
* @return QueryInterface
*/
private function prepareFieldCondition(FieldInterface $field, $condition)
private function prepareFieldCondition(FieldInterface $field, $condition, $currentPath)
{
$queryType = QueryInterface::TYPE_TERMS;
$condition = $this->prepareCondition($condition);
Expand All @@ -134,14 +136,33 @@ private function prepareFieldCondition(FieldInterface $field, $condition)

$query = $this->queryFactory->create($queryType, $condition);

if ($field->isNested()) {
if ($this->isNestedField($field, $currentPath)) {
$queryParams = ['path' => $field->getNestedPath(), 'query' => $query];
$query = $this->queryFactory->create(QueryInterface::TYPE_NESTED, $queryParams);
}

return $query;
}

/**
* @param FieldInterface $field Filter field.
* @param string|null $currentPath Current nested path or null.
*
* @return bool
* @throws \LogicException
*/
private function isNestedField(FieldInterface $field, $currentPath)
{
if ($field->isNested() && $field->getNestedPath() !== $currentPath) {
throw new \LogicException("Can not filter nested field {$field->getName()} with nested path $currentPath");
}
if (!$field->isNested() && $currentPath !== null) {
throw new \LogicException("Can not filter non nested field {$field->getName()} in nested context ($currentPath)");
}

return $field->isNested() && $currentPath === null;
}

/**
* Ensure the condition is supported and try to tranform it into a supported type.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public function buildSordOrders(ContainerConfigurationInterface $containerConfig
}

if (isset($sortOrderParams['nestedFilter'])) {
$nestedFilter = $this->queryBuilder->create($containerConfig, $sortOrderParams['nestedFilter']);
$nestedFilter = $this->queryBuilder->create(
$containerConfig,
$sortOrderParams['nestedFilter'],
$sortOrderParams['nestedPath']
);
$sortOrderParams['nestedFilter'] = $nestedFilter;
}
} catch (\LogicException $e) {
Expand Down