From 998739cf3468d1b7518d60cf979a5abe1eeba979 Mon Sep 17 00:00:00 2001 From: Douglas Hammond Date: Thu, 12 Apr 2018 04:14:51 -0400 Subject: [PATCH] Expose facets on pagerfanta adapter (#74) Closes #72 --- .../Pagerfanta/ContentSearchHitAdapter.php | 53 +++++++++++++--- .../LocationSearchFilterAdapter.php | 60 ++++++++++++++++--- .../Pagerfanta/LocationSearchHitAdapter.php | 53 +++++++++++++--- 3 files changed, 144 insertions(+), 22 deletions(-) diff --git a/lib/Core/Site/Pagination/Pagerfanta/ContentSearchHitAdapter.php b/lib/Core/Site/Pagination/Pagerfanta/ContentSearchHitAdapter.php index bf49bc6f..dda6a4b7 100644 --- a/lib/Core/Site/Pagination/Pagerfanta/ContentSearchHitAdapter.php +++ b/lib/Core/Site/Pagination/Pagerfanta/ContentSearchHitAdapter.php @@ -27,6 +27,16 @@ class ContentSearchHitAdapter implements AdapterInterface */ private $nbResults; + /** + * @var \eZ\Publish\API\Repository\Values\Content\Search\Facet[] + */ + private $facets; + + /** + * @var \eZ\Publish\API\Repository\Values\Content\Search\SearchResult + */ + private $searchResult; + public function __construct(Query $query, FindService $findService) { $this->query = $query; @@ -44,10 +54,21 @@ public function getNbResults() return $this->nbResults; } - $countQuery = clone $this->query; - $countQuery->limit = 0; + return $this->nbResults = $this->getSearchResult()->totalCount; + } + + /** + * Returns the facets of the results. + * + * @return \eZ\Publish\API\Repository\Values\Content\Search\Facet[] The facets of the results + */ + public function getFacets() + { + if (isset($this->facets)) { + return $this->facets; + } - return $this->nbResults = $this->findService->findContent($countQuery)->totalCount; + return $this->facets = $this->getSearchResult()->facets; } /** @@ -65,13 +86,31 @@ public function getSlice($offset, $length) $query->limit = $length; $query->performCount = false; - $searchResult = $this->findService->findContent($query); + $this->searchResult = $this->findService->findContent($query); // Set count for further use if returned by search engine despite !performCount (Solr, ES) - if (!isset($this->nbResults) && isset($searchResult->totalCount)) { - $this->nbResults = $searchResult->totalCount; + if (!isset($this->nbResults) && isset($this->searchResult->totalCount)) { + $this->nbResults = $this->searchResult->totalCount; + } + + if (!isset($this->facets) && isset($this->searchResult->facets)) { + $this->facets = $this->searchResult->facets; + } + + return $this->searchResult->searchHits; + } + + /** + * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult + */ + private function getSearchResult() + { + if ($this->searchResult === null) { + $query = clone $this->query; + $query->limit = 0; + $this->searchResult = $this->findService->findContent($query); } - return $searchResult->searchHits; + return $this->searchResult; } } diff --git a/lib/Core/Site/Pagination/Pagerfanta/LocationSearchFilterAdapter.php b/lib/Core/Site/Pagination/Pagerfanta/LocationSearchFilterAdapter.php index e2c24208..4e156e87 100644 --- a/lib/Core/Site/Pagination/Pagerfanta/LocationSearchFilterAdapter.php +++ b/lib/Core/Site/Pagination/Pagerfanta/LocationSearchFilterAdapter.php @@ -27,6 +27,16 @@ class LocationSearchFilterAdapter implements AdapterInterface */ private $nbResults; + /** + * @var \eZ\Publish\API\Repository\Values\Content\Search\Facet[] + */ + private $facets; + + /** + * @var \eZ\Publish\API\Repository\Values\Content\Search\SearchResult + */ + private $searchResult; + /** * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query * @param \Netgen\EzPlatformSiteApi\API\FilterService $filterService @@ -37,16 +47,32 @@ public function __construct(LocationQuery $query, FilterService $filterService) $this->filterService = $filterService; } + /** + * Returns the number of results. + * + * @return int The number of results + */ public function getNbResults() { - if (null !== $this->nbResults) { + if (isset($this->nbResults)) { return $this->nbResults; } - $countQuery = clone $this->query; - $countQuery->limit = 0; + return $this->nbResults = $this->getSearchResult()->totalCount; + } - return $this->nbResults = $this->filterService->filterLocations($countQuery)->totalCount; + /** + * Returns the facets of the results. + * + * @return \eZ\Publish\API\Repository\Values\Content\Search\Facet[] The facets of the results + */ + public function getFacets() + { + if (isset($this->facets)) { + return $this->facets; + } + + return $this->facets = $this->getSearchResult()->facets; } /** @@ -64,18 +90,36 @@ public function getSlice($offset, $length) $query->limit = $length; $query->performCount = false; - $searchResult = $this->filterService->filterLocations($query); + $this->searchResult = $this->filterService->filterLocations($query); // Set count for further use if returned by search engine despite !performCount (Solr, ES) - if (null === $this->nbResults && null !== $searchResult->totalCount) { - $this->nbResults = $searchResult->totalCount; + if (!isset($this->nbResults) && isset($this->searchResult->totalCount)) { + $this->nbResults = $this->searchResult->totalCount; + } + + if (!isset($this->facets) && isset($this->searchResult->facets)) { + $this->facets = $this->searchResult->facets; } $list = []; - foreach ($searchResult->searchHits as $hit) { + foreach ($this->searchResult->searchHits as $hit) { $list[] = $hit->valueObject; } return $list; } + + /** + * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult + */ + private function getSearchResult() + { + if ($this->searchResult === null) { + $query = clone $this->query; + $query->limit = 0; + $this->searchResult = $this->filterService->filterLocations($query); + } + + return $this->searchResult; + } } diff --git a/lib/Core/Site/Pagination/Pagerfanta/LocationSearchHitAdapter.php b/lib/Core/Site/Pagination/Pagerfanta/LocationSearchHitAdapter.php index c1f4ea05..5b40df1c 100644 --- a/lib/Core/Site/Pagination/Pagerfanta/LocationSearchHitAdapter.php +++ b/lib/Core/Site/Pagination/Pagerfanta/LocationSearchHitAdapter.php @@ -27,6 +27,16 @@ class LocationSearchHitAdapter implements AdapterInterface */ private $nbResults; + /** + * @var \eZ\Publish\API\Repository\Values\Content\Search\Facet[] + */ + private $facets; + + /** + * @var \eZ\Publish\API\Repository\Values\Content\Search\SearchResult + */ + private $searchResult; + public function __construct(LocationQuery $query, FindService $findService) { $this->query = $query; @@ -44,10 +54,21 @@ public function getNbResults() return $this->nbResults; } - $countQuery = clone $this->query; - $countQuery->limit = 0; + return $this->nbResults = $this->getSearchResult()->totalCount; + } + + /** + * Returns the facets of the results. + * + * @return \eZ\Publish\API\Repository\Values\Content\Search\Facet[] The facets of the results + */ + public function getFacets() + { + if (isset($this->facets)) { + return $this->facets; + } - return $this->nbResults = $this->findService->findLocations($countQuery)->totalCount; + return $this->facets = $this->getSearchResult()->facets; } /** @@ -65,13 +86,31 @@ public function getSlice($offset, $length) $query->limit = $length; $query->performCount = false; - $searchResult = $this->findService->findLocations($query); + $this->searchResult = $this->findService->findLocations($query); // Set count for further use if returned by search engine despite !performCount (Solr, ES) - if (!isset($this->nbResults) && isset($searchResult->totalCount)) { - $this->nbResults = $searchResult->totalCount; + if (!isset($this->nbResults) && isset($this->searchResult->totalCount)) { + $this->nbResults = $this->searchResult->totalCount; + } + + if (!isset($this->facets) && isset($this->searchResult->facets)) { + $this->facets = $this->searchResult->facets; + } + + return $this->searchResult->searchHits; + } + + /** + * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult + */ + private function getSearchResult() + { + if ($this->searchResult === null) { + $query = clone $this->query; + $query->limit = 0; + $this->searchResult = $this->findService->findLocations($query); } - return $searchResult->searchHits; + return $this->searchResult; } }