public function testGetHighlighting() { $hlt = $this->query->getHighlighting(); $this->assertEquals('Solarium\\QueryType\\Select\\Query\\Component\\Highlighting\\Highlighting', get_class($hlt)); }
/** * Add highlighting fields to the solarium query * * @param Query $solarium_query * @param array $highlighting_parameters */ public function add_highlighting_fields(Query $solarium_query, $highlighting_parameters) { // Field names $field_names = isset($highlighting_parameters[self::PARAMETER_HIGHLIGHTING_FIELD_NAMES]) ? $highlighting_parameters[self::PARAMETER_HIGHLIGHTING_FIELD_NAMES] : array(WpSolrSchema::_FIELD_NAME_TITLE, WpSolrSchema::_FIELD_NAME_CONTENT, WpSolrSchema::_FIELD_NAME_COMMENTS); // Fragment size $fragment_size = isset($highlighting_parameters[self::PARAMETER_HIGHLIGHTING_FRAGMENT_SIZE]) ? $highlighting_parameters[self::PARAMETER_HIGHLIGHTING_FRAGMENT_SIZE] : self::DEFAULT_HIGHLIGHTING_FRAGMENT_SIZE; // Prefix $prefix = isset($highlighting_parameters[self::PARAMETER_HIGHLIGHTING_PREFIX]) ? $highlighting_parameters[self::PARAMETER_HIGHLIGHTING_PREFIX] : self::DEFAULT_HIGHLIGHTING_PREFIX; // Postfix $postfix = isset($highlighting_parameters[self::PARAMETER_HIGHLIGHTING_POSTFIX]) ? $highlighting_parameters[self::PARAMETER_HIGHLIGHTING_POSTFIX] : self::DEFAULT_HIGHLIGHTING_POSFIX; $highlighting = $solarium_query->getHighlighting(); foreach ($field_names as $field_name) { $highlighting->getField($field_name)->setSimplePrefix($prefix)->setSimplePostfix($postfix); // Max size of each highlighting fragment for post content $highlighting->getField($field_name)->setFragSize($fragment_size); } }
/** * Sets up $query’s highlighting according to TypoScript settings. * Unicode Private Use Area Codepoints U+EEEE and U+EEEF are used to mark * the highlight to better deal with field contents that contain markup * themselves. * * @param \Solarium\QueryType\Select\Query\Query $query * @param array $arguments request arguments */ protected function addHighlighting($arguments) { $highlightConfig = SettingsUtility::getMergedSettings('highlight', $this->settings); if ($highlightConfig && $highlightConfig['fields'] && count($highlightConfig['fields']) > 0) { $highlight = $this->query->getHighlighting(); // Configure highlight queries. if ($highlightConfig['query']) { $queryWords = []; if ($highlightConfig['useQueryTerms'] && array_key_exists('q', $arguments)) { $queryParameters = $arguments['q']; foreach ($this->settings['queryFields'] as $fieldInfo) { $fieldID = $fieldInfo['id']; if ($fieldID && $queryParameters[$fieldID]) { $queryArguments = $queryParameters[$fieldID]; $queryAlternate = null; if (is_array($queryArguments) && array_key_exists('alternate', $queryArguments) && array_key_exists('queryAlternate', $fieldInfo)) { $queryAlternate = $queryArguments['alternate']; if (array_key_exists('term', $queryArguments)) { $queryTerms = $queryArguments['term']; } } else { $queryTerms = $queryArguments; } if (!is_array($queryTerms)) { $queryTerms = [$queryTerms]; } foreach ($queryTerms as $queryTerm) { if (!$fieldInfo['noescape']) { if ($fieldInfo['phrase']) { $queryTerm = $this->query->getHelper()->escapePhrase($queryTerm); } else { $queryTerm = $this->query->getHelper()->escapeTerm($queryTerm); } } $queryWords[] = $queryTerm; } } } } $queryWords = array_filter($queryWords); if ($highlightConfig['useFacetTerms']) { foreach ($this->getActiveFacets($arguments) as $facets) { foreach (array_keys($facets) as $facetTerm) { $queryWords[] = $this->query->getHelper()->escapePhrase($facetTerm); } } } $queryComponents = []; foreach ($queryWords as $queryWord) { $queryComponents[] = '(' . sprintf($highlightConfig['query'], $queryWord) . ')'; } $queryString = implode(' OR ', $queryComponents); $highlight->setQuery($queryString); } // Configure highlight fields. $highlight->addFields(implode(',', $highlightConfig['fields'])); // Configure the fragement length. $highlight->setFragSize($highlightConfig['fragsize']); // Set up alternative fields. if ($highlightConfig['alternateFields']) { foreach ($highlightConfig['alternateFields'] as $fieldName => $alternateFieldName) { $highlightField = $highlight->getField($fieldName); $highlightField->setAlternateField($alternateFieldName); } } // Set up prefix and postfix. $highlight->setSimplePrefix('\\ueeee'); $highlight->setSimplePostfix('\\ueeef'); } $this->setConfigurationValue('highlight', $highlightConfig); }
/** * Sets the highlighting parameters. * * (The $query parameter currently isn't used and only here for the potential * sake of subclasses.) * * @param \Drupal\search_api\Query\QueryInterface $query * The query object. * @param \Solarium\QueryType\Select\Query\Query $solarium_query * The Solarium select query object. */ public function setHighlighting(Query $solarium_query, QueryInterface $query, $highlight = true, $excerpt = true) { if ($excerpt || $highlight) { $hl = $solarium_query->getHighlighting(); $hl->setFields('spell'); $hl->setSimplePrefix('[HIGHLIGHT]'); $hl->setSimplePostfix('[/HIGHLIGHT]'); $hl->setSnippets(3); $hl->setFragSize(70); $hl->setMergeContiguous(TRUE); } if ($highlight) { $hl = $solarium_query->getHighlighting(); $hl->setFields('tm_*'); $hl->setSnippets(1); $hl->setFragSize(0); if (!empty($this->configuration['excerpt'])) { // If we also generate a "normal" excerpt, set the settings for the // "spell" field (which we use to generate the excerpt) back to the // above values. $hl->getField('spell')->setSnippets(3); $hl->getField('spell')->setFragSize(70); // It regrettably doesn't seem to be possible to set hl.fl to several // values, if one contains wild cards (i.e., "t_*,spell" wouldn't work). $hl->setFields('*'); } } }