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

EZP-32110: Exposed more search result data from Pagefanta adapters #144

Merged
merged 2 commits into from
Dec 30, 2020

Conversation

adamwojs
Copy link
Member

@adamwojs adamwojs commented Dec 6, 2020

Question Answer
JIRA issue EZP-32110
Type feature
Target eZ Platform version v3.3
BC breaks no
Doc needed yes

Introduced \eZ\Publish\Core\Pagination\Pagerfanta\SearchResultAdapter

/**
 * Contract for \eZ\Publish\API\Repository\SearchService based adapters.
 */
interface SearchResultAdapter extends AdapterInterface
{
    /**
     * Get results of aggregations associated with search query.
     *
     * Generates addition query if called before AdapterInterface::getSlice or AdapterInterface::getNbResults.
     */
    public function getAggregations(): AggregationResultCollection;

    /**
     * Get the duration of the search processing for current results slice (in s).
     *
     * Returns null if called before AdapterInterface::getSlice.
     */
    public function getTime(): ?float;

    /**
     * Indicates if the search has timed out for current results slice.
     *
     * Returns null if called before AdapterInterface::getSlice.
     */
    public function getTimedOut(): ?bool;

    /**
     * Return the maximum score or `null` if query wasn't executed.
     *
     * Returns null if called before AdapterInterface::getSlice.
     */
    public function getMaxScore(): ?float;

in order to be able to access additional search result data

  • Aggregations results
  • Max. score
  • Computation time
  • Timeout flag

from Pagefanta. 

In practice this change allows to use additional search results data when rendering query using built-in query controller, ez_render_content_query/ez_render_location_query etc.

Example

Query controller

config/packages/ezplatform.yaml

ezplatform:
    site:
        content_view:
            full:
                folder:
                    controller: 'ez_query::pagingQueryAction'
                    template: full/folder.html.twig
                    match:
                        Identifier\ContentType: folder
                    params:
                        query:
                            query_type: Children
                            parameters:
                                location: '@=location'
                            assign_results_to: items

templates/full/folder.html.twig

{# Access SearchResult::$aggregations #}
{{ dump(items.aggregations) }}
{# Access  SearchResult::{$time,timedOut,maxScore}. Requires first to fetch result set #}
{{ dump(items.maxScore) }}
{{ dump(items.timedOut) }}
{{ dump(items.time) }}

Custom controller

<?php 

namespace App\Controller;

use eZ\Publish\API\Repository\SearchService;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\Core\Pagination\Pagerfanta\ContentSearchAdapter;
use eZ\Publish\Core\Pagination\Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\Response;

final class CustomController
{
    /** @var \eZ\Publish\API\Repository\SearchService */
    private $searchService;

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

    public function searchAction(): Response
    {
        $query = new Query();
        // ... build query as usually

        // $items is instance of \eZ\Publish\Core\Pagination\Pagerfanta\Pagerfanta
        // instead of Pagerfanta\Pagerfanta
        $items = new Pagerfanta(new ContentSearchAdapter($query, $this->searchService));

        foreach ($items as $item) {
            // Iterate over results
        }

        // Access SearchResult::{$aggregations,maxScore,time,timedOUt}
        $items->getAggregations();
        $items->getMaxScore();
        $items->getTime();
        $items->getTimedOut();

        // ...
    }
}

Notes

  • It's also possible to access additional search result data from regular Pagerfanta\Pagerfanta object by using $pagerfanta->getAdapter()->get{Aggregations,MaxScore,Time,TimedOut}
  • Facets results are not exposed as they were deprecated in v3.2

Checklist:

  • Provided PR description.
  • Tested the solution manually.
  • Provided automated test coverage.
  • Checked that target branch is set correctly (master for features, the oldest supported for bugs).
  • Ran PHP CS Fixer for new PHP code (use $ composer fix-cs).
  • Asked for a review (ping @ezsystems/php-dev-team).

@adamwojs adamwojs added Feature New feature or request DX labels Dec 6, 2020
@adamwojs adamwojs self-assigned this Dec 6, 2020
@ezsystems ezsystems deleted a comment from sonarcloud bot Dec 6, 2020
@ezsystems ezsystems deleted a comment from sonarcloud bot Dec 19, 2020
@sonarcloud
Copy link

sonarcloud bot commented Dec 20, 2020

SonarCloud Quality Gate failed.

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

No Coverage information No Coverage information
17.3% 17.3% Duplication

@adamwojs adamwojs marked this pull request as ready for review December 20, 2020 11:15
@adamwojs adamwojs requested a review from a team December 22, 2020 13:26
Copy link
Contributor

@mikadamczyk mikadamczyk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests can be improved for duplication

Copy link
Member

@alongosz alongosz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SonarCloud analysis is relevant. I don't see a point of having it if we gonna ignore it (as communicated/requested internally). But +1 anyway, with one remark.

*/
declare(strict_types=1);

namespace eZ\Publish\Core\Pagination\Pagerfanta;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like custom code consumes methods from this interface. In this case it should be API. Given the context - entire pagerfanta impl. - which shouldn't even be part of Kernel, I'll accept for now, especially due to time constraints. But if you have time, fix it please.

@alongosz alongosz changed the title EZP-32110: Access additional search result data from Pagefanta adapters EZP-32110: Exposed more search result data from Pagefanta adapters Dec 28, 2020
@adamwojs adamwojs merged commit 243cdae into master Dec 30, 2020
@adamwojs adamwojs deleted the ezp_32110 branch December 30, 2020 13:34
Steveb-p pushed a commit that referenced this pull request Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DX Feature New feature or request Ready for QA
5 participants