/** * Search & display of search results * * @param \string $searchterm Search term(s) * @param \int $pointer Result pointer * @param \boolean $notfound Indicator for 404 based search * @return \void */ public function resultsAction($searchterm = '', $pointer = 0, $notfound = false) { $page = intval($this->settings['defaultResultsPage']) ? intval($this->settings['defaultResultsPage']) : $GLOBALS['TSFE']->id; $this->settings = \Tollwerk\TwLucenesearch\Utility\Indexer::indexConfig($GLOBALS['TSFE']); $indexInfo = null; $hits = array(); $error = false; $query = null; // Instanciating the lucene index service /* @var $indexerService \Tollwerk\TwLucenesearch\Service\Lucene */ $indexerService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstanceService('index', 'lucene'); if ($indexerService instanceof \TYPO3\CMS\Core\Service\AbstractService) { $indexInfo = $indexerService->indexInfo(); // Run the search $hits = $indexerService->find($searchterm, $query); // If the search didn't complete successful if (!$hits instanceof \Tollwerk\TwLucenesearch\Domain\Model\QueryHits) { $error = true; } } $this->view->assign('settings', $this->settings); $this->view->assign('error', $error); $this->view->assign('searchterm', trim($searchterm)); $this->view->assign('page', $page); $this->view->assign('indexInfo', $indexInfo); $this->view->assign('hits', $hits); $this->view->assign('query', $query); $this->view->assign('notfound', $notfound * 1); $this->view->assign('baseUrl', $_SERVER['SERVER_NAME']); }
/** * Set the indexed and page title of the current page * * @param string $title User defined page title * @param string $format Title format with substitution markers (%S = Website title, %P = Page title, %C = User defined title as given in argument 1) * @param string $pageFormat Optional: Alternative format for page title * @return string Dummy result string (empty) */ public function render($title, $format = '%S: %P - %C', $pageFormat = null) { \Tollwerk\TwLucenesearch\Utility\Indexer::setPageTitle($title, $format, $pageFormat); return ''; }
/** * Rewrite the original search in accordance to the search configuration * * @param string $searchterm Original search terms * @param array $tokenTerms Single search term tokens * @return string Rewritten search terms */ protected function _rewriteQueryTerms($searchTerm, &$tokenTerms = array()) { $tokenTerms = array(); try { $searchConfig = \Tollwerk\TwLucenesearch\Utility\Indexer::indexConfig($GLOBALS['TSFE'], 'search.searchConfig'); $lexer = new \Zend_Search_Lucene_Search_QueryLexer(); // Iterate over the extracted tokens /* @var $token \Zend_Search_Lucene_Search_QueryToken */ $tokens = array_reverse($lexer->tokenize($searchTerm, 'UTF-8')); $tokenCount = count($tokens); foreach ($tokens as $tokenIndex => $token) { // If there's a word or a phrase which has no certain field name applied if (($token->type == \Zend_Search_Lucene_Search_QueryToken::TT_WORD || $token->type == \Zend_Search_Lucene_Search_QueryToken::TT_PHRASE) && ($tokenIndex == $tokenCount - 1 || $tokens[$tokenIndex + 1]->type != \Zend_Search_Lucene_Search_QueryToken::TT_FIELD)) { $tokenTerms[] = $token; $isPhrase = $token->type == \Zend_Search_Lucene_Search_QueryToken::TT_PHRASE; $tokenStr = $isPhrase ? '"' . $token->text . '"' : $token->text; $tokenStrLength = strlen($tokenStr); // Apply external rewriters if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tw_lucenesearch']['term-rewrite-hooks'])) { $params = array('token' => &$token, 'pObj' => &$this); foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tw_lucenesearch']['term-rewrite-hooks'] as $rewriteHook) { \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($rewriteHook, $params, $this); } } // If the token type is being changed from "word" dot "phrase" ... if (!$isPhrase && $token->type == \Zend_Search_Lucene_Search_QueryToken::TT_PHRASE) { $isPhrase = true; $tokenStr = '"' . $token->text . '"'; } // Rewrite in accordance to the search configuration $tokenRewrite = array(); foreach ($searchConfig as $searchField) { $value = $isPhrase || $searchField->fuzzy ? strtr($searchField->value, array('*' => '')) : $searchField->value; $sfTokenStr = $searchField->field !== true ? $searchField->field . ':' : ''; $sfTokenStr .= strtr($value, array('?' => $tokenStr)); if ($searchField->fuzzy !== false) { $sfTokenStr .= '~' . $searchField->fuzzy; } if ($searchField->boost !== null) { $sfTokenStr .= '^' . $searchField->boost; } $tokenRewrite[] = $sfTokenStr; } $tokenRewrite = '(' . implode(' OR ', $tokenRewrite) . ')'; $searchTerm = substr($searchTerm, 0, $token->position - $tokenStrLength) . $tokenRewrite . substr($searchTerm, $token->position); } } } catch (\Zend_Search_Lucene_Search_QueryParserException $e) { } return $searchTerm; }