You are here

function search_api_elasticsearch_analysis_form in Search API Elasticsearch 7

Form constructor for the Elasticsearch analysis configuration form.

Parameters

SearchApiIndex $index: The index for which analysis options should be configured.

1 string reference to 'search_api_elasticsearch_analysis_form'
search_api_elasticsearch_menu in ./search_api_elasticsearch.module
Implements hook_menu().

File

includes/search_api_elasticsearch.admin.inc, line 16
Admin page callbacks for the Search API Elasticsearch module.

Code

function search_api_elasticsearch_analysis_form($form, &$form_state, SearchApiIndex $index) {
  $server = $index
    ->server();
  $service_info = search_api_get_service_info();

  // We only want to work with Elasticsearch servers.
  $reflection = new ReflectionClass($service_info[$server->class]['class']);
  if ($reflection
    ->isSubclassOf('SearchApiElasticsearchAbstractService')) {
    $elasticsearch_client = new $service_info[$server->class]['class']($server);
    $analysis_settings = $elasticsearch_client
      ->getAnalysisSettings($index);

    // Build our form.
    $form['default'] = array(
      '#title' => t("Default analyzer"),
      '#type' => 'select',
      '#description' => t("The default analyzer for all fields of this index. You do not need to enable a custom analyzer below to configure a default analyzer."),
      '#options' => array(
        0 => t('-Select-'),
        t('Built-in') => drupal_map_assoc(array(
          'standard',
          'simple',
          'whitespace',
          'stop',
          'keyword',
          'pattern',
          'snowball',
        )),
        t('Language') => drupal_map_assoc(array(
          'arabic',
          'armenian',
          'basque',
          'brazilian',
          'bulgarian',
          'catalan',
          'cjk',
          'czech',
          'danish',
          'dutch',
          'english',
          'finnish',
          'french',
          'galician',
          'german',
          'greek',
          'hindi',
          'hungarian',
          'indonesian',
          'irish',
          'italian',
          'latvian',
          'lithuanian',
          'norwegian',
          'persian',
          'portuguese',
          'romanian',
          'russian',
          'sorani',
          'spanish',
          'swedish',
          'turkish',
          'thai',
        )),
      ),
      '#default_value' => isset($analysis_settings['analyzer']['default']) ? $analysis_settings['analyzer']['default']['type'] : 0,
    );
    $form['analyzer'] = array(
      '#title' => t('Analyzers'),
      '#type' => 'fieldset',
      '#description' => t('Select the analyzers to customize on this index. See <a href="@elasticsearch_analyzers_url">Elasticsearch Analyzers</a>.', array(
        '@elasticsearch_analyzers_url' => url('http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/analysis-analyzers.html'),
      )),
      '#collapsible' => FALSE,
      '#tree' => TRUE,
    );
    $form['analyzer']['standard'] = array(
      '#title' => t('Standard'),
      '#type' => 'fieldset',
      '#description' => t('An analyzer built using the Standard Tokenizer with the Standard Token Filter, Lower Case Token Filter, and Stop Token Filter.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );
    $form['analyzer']['standard']['enabled'] = array(
      '#title' => t('Enabled'),
      '#type' => 'checkbox',
      '#default_value' => isset($analysis_settings['analyzer']['standard']) ? 1 : 0,
    );
    $form['analyzer']['standard']['stopwords'] = array(
      '#title' => t('Stopwords'),
      '#type' => 'textarea',
      '#description' => t('A list of stopwords to initialize the stop filter with. Defaults to an empty stopword list.'),
      '#default_value' => !empty($analysis_settings['analyzer']['standard']['stopwords']) ? $analysis_settings['analyzer']['standard']['stopwords'] : '',
    );
    $form['analyzer']['standard']['max_token_length'] = array(
      '#title' => t('Maximum Token Length'),
      '#type' => 'textfield',
      '#description' => t('The maximum token length. If a token is seen that exceeds this length then it is discarded. Defaults to 255.'),
      '#default_value' => !empty($analysis_settings['analyzer']['standard']['max_token_length']) ? $analysis_settings['analyzer']['standard']['max_token_length'] : '',
    );
    $form['analyzer']['simple'] = array(
      '#title' => t('Simple'),
      '#type' => 'fieldset',
      '#description' => t('An analyzer built using a Lower Case Tokenizer.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );
    $form['analyzer']['simple']['enabled'] = array(
      '#title' => t('Enabled'),
      '#type' => 'checkbox',
      '#default_value' => isset($analysis_settings['analyzer']['simple']) ? 1 : 0,
    );
    $form['analyzer']['whitespace'] = array(
      '#title' => t('Whitespace'),
      '#type' => 'fieldset',
      '#description' => t('An analyzer built using a Whitespace Tokenizer.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );
    $form['analyzer']['whitespace']['enabled'] = array(
      '#title' => t('Enabled'),
      '#type' => 'checkbox',
      '#default_value' => isset($analysis_settings['analyzer']['whitespace']) ? 1 : 0,
    );
    $form['analyzer']['stop'] = array(
      '#title' => t('Stop'),
      '#type' => 'fieldset',
      '#description' => t('An analyzer built using a Lower Case Tokenizer, with Stop Token Filter.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );
    $form['analyzer']['stop']['enabled'] = array(
      '#title' => t('Enabled'),
      '#type' => 'checkbox',
      '#default_value' => isset($analysis_settings['analyzer']['stop']) ? 1 : 0,
    );
    $form['analyzer']['stop']['stopwords'] = array(
      '#title' => t('Stopwords'),
      '#type' => 'textarea',
      '#description' => t('A list of stopwords to initialize the stop filter with. Defaults to the English stopwords.'),
      '#default_value' => !empty($analysis_settings['analyzer']['stop']['stopwords']) ? $analysis_settings['analyzer']['stop']['stopwords'] : '',
    );
    $form['analyzer']['keyword'] = array(
      '#title' => t('Keyword'),
      '#type' => 'fieldset',
      '#description' => t('An analyzer that "tokenizes" an entire stream as a single token. This is useful for data like zip codes, ids and so on.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );
    $form['analyzer']['keyword']['enabled'] = array(
      '#title' => t('Enabled'),
      '#type' => 'checkbox',
      '#default_value' => isset($analysis_settings['analyzer']['keyword']) ? 1 : 0,
    );
    $form['analyzer']['pattern'] = array(
      '#title' => t('Pattern'),
      '#type' => 'fieldset',
      '#description' => t('An analyzer that can flexibly separate text into terms via a regular expression.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );
    $form['analyzer']['pattern']['enabled'] = array(
      '#title' => t('Enabled'),
      '#type' => 'checkbox',
      '#default_value' => isset($analysis_settings['analyzer']['pattern']) ? 1 : 0,
    );
    $form['analyzer']['pattern']['lowercase'] = array(
      '#title' => t('Lowercase'),
      '#type' => 'checkbox',
      '#default_value' => !empty($analysis_settings['analyzer']['pattern']['lowercase']) ? 1 : 0,
    );
    $form['analyzer']['pattern']['pattern'] = array(
      '#title' => t('Pattern'),
      '#type' => 'textarea',
      '#description' => t('The regular expression pattern. The regular expression should match the token separators, not the tokens themselves. Defaults to \\W+.'),
      '#default_value' => !empty($analysis_settings['analyzer']['pattern']['pattern']) ? $analysis_settings['analyzer']['pattern']['pattern'] : '',
    );
    $form['analyzer']['pattern']['flags'] = array(
      '#title' => t('Flags'),
      '#type' => 'textfield',
      '#description' => t('The regular expression flags. Flags should be pipe-separated, eg "CASE_INSENSITIVE|COMMENTS". Check Java Pattern API for more details about flags options.'),
      '#default_value' => !empty($analysis_settings['analyzer']['pattern']['flags']) ? $analysis_settings['analyzer']['pattern']['flags'] : '',
    );
    $form['analyzer']['pattern']['stopwords'] = array(
      '#title' => t('Stopwords'),
      '#type' => 'textarea',
      '#description' => t('A list of stopwords to initialize the stop filter with. Defaults to an empty stopwords list.'),
      '#default_value' => !empty($analysis_settings['analyzer']['pattern']['stopwords']) ? $analysis_settings['analyzer']['pattern']['stopwords'] : '',
    );

    // @TODO Determine proper way to implement language analyzers.

    /*$form['analyzer']['language'] = array(
        '#title' =>t('Language'),
        '#type' => 'fieldset',
        '#description' => t('An analyzer that can flexibly separate text into terms via a regular expression.'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#tree' => TRUE,
      );*/
    $form['analyzer']['snowball'] = array(
      '#title' => t('Snowball'),
      '#type' => 'fieldset',
      '#description' => t('An analyzer that uses the standard tokenizer, with standard filter, lowercase filter, stop filter, and snowball filter.'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#tree' => TRUE,
    );
    $form['analyzer']['snowball']['enabled'] = array(
      '#title' => t('Enabled'),
      '#type' => 'checkbox',
      '#default_value' => isset($analysis_settings['analyzer']['snowball']) ? 1 : 0,
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
      '#validate' => array(
        'search_api_elasticsearch_analysis_form_validate',
      ),
      '#submit' => array(
        'search_api_elasticsearch_analysis_form_submit',
      ),
    );

    // We need to pass certain things to the submit handler.
    $form['#SearchApiIndex'] = $index;
    $form['#SearchApiElasticsearchClient'] = $elasticsearch_client;
    return $form;
  }
}