class FuzzysearchSearch in Fuzzy Search 7
Processor to set the index and search settings. Requires FuzzySearchService.
Hierarchy
- class \SearchApiAbstractProcessor implements SearchApiProcessorInterface
- class \FuzzysearchSearch
Expanded class hierarchy of FuzzysearchSearch
1 string reference to 'FuzzysearchSearch'
File
- includes/
processor_search.inc, line 6
View source
class FuzzysearchSearch extends SearchApiAbstractProcessor {
/**
* Checks whether this processor is applicable for a certain index.
*
* @param \SearchApiIndex $index
* The index to check for.
*
* @return bool
* TRUE if the processor can run on the given index; FALSE otherwise.
*/
public function supportsIndex(SearchApiIndex $index) {
return $index
->server() && $index
->server()
->supportsFeature('fuzzysearch');
}
/**
* Return the processor configuration form.
*
* @return array
* Associative configuration form array.
*/
public function configurationForm() {
$form['ngram_length'] = array(
'#type' => 'select',
'#title' => t('Ngram length'),
'#description' => t('Choose 3 unless you have special requirements.'),
'#options' => drupal_map_assoc(array(
2,
3,
4,
5,
6,
)),
'#default_value' => 3,
);
$form['missing_letters'] = array(
'#type' => 'select',
'#title' => t('Assume missing letters in search terms'),
'#description' => t('A search term as entered by a user may be missing letters. Up to how many missing letters in the term do you want to assume? 0 means the term will not return longer words in the results. 1 means a 4 letter search term will also check 5 letters words in the index.'),
'#options' => drupal_map_assoc(range(0, 50)),
'#default_value' => 5,
);
$form['extra_letters'] = array(
'#type' => 'select',
'#title' => t('Assume extra letters in search terms'),
'#description' => t('A search term as entered by a user may have extra letters. Up to how many extra letters in the term do you want to assume? 0 means the term will not return shorter words in the results. 1 means a 4 letter search term will also check 3 letters words in the index.'),
'#options' => drupal_map_assoc(range(0, 50)),
'#default_value' => 5,
);
$form['completeness'] = array(
'#type' => 'textfield',
'#title' => t('Minimum completeness'),
'#size' => 3,
'#maxlength' => 3,
'#description' => t('Enter a value between 0 and 100 to set the minimum match completeness required in the returned results.'),
'#default_value' => 40,
);
$form['sort_score'] = array(
'#type' => 'checkbox',
'#title' => t('Sort by score'),
'#description' => t('If selected, the results will be sorted by score first and completeness second, which can make tag scores even more important. The default is to sort by completeness first.'),
'#default_value' => FALSE,
);
if (!empty($this->options)) {
$form['ngram_length']['#default_value'] = $this->options['ngram_length'];
$form['missing_letters']['#default_value'] = $this->options['missing_letters'];
$form['extra_letters']['#default_value'] = $this->options['extra_letters'];
$form['completeness']['#default_value'] = $this->options['completeness'];
$form['sort_score']['#default_value'] = $this->options['sort_score'];
}
return $form;
}
/**
* Validate the processor configuration form values.
*
* @param array $form
* The form array.
* @param array $values
* The form values to validate.
* @param array $form_state
* The form state.
*/
public function configurationFormValidate(array $form, array &$values, array &$form_state) {
if (!is_numeric($values['completeness']) || $values['completeness'] < 0 || $values['completeness'] > 100) {
form_error($form['completeness'], t('The Fuzzy Search processor completeness setting must use a numeric value between 0 and 100.'));
}
}
/**
* {@inheritdoc}
*/
public function preprocessIndexItems(array &$items) {
foreach ($items as &$item) {
foreach ($item as $name => &$field) {
if ($this
->testField($name, $field)) {
$this
->split($field);
}
}
}
}
/**
* Split token fields into ngrams.
*
* @param array $field
* Associative array of field information.
*/
protected function split(array &$field) {
if ($field['type'] == 'tokens') {
foreach ($field['value'] as $key => $value) {
$word = '';
$word = $value['value'];
$length = drupal_strlen($word);
if ($length < $this->options['ngram_length']) {
continue;
}
// Cleanse and remove spaces.
$ngrams = array(
'ngrams' => array(),
);
$word = str_replace(' ', '', fuzzysearch_cleanse($word));
$ngrams['completeness'] = $length - $this->options['ngram_length'] + 1 == 0 ? 100 : number_format(100 / ($length - $this->options['ngram_length'] + 1), 3);
$ngrams['comp_min'] = number_format(100 / ($length - $this->options['ngram_length'] + 1 + $this->options['missing_letters']), 3);
if ($length - $this->options['ngram_length'] + 1 - $this->options['extra_letters'] <= 0) {
$ngrams['comp_max'] = number_format(100, 3) + 0.001;
}
else {
$ngrams['comp_max'] = number_format(100 / ($length - $this->options['ngram_length'] + 1 - $this->options['extra_letters']), 3) + 0.001;
}
// Split word into ngrams.
for ($i = 0; $i < $length - $this->options['ngram_length'] + 1; $i++) {
$ngrams['ngrams'][] = drupal_substr($word, $i, $this->options['ngram_length']);
}
$field['value'][$key] += $ngrams;
$i = 9;
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FuzzysearchSearch:: |
public | function |
Return the processor configuration form. Overrides SearchApiAbstractProcessor:: |
|
FuzzysearchSearch:: |
public | function |
Validate the processor configuration form values. Overrides SearchApiAbstractProcessor:: |
|
FuzzysearchSearch:: |
public | function |
Calls processField() for all appropriate fields. Overrides SearchApiAbstractProcessor:: |
|
FuzzysearchSearch:: |
protected | function | Split token fields into ngrams. | |
FuzzysearchSearch:: |
public | function |
Checks whether this processor is applicable for a certain index. 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 |
Does nothing. Overrides SearchApiProcessorInterface:: |
2 |
SearchApiAbstractProcessor:: |
public | function |
Calls processKeys() for the keys and processFilters() for the filters. Overrides SearchApiProcessorInterface:: |
1 |
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. | |
SearchApiAbstractProcessor:: |
public | function |
Constructor, saving its arguments into properties. Overrides SearchApiProcessorInterface:: |
2 |