Commit 5c78f3cd authored by Markus Sommer's avatar Markus Sommer
Browse files

[FEATURE] Integrate suggest

parent f31147b5
Pipeline #1229 passed with stages
in 3 minutes and 35 seconds
......@@ -56,7 +56,8 @@
},
"autoload": {
"psr-4": {
"t3o\\t3olayout\\": "html/typo3conf/ext/t3olayout/Classes"
"T3o\\T3olayout\\": "html/typo3conf/ext/t3olayout/Classes",
"T3o\\T3orgLayout\\": "html/typo3conf/ext/t3org_layout/Classes"
}
}
}
<?php
namespace T3o\T3orgLayout\Controller;
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
use ApacheSolrForTypo3\Solr\Query;
use ApacheSolrForTypo3\Solr\Search;
use ApacheSolrForTypo3\Solr\Util;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Utility\EidUtility;
/**
* Search eid for solr
* url: index.php?eID=SolrSuggest&term=sdfsdfsdf
*
* @scope frontend
*/
class SolrInstantSearchController
{
/**
* @var Query
*/
protected $query = null;
/**
* @param string $term
*/
public static function suggest($term)
{
EidUtility::initTCA();
$GLOBALS['TSFE'] = GeneralUtility::makeInstance(
TypoScriptFrontendController::class,
$GLOBALS['TYPO3_CONF_VARS'],
0,
0
);
$GLOBALS['TSFE']->connectToDB();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->getConfigArray();
$siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
$site = $siteRepository->getSiteByPageId($GLOBALS['TSFE']->id);
$searchTerm = GeneralUtility::_GP('term');
$solrConfiguration = Util::getSolrConfigurationFromPageId($GLOBALS['TSFE']->id, FALSE, $GLOBALS['TSFE']->sys_language_uid);
$solrConfiguration = $solrConfiguration->getSearchConfiguration();
$query = GeneralUtility::makeInstance(Query::class, $searchTerm . '*');
$query->setGrouping(true);
$query->addReturnField('title');
$query->addReturnField('url');
$query->addReturnField('score');
$query->addReturnField('uid');
$query->addGroupField('type');
$query->setNumberOfResultsPerGroup(5);
if (!empty($solrConfiguration['query.']['boostFunction'])) {
$query->setBoostFunction($solrConfiguration['query.']['boostFunction']);
}
if (!empty($solrConfiguration['query.']['boostQuery'])) {
$query->setBoostQuery($solrConfiguration['query.']['boostQuery']);
}
if (!empty($solrConfiguration['query.']['boostQuery.'])) {
$boostQueries = [];
$boostConfiguration = $solrConfiguration['query.']['boostQuery.'];
foreach ($boostConfiguration as $boostQuery) {
$boostQueries[] = $boostQuery;
}
$query->setBoostQuery($boostQueries);
}
$query->setSiteHashFilter($site->getDomain());
$solrSearch = new Search();
$search = $solrSearch->search($query, 0, 5);
$response = json_decode($search->getRawResponse());
/** @var StandaloneView $template */
$template = GeneralUtility::makeInstance(StandaloneView::class);
$template->setTemplatePathAndFilename(
ExtensionManagementUtility::extPath(
't3org_layout'
) . '/Resources/Private/Templates/Solr/Suggest.html'
);
$template->assign('response', $response);
$template->assign('searchTerm', urlencode($searchTerm));
echo $template->render();
}
}
\ No newline at end of file
<div class="solr-suggest-container">
<f:if condition="{response.grouped.type.groups}">
<f:then>
<f:for each="{response.grouped.type.groups}" as="group">
<div class="solr-suggest-results">
<h3>
<f:translate key="solr_suggest.group.{group.groupValue}" default="{group.groupValue}" />
</h3>
<ul>
<f:for each="{group.doclist.docs}" as="doc">
<li><a class="solr-suggest-result" href="{doc.url}">{doc.title}</a>
</f:for>
</ul>
</div>
</f:for>
</f:then>
<f:else>
<f:translate key="solr_suggest.no_restult}" default="no result" />
</f:else>
</f:if>
</div>
<?php
// url index.php?eID=SolrSuggest&term=sdfsdfsdf
$GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include']['SolrSuggest'] = \T3o\T3orgLayout\Controller\SolrInstantSearchController::class . '::suggest';
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment