You are here

class ElasticsearchConnectorSearchApiHighlight in Elasticsearch Connector 7.2

Same name and namespace in other branches
  1. 7.5 modules/elasticsearch_connector_search_api/includes/processor_highlight.inc \ElasticsearchConnectorSearchApiHighlight
  2. 7 modules/elasticsearch_connector_search_api/includes/processor_highlight.inc \ElasticsearchConnectorSearchApiHighlight

@file Contains the SearchApiHighlight class.

Hierarchy

Expanded class hierarchy of ElasticsearchConnectorSearchApiHighlight

1 string reference to 'ElasticsearchConnectorSearchApiHighlight'
elasticsearch_connector_search_api_search_api_processor_info in modules/elasticsearch_connector_search_api/elasticsearch_connector_search_api.module
Implements hook_search_api_processor_info().

File

modules/elasticsearch_connector_search_api/includes/processor_highlight.inc, line 7
Contains the SearchApiHighlight class.

View source
class ElasticsearchConnectorSearchApiHighlight extends SearchApiAbstractProcessor {

  /**
   * {@inheritdoc}
   */
  public function __construct(SearchApiIndex $index, array $options = array()) {
    parent::__construct($index, $options);
  }

  /**
   * {@inheritdoc}
   */
  public function supportsIndex(SearchApiIndex $index) {
    $server = $index
      ->server();
    if (!empty($server) && $server->class == 'search_api_elasticsearch_connector') {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function configurationForm() {
    $this->options += array(
      'prefix' => '<strong>',
      'suffix' => '</strong>',
      'excerpt' => TRUE,
      'excerpt_length' => 256,
      'excerpt_fragments' => 3,
      'concatenator' => '...',
      'highlight' => 'always',
    );
    $form['prefix'] = array(
      '#type' => 'textfield',
      '#title' => t('Highlighting prefix'),
      '#description' => t('Text/HTML that will be prepended to all occurrences of search keywords in highlighted text.'),
      '#default_value' => $this->options['prefix'],
    );
    $form['suffix'] = array(
      '#type' => 'textfield',
      '#title' => t('Highlighting suffix'),
      '#description' => t('Text/HTML that will be appended to all occurrences of search keywords in highlighted text.'),
      '#default_value' => $this->options['suffix'],
    );
    $form['excerpt'] = array(
      '#type' => 'checkbox',
      '#title' => t('Create excerpt'),
      '#description' => t('When enabled, an excerpt will be created for searches with keywords, containing all occurrences of keywords in a fulltext field.'),
      '#default_value' => $this->options['excerpt'],
    );
    $form['excerpt_length'] = array(
      '#type' => 'textfield',
      '#title' => t('Excerpt length'),
      '#description' => t('The requested length of the excerpt, in characters. This is equivalent of Elasticsearch\'s fragment_size option.'),
      '#default_value' => $this->options['excerpt_length'],
      '#element_validate' => array(
        'element_validate_integer_positive',
      ),
      '#states' => array(
        'visible' => array(
          '#edit-processors-search-api-highlighting-settings-excerpt' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['excerpt_fragments'] = array(
      '#type' => 'textfield',
      '#title' => t('Excerpt fragments'),
      '#description' => t('The number of highlighted excerpt fragments.'),
      '#default_value' => $this->options['excerpt_fragments'],
      '#element_validate' => array(
        'element_validate_integer_positive',
      ),
      '#states' => array(
        'visible' => array(
          '#edit-processors-search-api-highlighting-settings-excerpt' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['concatenator'] = array(
      '#type' => 'textfield',
      '#title' => t('Concatenator'),
      '#description' => t('The concatenator between different excerpt fragments used for both the excerpt and fields highlighting.'),
      '#default_value' => $this->options['concatenator'],
    );
    $form['highlight'] = array(
      '#type' => 'select',
      '#title' => t('Highlight returned field data'),
      '#description' => t('Select whether returned fields should be highlighted.'),
      '#options' => array(
        'always' => t('Always'),
        'server' => t('If the server returns fields'),
        'never' => t('Never'),
      ),
      '#default_value' => $this->options['highlight'],
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function configurationFormValidate(array $form, array &$values, array &$form_state) {

    // Overridden so $form['fields'] is not checked.
  }

  /**
   * {@inheritdoc}
   */
  public function postprocessSearchResults(array &$response, SearchApiQuery $query) {
    if (!$this
      ->supportsIndex($query
      ->getIndex())) {
      throw new SearchApiException("This processor is only available for Elasticsearch connector search api implementation.");
    }
    if (!empty($response['results'])) {
      foreach ($response['results'] as $id => &$result) {
        $result = $this
          ->parseHighlightFields($result, $query);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function preprocessSearchQuery(SearchApiQuery $query) {
    if (!$this
      ->supportsIndex($query
      ->getIndex())) {
      throw new SearchApiException("This processor is only available for Elasticsearch connector search api implementation.");
    }
    $fields = $query
      ->getFields();
    if (!empty($fields)) {
      $params['highlight']['pre_tags'] = array(
        $this->options['prefix'],
      );
      $params['highlight']['post_tags'] = array(
        $this->options['suffix'],
      );
      $params['highlight']['fragment_size'] = $this->options['excerpt_length'];
      $params['highlight']['number_of_fragments'] = $this->options['excerpt_fragments'];
      foreach ($fields as $field) {
        $params['highlight']['fields'][$field] = new stdClass();
      }
      $elasticsearch_processors_params = $query
        ->getOption('elasticsearch_processors_params', array());
      $elasticsearch_processors_params['highlight'] = $params['highlight'];
      $query
        ->setOption('elasticsearch_processors_params', $elasticsearch_processors_params);
    }
  }

  /**
   * Transform the response
   * @param array $result
   * @param SearchApiQueryInterface $query
   * @return array $result
   */
  protected function parseHighlightFields($result, SearchApiQueryInterface $query) {
    $fields = $query
      ->getFields();
    $result_fields =& $result['fields'];
    if (!empty($result['highlight']) && !empty($fields)) {
      foreach ($fields as $field) {
        if (isset($result_fields[$field]) && !empty($result['highlight'][$field])) {
          if ($this->options['highlight'] != 'never') {
            $result_fields[$field] = implode($this->options['concatenator'], $result['highlight'][$field]);
          }
          if ($this->options['excerpt']) {
            if (!isset($result['excerpt'])) {
              $result['excerpt'] = implode($this->options['concatenator'], $result['highlight'][$field]);
            }
            else {
              $result['excerpt'] .= $this->options['concatenator'] . implode($this->options['concatenator'], $result['highlight'][$field]);
            }
          }
        }
      }
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ElasticsearchConnectorSearchApiHighlight::configurationForm public function Display a form for configuring this processor. Since forcing users to specify options for disabled processors makes no sense, none of the form elements should have the '#required' attribute set. Overrides SearchApiAbstractProcessor::configurationForm
ElasticsearchConnectorSearchApiHighlight::configurationFormValidate public function Validation callback for the form returned by configurationForm(). Overrides SearchApiAbstractProcessor::configurationFormValidate
ElasticsearchConnectorSearchApiHighlight::parseHighlightFields protected function Transform the response
ElasticsearchConnectorSearchApiHighlight::postprocessSearchResults public function Does nothing. Overrides SearchApiAbstractProcessor::postprocessSearchResults
ElasticsearchConnectorSearchApiHighlight::preprocessSearchQuery public function Calls processKeys() for the keys and processFilters() for the filters. Overrides SearchApiAbstractProcessor::preprocessSearchQuery
ElasticsearchConnectorSearchApiHighlight::supportsIndex public function Check whether this processor is applicable for a certain index. Overrides SearchApiAbstractProcessor::supportsIndex
ElasticsearchConnectorSearchApiHighlight::__construct public function Constructor, saving its arguments into properties. Overrides SearchApiAbstractProcessor::__construct
SearchApiAbstractProcessor::$index protected property
SearchApiAbstractProcessor::$options protected property
SearchApiAbstractProcessor::configurationFormSubmit public function Submit callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface::configurationFormSubmit
SearchApiAbstractProcessor::implodeTokens protected function Internal helper function for imploding tokens into a single string.
SearchApiAbstractProcessor::normalizeTokens protected function Internal helper function for normalizing tokens.
SearchApiAbstractProcessor::preprocessIndexItems public function Calls processField() for all appropriate fields. Overrides SearchApiProcessorInterface::preprocessIndexItems
SearchApiAbstractProcessor::process protected function Function that is ultimately called for all text by the standard implementation, and does nothing by default. 5
SearchApiAbstractProcessor::processField protected function Method for preprocessing field data.
SearchApiAbstractProcessor::processFieldValue protected function Called for processing a single text element in a field. The default implementation just calls process(). 2
SearchApiAbstractProcessor::processFilters protected function Method for preprocessing query filters.
SearchApiAbstractProcessor::processFilterValue protected function Called for processing a single filter value. The default implementation just calls process().
SearchApiAbstractProcessor::processKey protected function Called for processing a single search keyword. The default implementation just calls process().
SearchApiAbstractProcessor::processKeys protected function Method for preprocessing search keys.
SearchApiAbstractProcessor::testField protected function Determines whether to process data from the given field.
SearchApiAbstractProcessor::testType protected function Determines whether fields of the given type should normally be processed.