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

IBX-3639: As an Editor I'd like use facets to filter global search results #26

Merged
merged 4 commits into from
Sep 15, 2023
Merged
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
22 changes: 22 additions & 0 deletions src/bundle/Form/ChoiceList/View/FacetGroupView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Search\Form\ChoiceList\View;

use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;

final class FacetGroupView extends ChoiceGroupView
{
public static function createFromChoiceGroupView(ChoiceGroupView $groupView): self
{
return new self(
$groupView->label,
$groupView->choices
);
}
}
32 changes: 32 additions & 0 deletions src/bundle/Form/ChoiceList/View/FacetView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Search\Form\ChoiceList\View;

use Ibexa\Contracts\Core\Repository\Values\Content\Search\AggregationResult\TermAggregationResultEntry;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;

final class FacetView extends ChoiceView
{
public ?TermAggregationResultEntry $term = null;

public static function createFromChoiceView(ChoiceView $choiceView, ?TermAggregationResultEntry $term): self
{
$facet = new self(
$choiceView->data,
$choiceView->value,
$choiceView->label,
$choiceView->attr,
$choiceView->labelTranslationParameters,
);

$facet->term = $term;

return $facet;
}
}
1 change: 1 addition & 0 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
imports:
- { resource: forms.yaml }
- { resource: twig.yaml }
- { resource: sorting_definitions.yaml }
- { resource: views.yaml }

Expand Down
9 changes: 9 additions & 0 deletions src/bundle/Resources/config/twig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Ibexa\Bundle\Search\Twig\Extension\SearchFacetsExtension:
tags:
- name: twig.extension
92 changes: 92 additions & 0 deletions src/bundle/Twig/Extension/SearchFacetsExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Search\Twig\Extension;

use Ibexa\Bundle\Search\Form\ChoiceList\View\FacetGroupView;
use Ibexa\Bundle\Search\Form\ChoiceList\View\FacetView;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\AggregationResult\TermAggregationResult;
use Ibexa\Contracts\Core\Repository\Values\Content\Search\AggregationResult\TermAggregationResultEntry;
use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
use Symfony\Component\Form\ChoiceList\View\ChoiceView;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

final class SearchFacetsExtension extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter(
'ibexa_choices_as_facets',
[$this, 'getChoicesAsFacets']
),
];
}

/**
* @param \Symfony\Component\Form\ChoiceList\View\ChoiceView[]|\Symfony\Component\Form\ChoiceList\View\ChoiceGroupView[] $choices
* @param callable(ChoiceView, TermAggregationResultEntry): bool|null $comparator
*
* @return \Symfony\Component\Form\ChoiceList\View\ChoiceView[]|\Symfony\Component\Form\ChoiceList\View\ChoiceGroupView[]
*/
public function getChoicesAsFacets(
array $choices,
?TermAggregationResult $terms,
?callable $comparator = null
): array {
if ($terms === null) {
return $choices;
}

if ($comparator === null) {
$comparator = static function (ChoiceView $choice, TermAggregationResultEntry $term): bool {
return $choice->data == $term->getKey();
};
}

$facets = [];
foreach ($choices as $key => $choice) {
if ($choice instanceof ChoiceGroupView) {
$group = FacetGroupView::createFromChoiceGroupView($choice);
$group->choices = $this->getChoicesAsFacets($group->choices, $terms, $comparator);
if (!empty($group->choices)) {
$facets[$key] = $group;
}

continue;
}

$term = $this->findTermEntry($terms, $choice, $comparator);
if ($term !== null) {
$facet = FacetView::createFromChoiceView($choice, $term);
$facet->label = sprintf('%s (%d)', $choice->label, $term->getCount());
$facets[$key] = $facet;
}
}

return $facets;
}

/**
* @param callable(ChoiceView, TermAggregationResultEntry): bool $comparator
*/
private function findTermEntry(
TermAggregationResult $terms,
ChoiceView $choice,
callable $comparator
): ?TermAggregationResultEntry {
foreach ($terms->getEntries() as $entry) {
if ($comparator($choice, $entry) === true) {
return $entry;
}
}

return null;
}
}
40 changes: 38 additions & 2 deletions src/lib/QueryType/SearchQueryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@

use Ibexa\Bundle\Search\Form\Data\SearchData;
use Ibexa\Contracts\Core\Repository\Values\Content\Query;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\ContentTypeTermAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Aggregation\SectionTermAggregation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause\ContentId;
use Ibexa\Contracts\Core\Repository\Values\User\User;
use Ibexa\Contracts\Search\SortingDefinition\SortingDefinitionRegistryInterface;
use Ibexa\Core\QueryType\OptionsResolverBasedQueryType;
use Ibexa\Core\Repository\SearchService;
use Symfony\Component\OptionsResolver\OptionsResolver;

class SearchQueryType extends OptionsResolverBasedQueryType
{
private SearchService $searchService;

private SortingDefinitionRegistryInterface $sortingDefinitionRegistry;

public function __construct(SortingDefinitionRegistryInterface $sortingDefinitionRegistry)
{
public function __construct(
SearchService $searchService,
SortingDefinitionRegistryInterface $sortingDefinitionRegistry
) {
$this->sortingDefinitionRegistry = $sortingDefinitionRegistry;
$this->searchService = $searchService;
}

protected function doGetQuery(array $parameters): Query
Expand All @@ -49,13 +57,19 @@ protected function doGetQuery(array $parameters): Query
// Search results order MUST BE deterministic
$query->sortClauses[] = new ContentId(Query::SORT_ASC);

if ($this->searchService->supports(SearchService::CAPABILITY_AGGREGATIONS)) {
$query->aggregations[] = $this->buildContentTypeTermAggregation($parameters);
$query->aggregations[] = $this->buildSectionTermAggregation($parameters);
}

return $query;
}

protected function configureOptions(OptionsResolver $optionsResolver): void
{
$optionsResolver->setDefaults([
'search_data' => new SearchData(),
'facets_limit' => 128,
]);

$optionsResolver->setAllowedTypes('search_data', SearchData::class);
Expand Down Expand Up @@ -134,6 +148,28 @@ protected function buildCriteria(SearchData $searchData): array

return $criteria;
}

/**
* @param array<string, mixed> $parameters
*/
private function buildContentTypeTermAggregation(array $parameters): ContentTypeTermAggregation
{
$aggregation = new ContentTypeTermAggregation('content_types');
$aggregation->setLimit($parameters['facets_limit']);

return $aggregation;
}

/**
* @param array<string, mixed> $parameters
*/
private function buildSectionTermAggregation(array $parameters): SectionTermAggregation
{
$aggregation = new SectionTermAggregation('sections');
$aggregation->setLimit($parameters['facets_limit']);

return $aggregation;
}
}

class_alias(SearchQueryType::class, 'Ibexa\Platform\Search\QueryType\SearchQueryType');
13 changes: 7 additions & 6 deletions src/lib/View/SearchViewBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,20 @@ public function buildView(array $parameters): SearchView
: null;
$languageFilter = $this->getSearchLanguageFilter($searchLanguageCode);

$pagerfanta = new Pagerfanta(
new ContentSearchHitAdapter(
$this->searchQueryType->getQuery(['search_data' => $data]),
$this->searchService,
$languageFilter
)
$adapter = new ContentSearchHitAdapter(
$this->searchQueryType->getQuery(['search_data' => $data]),
$this->searchService,
$languageFilter
);

$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage($data->getLimit());
$pagerfanta->setCurrentPage(min($data->getPage(), $pagerfanta->getNbPages()));

$view->addParameters([
'results' => $this->pagerSearchContentToDataMapper->map($pagerfanta),
'pager' => $pagerfanta,
'aggregations' => $adapter->getAggregations(),
]);
}

Expand Down
Loading