Skip to content

Commit

Permalink
Expose facets on pagerfanta adapter (netgen#74)
Browse files Browse the repository at this point in the history
Closes netgen#72
  • Loading branch information
wizhippo authored and pspanja committed Apr 12, 2018
1 parent 8da6a62 commit 998739c
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 22 deletions.
53 changes: 46 additions & 7 deletions lib/Core/Site/Pagination/Pagerfanta/ContentSearchHitAdapter.php
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
53 changes: 46 additions & 7 deletions lib/Core/Site/Pagination/Pagerfanta/LocationSearchHitAdapter.php
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}

0 comments on commit 998739c

Please sign in to comment.