You are here

search_api_elasticsearch.module in Search API Elasticsearch 7

Same filename and directory in other branches
  1. 7.2 search_api_elasticsearch.module

Provides an elasticsearch-based service class for the Search API.

File

search_api_elasticsearch.module
View source
<?php

/**
 * @file
 * Provides an elasticsearch-based service class for the Search API.
 */

/**
 * Implements hook_menu().
 */
function search_api_elasticsearch_menu() {
  $items['admin/config/search/search_api/index/%search_api_index/analysis'] = array(
    'title' => 'Analysis',
    'description' => 'Configure Elasticsearch analysis',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'search_api_elasticsearch_analysis_form',
      5,
    ),
    'access arguments' => array(
      'administer search_api',
    ),
    'file' => 'includes/search_api_elasticsearch.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE | MENU_CONTEXT_PAGE,
  );
  return $items;
}

/**
 * Implements hook_views_api().
 */
function search_api_elasticsearch_views_api() {
  return array(
    'api' => '3.0',
  );
}

/**
 * Get server ID by name.
 *
 * @param integer $server_machine_name
 */
function search_api_elasticsearch_get_server_id_by_name($server_machine_name) {
  $query = '';
  $result = '';
  try {
    $query = db_select('search_api_server', 'sas');
    $query
      ->addField('sas', 'id');
    $query
      ->condition('sas.machine_name', $server_machine_name, '=');
    $result = $query
      ->execute()
      ->fetchAssoc();
  } catch (Exception $e) {
    watchdog('Elasticsearch', $e
      ->getMessage(), array(), WATCHDOG_ERROR);
    return FALSE;
  }
  if (isset($result) && !empty($result)) {
    return reset($result);
  }
  else {
    return FALSE;
  }
}

/**
 * Ajax callback.
 *
 * @param array $form
 * @param array $form_state
 *
 * @return array
 */
function search_api_elasticsearch_ajax_callback($form, &$form_state) {
  return $form['options'];
}

/**
 * Attach the Ajax attributes.
 *
 * @param array $form
 */
function search_api_elasticsearch_attach_ajax_callback(&$form) {
  $form['options']['#prefix'] = '<div id="elasticsearch-add-index">';
  $form['options']['#suffix'] = '</div>';
  $form['server']['#ajax'] = array(
    'callback' => 'search_api_elasticsearch_ajax_callback',
    'wrapper' => 'elasticsearch-add-index',
    'method' => 'replace',
    'effect' => 'fade',
  );
}

/**
 * Submit handler
 * @param  array  $form
 * @param  array  $form_state
 */
function search_api_elasticsearch_edit_index_submit(array $form, array &$form_state) {
  form_state_values_clean($form_state);
  $merge_types = array();
  $elastic_server_id = search_api_elasticsearch_get_server_id_by_name($form_state['index']->server);
  $elastic_server = search_api_server_load($elastic_server_id);
  $index = $form_state['index'];
  $elastic_server
    ->updateSettings($index, $merge_types);
}

/**
 * Submit handler
 * @param  array  $form
 * @param  array  $form_state
 */
function search_api_elasticsearch_add_index_submit(array $form, array &$form_state) {
  form_state_values_clean($form_state);
}

/**
 * Ajax callback for adding more nodes.
 *
 * @param array $form
 * @param array $form_state
 */
function _search_api_elasticsearch_configuration_form_ajax($form, &$form_state) {
  unset($form['options']['form']['add_more']);
  if (module_exists('search_api_facetapi')) {
    unset($form['options']['form']['facet_settings']);
  }
  return $form['options']['form'];
}

/**
 * Ajax callback for remove nodes.
 * @param array $form
 * @param array $form_state
 */
function _search_api_elasticsearch_configuration_form_remove_ajax($form, &$form_state) {
  unset($form['options']['form']['add_more']);
  if (module_exists('search_api_facetapi')) {
    unset($form['options']['form']['facet_settings']);
  }
  return $form['options']['form'];
}

/**
 * Submit Handler for the configuration form.
 *
 * @param array $form
 * @param array $form_state
 */
function _search_api_elasticsearch_configuration_form_submit_custom($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
}

/**
 * Submit handler on removing elements
 *
 * @param array $form
 * @param array $form_state
 */
function _search_api_elasticsearch_configuration_form_remove_custom($form, &$form_state) {
  $form_state['values']['remove_delta'] == 'none';
  if (isset($form_state['triggering_element']['#remove_delta'])) {
    $form_state['values']['remove_delta'] = $form_state['triggering_element']['#remove_delta'];
  }
  $form_state['rebuild'] = TRUE;
}