You are here

search_api_elasticsearch.module in Search API Elasticsearch 7.2

Same filename and directory in other branches
  1. 7 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.
 */
define('SEARCH_API_ELASTICSEARCH_DATE_FORMAT', 'Y-m-d\\TH:i:s.uP');

/**
 * Implements hook_entity_presave().
 */
function search_api_elasticsearch_entity_presave($entity, $type) {
  if ($type == 'search_api_index') {
    if ($entity->is_new) {
      SearchApiElasticsearchIndex::setCurrentIndexName($entity);
    }
  }
}

/**
 * Implements hook_menu().
 */
function search_api_elasticsearch_menu() {
  $items = [];
  return $items;
}

/**
 * Implements hook_search_api_service_info().
 */
function search_api_elasticsearch_search_api_service_info() {
  $services['search_api_elasticsearch_service'] = array(
    'name' => t('Elasticsearch'),
    'description' => t('
    <p>Index items using a !url_elasticsearch search server.</p>', array(
      '!url_elasticsearch' => '<a href="http://www.elasticsearch.org/">' . t('Elasticsearch') . '</a>',
    )),
    'class' => 'SearchApiElasticsearchService',
  );
  return $services;
}

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

/**
 * Get server ID by name.
 *
 * @param string $server_machine_name
 *
 * @return int
 */
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('Search API 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;
}