protected function analyzeScore(array $resultDocument) { $highScores = array(); $debugData = $this->search->getDebugResponse()->explain->{$resultDocument['id']}; /* TODO Provide better parsing * * parsing could be done line by line, * * recording indentation level * * replacing abbreviations * * replacing phrases like "product of" by mathematical symbols (* or x) * * ... */ // matches search term weights, ex: 0.42218783 = (MATCH) weight(content:iPod^40.0 in 43), product of: $pattern = '/(.*) = \\(MATCH\\) weight\\((.*)\\^/'; $matches = array(); preg_match_all($pattern, $debugData, $matches); foreach ($matches[0] as $key => $value) { // split field from search term list($field, $searchTerm) = explode(':', $matches[2][$key]); // keep track of highest score per search term if (!isset($highScores[$field]) || $highScores[$field]['score'] < $matches[1][$key]) { $highScores[$field] = array('score' => $matches[1][$key], 'field' => $field, 'searchTerm' => $searchTerm); } } return $highScores; }
/** * Modifies the given query and returns the modified query as result * * @param ResultsCommand $resultCommand The search result command * @param array $resultDocument Result document * @return array The document with fields as array */ public function modifyResultDocument(ResultsCommand $resultCommand, array $resultDocument) { $this->search = $resultCommand->getParentPlugin()->getSearchResultSetService()->getSearch(); // only check whether a BE user is logged in, don't need to check // for enabled score analysis as we wouldn't be here if it was disabled if ($GLOBALS['TSFE']->beUserLogin) { $configuration = Util::getSolrConfiguration(); $queryFields = $configuration->getSearchQueryQueryFields(); $debugData = $this->search->getDebugResponse()->explain->{$resultDocument['id']}; $scoreCalculationService = GeneralUtility::makeInstance(ScoreCalculationService::class); $resultDocument['score_analysis'] = $scoreCalculationService->getRenderedScores($debugData, $queryFields); } return $resultDocument; }