Skip to content

Instantly share code, notes, and snippets.

@spoonerWeb
Created April 29, 2020 14:20
Show Gist options
  • Save spoonerWeb/dd4ed4b05d81efb9006bf123ee50a64d to your computer and use it in GitHub Desktop.
Save spoonerWeb/dd4ed4b05d81efb9006bf123ee50a64d to your computer and use it in GitHub Desktop.
TYPO3 Solr Search with AJAX and Middleware (TYPO3 >= 9.5, EXT:solr >= 10)

Solr search via AJAX and Middleware

How To

  • Add this code into your extension
  • Call your domain URL with the parameter ?ajaxsearch=1&q=<search-term>
  • Get JSON with all results based on your TS settings on root page
<?php
return [
'frontend' => [
'vendor-solr-search' => [
'target' => \Vendor\Extension\Middleware\SolrSearch::class,
'after' => [
'typo3/cms-frontend/prepare-tsfe-rendering'
]
]
]
];
<?php
namespace Vendor\Extension\Middleware;
/*
* 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!
*/
use ApacheSolrForTypo3\Solr\ConnectionManager;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService;
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestBuilder;
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
use ApacheSolrForTypo3\Solr\Search;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class SolrSearch implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if (!isset($request->getQueryParams()['ajaxsearch'], $request->getQueryParams()['q'])) {
return $response;
}
$typoscriptConfiguration = GeneralUtility::makeInstance(TypoScriptConfiguration::class, $GLOBALS['TSFE']->tmpl->setup, $request->getAttribute('site')->getRootpageId());
$searchRequestBuilder = new SearchRequestBuilder($typoscriptConfiguration);
$searchRequest = $searchRequestBuilder->buildForSearch(
$request->getQueryParams(), $request->getAttribute('site')->getRootpageId(), $request->getAttribute('language')->getLanguageId());
try {
$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)
->getConnectionByPageId(
$request->getAttribute('site')->getRootpageId(),
$request->getAttribute('language')->getLanguageId()
);
$search = GeneralUtility::makeInstance(Search::class, $solrConnection);
} catch (NoSolrConnectionFoundException $e) {
$response->getBody()->write('No Solr connection available.');
return $response;
}
$searchResultSet = GeneralUtility::makeInstance(SearchResultSetService::class, $typoscriptConfiguration, $search)->search($searchRequest);
$results = [];
foreach ($searchResultSet->getSearchResults()->getArrayCopy() as $result) {
/** @var \ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResult $result */
$results[] = $result->getFields();
}
return new JsonResponse($results);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment