class ElasticsearchConnectorSearchApiHighlight in Elasticsearch Connector 7.2
Same name and namespace in other branches
- 7.5 modules/elasticsearch_connector_search_api/includes/processor_highlight.inc \ElasticsearchConnectorSearchApiHighlight
- 7 modules/elasticsearch_connector_search_api/includes/processor_highlight.inc \ElasticsearchConnectorSearchApiHighlight
@file Contains the SearchApiHighlight class.
Hierarchy
- class \SearchApiAbstractProcessor implements SearchApiProcessorInterface
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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ElasticsearchConnectorSearchApiHighlight:: |
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:: |
|
ElasticsearchConnectorSearchApiHighlight:: |
public | function |
Validation callback for the form returned by configurationForm(). Overrides SearchApiAbstractProcessor:: |
|
ElasticsearchConnectorSearchApiHighlight:: |
protected | function | Transform the response | |
ElasticsearchConnectorSearchApiHighlight:: |
public | function |
Does nothing. Overrides SearchApiAbstractProcessor:: |
|
ElasticsearchConnectorSearchApiHighlight:: |
public | function |
Calls processKeys() for the keys and processFilters() for the filters. Overrides SearchApiAbstractProcessor:: |
|
ElasticsearchConnectorSearchApiHighlight:: |
public | function |
Check whether this processor is applicable for a certain index. Overrides SearchApiAbstractProcessor:: |
|
ElasticsearchConnectorSearchApiHighlight:: |
public | function |
Constructor, saving its arguments into properties. Overrides SearchApiAbstractProcessor:: |
|
SearchApiAbstractProcessor:: |
protected | property | ||
SearchApiAbstractProcessor:: |
protected | property | ||
SearchApiAbstractProcessor:: |
public | function |
Submit callback for the form returned by configurationForm(). Overrides SearchApiProcessorInterface:: |
|
SearchApiAbstractProcessor:: |
protected | function | Internal helper function for imploding tokens into a single string. | |
SearchApiAbstractProcessor:: |
protected | function | Internal helper function for normalizing tokens. | |
SearchApiAbstractProcessor:: |
public | function |
Calls processField() for all appropriate fields. Overrides SearchApiProcessorInterface:: |
|
SearchApiAbstractProcessor:: |
protected | function | Function that is ultimately called for all text by the standard implementation, and does nothing by default. | 5 |
SearchApiAbstractProcessor:: |
protected | function | Method for preprocessing field data. | |
SearchApiAbstractProcessor:: |
protected | function | Called for processing a single text element in a field. The default implementation just calls process(). | 2 |
SearchApiAbstractProcessor:: |
protected | function | Method for preprocessing query filters. | |
SearchApiAbstractProcessor:: |
protected | function | Called for processing a single filter value. The default implementation just calls process(). | |
SearchApiAbstractProcessor:: |
protected | function | Called for processing a single search keyword. The default implementation just calls process(). | |
SearchApiAbstractProcessor:: |
protected | function | Method for preprocessing search keys. | |
SearchApiAbstractProcessor:: |
protected | function | Determines whether to process data from the given field. | |
SearchApiAbstractProcessor:: |
protected | function | Determines whether fields of the given type should normally be processed. |