Skip to content

Instantly share code, notes, and snippets.

@spoonerWeb
Last active June 28, 2023 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spoonerWeb/f0393fbf393c2e5c351af555c3aca1fb to your computer and use it in GitHub Desktop.
Save spoonerWeb/f0393fbf393c2e5c351af555c3aca1fb to your computer and use it in GitHub Desktop.
Get result count of Solr search without facets in TYPO3
<?php
$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
$signalSlotDispatcher->connect(
\ApacheSolrForTypo3\Solr\Controller\SearchController::class,
'resultsAction',
\Vendor\ExtensionName\Slot\Solr::class,
'addResultsWithoutFacets'
);
<?php
namespace Vendor\ExtensionName\Slot;
/*
* This file is part of a TYPO3 extension.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
class Solr
{
public function addResultsWithoutFacets(array $values): array
{
// remove filter queries of facets
$filterQueries = $values['resultSet']->getUsedQuery()->getFilterQueries();
unset($filterQueries['type']);
$values['resultSet']->getUsedQuery()->setFilterQueries($filterQueries);
// get result count on search without filter queries
$allValuesFoundWithoutFacet = (int)$values['resultSet']->getUsedSearch()->search($values['resultSet']->getUsedQuery())->getParsedData()->response->numFound;
// set the default result count into variable
$values['allResultCountWithoutFacets'] = $values['resultSet']->getAllResultCount();
// save value from search without facet into variable
if ($allValuesFoundWithoutFacet > 0) {
$values['allResultCountWithoutFacets'] = $allValuesFoundWithoutFacet;
}
return [$values];
}
}
@helhum
Copy link

helhum commented Jun 28, 2023

Thanks a bunch @spoonerWeb that is exactly what I have been looking for. Works like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment