Skip to content

Commit 04e2a93

Browse files
becnignatov
bec
authored andcommitted
Fixing issue 2192625.
1 parent b641259 commit 04e2a93

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

modules/elasticsearch_connector_search_api/elasticsearch_connector_search_api.install

+34
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,37 @@ function elasticsearch_connector_search_api_uninstall() {
3030
->execute();
3131
}
3232
}
33+
34+
/**
35+
* Update Search API servers and indexes that use ElasticSearch.
36+
*/
37+
function elasticsearch_connector_search_api_update_7001() {
38+
if (!function_exists('search_api_server_load_multiple')) {
39+
return t('No Search API servers to update.');
40+
}
41+
42+
$servers = search_api_server_load_multiple(FALSE);
43+
$indexes = search_api_index_load_multiple(FALSE);
44+
45+
foreach ($indexes as $index) {
46+
// If the index uses a server with our service class, then the server
47+
// configuration needs to be updated.
48+
if ($index->server && $servers[$index->server]->class == 'search_api_elasticsearch_connector') {
49+
// Duplicate the old method of setting the ElasticSearch index name:
50+
// concatenate the database name and the index machine name with a set
51+
// prefix.
52+
global $databases;
53+
$site_database = $databases['default']['default']['database'];
54+
$cluster_index_name = preg_replace('/[^A-Za-z0-9_]+/', '', 'elasticsearch_index_' . $site_database . '_' . $index->machine_name);
55+
56+
// If the server's cluster index name isn't set yet, update it.
57+
if (empty($index->options['index_name'])) {
58+
$server->options['cluster_index_name'] = $cluster_index_name;
59+
$index->options['index_name'] = $cluster_index_name;
60+
$index->save();
61+
}
62+
}
63+
}
64+
65+
return t('Updated Search API servers and indexes that use ElasticSearch. You may need to re-export Search API configuration features.');
66+
}

modules/elasticsearch_connector_search_api/elasticsearch_connector_search_api.module

+19
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@ function elasticsearch_connector_search_api_get_server_id_by_name($server_machin
151151
* @param array $default_values
152152
*/
153153
function elasticsearch_connector_search_api_return_form_options(&$form, &$form_state, $default_values = array(), $flag) {
154+
global $databases;
155+
154156
$default_options = $default_values + array(
155157
'number_of_shards' => 1,
156158
'number_of_replicas' => 0,
159+
'index_name' => $databases['default']['default']['database']
157160
);
158161

159162
$form['options']['number_of_shards'] = array(
@@ -169,6 +172,22 @@ function elasticsearch_connector_search_api_return_form_options(&$form, &$form_s
169172
'#size' => 4,
170173
'#title' => t('Number of replicas'),
171174
);
175+
176+
$form['options']['index_name'] = array(
177+
'#type' => 'textfield',
178+
'#title' => t('Index name'),
179+
'#required' => TRUE,
180+
'#element_validate' => array('elasticsearch_connector_search_api_index_name_validate'),
181+
'#default_value' => $default_options['index_name'],
182+
'#description' => t('The name of the ElasticSearch index to use. If this index does not already exist, it will be created.'),
183+
);
184+
}
185+
186+
function elasticsearch_connector_search_api_index_name_validate($element, &$form_state, $form) {
187+
// Make sure the index name contains appropriate characters.
188+
if (!preg_match('/^[a-z][a-z0-9_]*$/i', $element['#value'])) {
189+
form_error($element, t('Enter an index name that begins with a letter and contains only letters, numbers, and underscores.'));
190+
}
172191
}
173192

174193
/**

modules/elasticsearch_connector_search_api/service.inc

+1-14
Original file line numberDiff line numberDiff line change
@@ -726,21 +726,8 @@ class SearchApiElasticsearchConnector extends SearchApiAbstractService {
726726
*/
727727
public function getIndexName(SearchApiIndex $index) {
728728
global $databases;
729-
730729
$site_database = $databases['default']['default']['database'];
731-
732-
$index_machine_name = is_string($index) ? $index : $index->machine_name;
733-
734-
return self::escapeName('elasticsearch_index_' . $site_database . '_' . $index_machine_name);
735-
}
736-
737-
/**
738-
* Helper function. Escape a field or index name.
739-
*
740-
* Force names to be strictly alphanumeric-plus-underscore.
741-
*/
742-
public static function escapeName($name) {
743-
return preg_replace('/[^A-Za-z0-9_]+/', '', $name);
730+
return !empty($index->options['index_name']) ? $index->options['index_name'] : $site_database;
744731
}
745732

746733
/**

0 commit comments

Comments
 (0)