Skip to content

Commit d7e97b4

Browse files
committed
Issue #2304617 by @becw: Allowing index update and change the index to be a dropdown instead of input. Aloowing the index to be created in a dialog box using the elasticsearch_connector module.
1 parent fde4261 commit d7e97b4

8 files changed

+213
-95
lines changed

css/ec-index.css

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ ul.index-dialog-links a {
1414
margin: 5px;
1515
}
1616

17-
ul.index-dialog-links li a {
17+
ul.index-dialog-links li a.ec-index-dialog-add {
1818
background: url('../img/plus.png') left no-repeat;
1919
}
2020

21+
ul.index-dialog-links li a.ec-index-dialog-edit {
22+
background: url('../img/edit.png') left no-repeat;
23+
}
24+

elasticsearch_connector.admin.inc

+68-22
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ function elasticsearch_connector_cluster_indices($cluster) {
9595

9696
$operations = theme('links__ctools_dropbutton', array(
9797
'links' => array(
98+
array('title' => t('Edit'), 'href' => elasticsearch_connector_main_settings_path() . '/clusters/' . $cluster->cluster_id . '/indices/' . $index_name . '/edit'),
9899
array('title' => t('Aliases'), 'href' => elasticsearch_connector_main_settings_path() . '/clusters/' . $cluster->cluster_id . '/indices/' . $index_name . '/aliases'),
99100
array('title' => t('Delete'), 'href' => elasticsearch_connector_main_settings_path() . '/clusters/' . $cluster->cluster_id . '/indices/' . $index_name . '/delete'),
100101
),
@@ -408,31 +409,51 @@ function elasticsearch_connector_edit_cluster($form, $form_state, $cluster = NUL
408409
* @param array $cluster
409410
* @return array
410411
*/
411-
function elasticsearch_connector_cluster_indices_add($form, &$form_state, $cluster) {
412+
function elasticsearch_connector_cluster_indices_add($form, &$form_state, $cluster, $index = NULL) {
412413
$form = array();
413414

414415
$form['#cluster'] = $cluster;
415416

417+
if (isset($index)) {
418+
$form['#index'] = $index;
419+
420+
$client = elasticsearch_connector_load_library($cluster);
421+
$settings = array();
422+
try {
423+
$settings = $client->indices()->getSettings(array(
424+
'index' => $index
425+
));
426+
427+
$settings = $settings[$index]['settings'];
428+
}
429+
catch (Exception $e) {
430+
watchdog('elasticsearch_connector', $e->getMessage(), array(), WATCHDOG_WARNING);
431+
}
432+
}
433+
416434
$form['index_name'] = array(
417435
'#type' => 'textfield',
418436
'#title' => t('Index name'),
419437
'#required' => TRUE,
420-
'#default_value' => '',
438+
'#disabled' => isset($index),
439+
'#default_value' => isset($index) ? $index : '',
421440
'#description' => t('Enter the index name.')
422441
);
423442

424443
$form['num_of_shards'] = array(
425444
'#type' => 'textfield',
426445
'#title' => t('Number of shards'),
427446
'#required' => TRUE,
428-
'#default_value' => '',
447+
'#disabled' => isset($index),
448+
'#default_value' => isset($settings['index']['number_of_shards']) ? $settings['index']['number_of_shards'] : '',
429449
'#description' => t('Enter the number of shards for the index.')
430450
);
431451

432452
$form['num_of_replica'] = array(
433453
'#type' => 'textfield',
434454
'#title' => t('Number of replica'),
435-
'#default_value' => '',
455+
'#required' => TRUE,
456+
'#default_value' => isset($settings['index']['number_of_replicas']) ? $settings['index']['number_of_replicas'] : '',
436457
'#description' => t('Enter the number of shards replicas.')
437458
);
438459

@@ -444,7 +465,7 @@ function elasticsearch_connector_cluster_indices_add($form, &$form_state, $clust
444465
'#type' => 'submit',
445466
'#validate' => array('elasticsearch_connector_cluster_indices_add_validate'),
446467
'#submit' => array('elasticsearch_connector_cluster_indices_add_submit'),
447-
'#value' => t('Save'),
468+
'#value' => (isset($index) ? t('Update') : t('Save')),
448469
);
449470

450471
return $form;
@@ -479,27 +500,52 @@ function elasticsearch_connector_cluster_indices_add_submit($form, &$form_state)
479500
$values = $form_state['values'];
480501
$cluster = $form['#cluster'];
481502
$client = elasticsearch_connector_load_library($cluster);
503+
$index = isset($form['#index']) ? $form['#index'] : FALSE;
482504
if ($client) {
483-
try {
484-
$index_params['index'] = $values['index_name'];
485-
$index_params['body']['settings']['number_of_shards'] = $values['num_of_shards'];
486-
$index_params['body']['settings']['number_of_replicas'] = $values['num_of_replica'];
487-
$response = $client->indices()->create($index_params);
488-
if (elasticsearch_connector_check_response_ack($response)) {
489-
drupal_set_message(t('The index %index has been successfully created.', array('%index' => $values['index_name'])));
505+
if (empty($index)) {
506+
try {
507+
$index_params['index'] = $values['index_name'];
508+
$index_params['body']['settings']['number_of_shards'] = $values['num_of_shards'];
509+
$index_params['body']['settings']['number_of_replicas'] = $values['num_of_replica'];
510+
$response = $client->indices()->create($index_params);
511+
if (elasticsearch_connector_check_response_ack($response)) {
512+
drupal_set_message(t('The index %index has been successfully created.', array('%index' => $values['index_name'])));
513+
}
514+
else {
515+
drupal_set_message(t('Fail to create the index %index', array('%index' => $values['index_name'])), 'error');
516+
}
517+
518+
// If the form has been opened in dialog, close the window if it was
519+
// setup to do so.
520+
if (elasticsearch_connector_in_dialog() && elasticsearch_connector_close_on_submit()) {
521+
elasticsearch_connector_close_on_redirect($cluster->cluster_id, $values['index_name']);
522+
}
490523
}
491-
else {
492-
drupal_set_message(t('Fail to create the index %index', array('%index' => $values['index_name'])), 'error');
493-
}
494-
495-
// If the form has been opened in dialog, close the window if it was
496-
// setup to do so.
497-
if (elasticsearch_connector_in_dialog() && elasticsearch_connector_close_on_submit()) {
498-
elasticsearch_connector_close_on_redirect($cluster->cluster_id, $values['index_name']);
524+
catch (Exception $e) {
525+
drupal_set_message($e->getMessage(), 'error');
499526
}
500527
}
501-
catch (Exception $e) {
502-
drupal_set_message($e->getMessage(), 'error');
528+
else {
529+
try {
530+
$index_params['index'] = $values['index_name'];
531+
$index_params['body']['settings']['number_of_replicas'] = $values['num_of_replica'];
532+
$response = $client->indices()->putSettings($index_params);
533+
if (elasticsearch_connector_check_response_ack($response)) {
534+
drupal_set_message(t('The index %index has been successfully updated.', array('%index' => $values['index_name'])));
535+
}
536+
else {
537+
drupal_set_message(t('Fail to update the index %index', array('%index' => $values['index_name'])), 'error');
538+
}
539+
540+
// If the form has been opened in dialog, close the window if it was
541+
// setup to do so.
542+
if (elasticsearch_connector_in_dialog() && elasticsearch_connector_close_on_submit()) {
543+
elasticsearch_connector_close_on_redirect($cluster->cluster_id, $values['index_name']);
544+
}
545+
}
546+
catch (Exception $e) {
547+
drupal_set_message($e->getMessage(), 'error');
548+
}
503549
}
504550
}
505551
}

elasticsearch_connector.module

+112-24
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ function elasticsearch_connector_menu() {
133133
'type' => MENU_LOCAL_ACTION,
134134
);
135135

136+
$items[$settings_path . '/clusters/%elasticsearch_connector_cluster/indices/%elasticsearch_connector_index/edit'] = array(
137+
'title' => 'Elasticsearch cluster indices',
138+
'description' => 'Elasticsearch cluster indices',
139+
'page callback' => 'drupal_get_form',
140+
'page arguments' => array('elasticsearch_connector_cluster_indices_add', 4, 6),
141+
'access arguments' => array('adminiser elasticsearch connector'),
142+
'load arguments' => array('%map', '%index'),
143+
'file' => 'elasticsearch_connector.admin.inc',
144+
);
145+
136146
$items[$settings_path . '/clusters/%elasticsearch_connector_cluster/indices/%elasticsearch_connector_index_valid/aliases'] = array(
137147
'title' => 'Elasticsearch cluster indices',
138148
'description' => 'Elasticsearch cluster indices',
@@ -211,6 +221,30 @@ function _elasticsearch_connector_ec_clusters_process($element, &$form_state, $f
211221
return $element;
212222
}
213223

224+
/**
225+
* Check if the index is valid and compare it with the indexes comming from Elasticsearch.
226+
*
227+
* @param string $index_name
228+
* @param array $map_array
229+
* @param int $index
230+
* @return string|boolean
231+
*/
232+
function elasticsearch_connector_index_load($index_name, $map_array, $index) {
233+
if (elasticsearch_connector_index_valid_load($index_name)) {
234+
$cluster = $map_array[4];
235+
$client = elasticsearch_connector_load_library($cluster);
236+
if (!empty($client)) {
237+
$indices = $client->indices()->stats();
238+
if (isset($indices['indices'][$index_name])) {
239+
return $index_name;
240+
}
241+
else {
242+
return FALSE;
243+
}
244+
}
245+
}
246+
}
247+
214248
/**
215249
* Attach required javascript for the ec_index element.
216250
* @return array
@@ -290,24 +324,32 @@ function _elasticsearch_connector_ec_index_process($element, &$form_state, $form
290324

291325
// TODO: Add icon if the cluster is OK or not.
292326

293-
$element['cluster_id'] = array(
294-
'#type' => 'select',
295-
'#id' => $element_id . '-cluster-id',
296-
'#title' => t('Select cluster'),
297-
'#required' => $element['#required'],
298-
'#default_value' => isset($element['#default_value'])
299-
&& is_array($element['#default_value'])
300-
&& isset($element['#default_value']['cluster_id'])
301-
? $element['#default_value']['cluster_id'] : '',
302-
// TODO: Allow this option to be overwritten and #value if we had such.
303-
'#description' => t('Select the cluster.'),
304-
'#ajax' => array(
305-
'callback' => '_elasticsearch_connector_ec_index_ajax',
306-
'wrapper' => $wrapper_id,
307-
'method' => 'replace',
308-
'effect' => 'fade',
309-
)
310-
);
327+
if (empty($element['#cluster_id'])) {
328+
$element['cluster_id'] = array(
329+
'#type' => 'select',
330+
'#id' => $element_id . '-cluster-id',
331+
'#title' => t('Select cluster'),
332+
'#required' => $element['#required'],
333+
'#default_value' => isset($element['#default_value'])
334+
&& is_array($element['#default_value'])
335+
&& isset($element['#default_value']['cluster_id'])
336+
? $element['#default_value']['cluster_id'] : '',
337+
// TODO: Allow this option to be overwritten and #value if we had such.
338+
'#description' => t('Select the cluster.'),
339+
'#ajax' => array(
340+
'callback' => '_elasticsearch_connector_ec_index_ajax',
341+
'wrapper' => $wrapper_id,
342+
'method' => 'replace',
343+
'effect' => 'fade',
344+
)
345+
);
346+
}
347+
else {
348+
$element['cluster_id'] = array(
349+
'#type' => 'value',
350+
'#value' => $element['#cluster_id']
351+
);
352+
}
311353

312354
if (!isset($element['cluster_id']['#current_path'])) {
313355
$element['cluster_id']['#current_path'] = current_path();
@@ -321,12 +363,13 @@ function _elasticsearch_connector_ec_index_process($element, &$form_state, $form
321363
}
322364

323365
// TODO: We need to handle the incomming tree name if such.
324-
$links = array();
366+
$links = $manage_indices = array();
325367
$index_options = array('' => t('Select index'));
326-
if (is_array($element['#value']) && !empty($element['#value']['cluster_id'])) {
368+
if ((is_array($element['#value']) && !empty($element['#value']['cluster_id'])) || !empty($element['#cluster_id'])) {
369+
$cluster_id = (is_array($element['#value']) && !empty($element['#value']['cluster_id'])) ? $element['#value']['cluster_id'] : $element['#cluster_id'];
327370
$index_options = array();
328371
try {
329-
$index_options = elasticsearch_connector_get_indices_options($element['#value']['cluster_id'], TRUE);
372+
$index_options = elasticsearch_connector_get_indices_options($cluster_id, TRUE);
330373
}
331374
catch (Exception $e) {
332375
if (!empty($element['#throw_exp'])) {
@@ -335,8 +378,35 @@ function _elasticsearch_connector_ec_index_process($element, &$form_state, $form
335378
}
336379
$links[] = array(
337380
'title' => t('Add index'),
338-
'href' => 'admin/config/elasticsearch-connector/clusters/' . $element['#value']['cluster_id'] . '/indices/add',
339-
'attributes' => array('target' => '_blank', 'class' => 'ec-index-dialog'),
381+
'href' => 'admin/config/elasticsearch-connector/clusters/' .$cluster_id . '/indices/add',
382+
'attributes' => array('target' => '_blank', 'class' => 'ec-index-dialog ec-index-dialog-add'),
383+
'query' => array(
384+
'render' => 'elasticsearch-connector-dialog',
385+
'index_element_id' => $element_id . '-index',
386+
'cluster_element_id' => $element_id . '-cluster-id'
387+
)
388+
);
389+
390+
$manage_indices = array(
391+
'title' => t('Manage indices'),
392+
'href' => 'admin/config/elasticsearch-connector/clusters/' .$cluster_id . '/indices',
393+
'attributes' => array('target' => '_blank', 'class' => 'manage-indices'),
394+
);
395+
}
396+
397+
$index = '';
398+
if (isset($element['#value']) && is_array($element['#value']) && isset($element['#value']['index'])) {
399+
$index = $element['#value']['index'];
400+
}
401+
elseif (isset($element['#default_value']) && is_array($element['#default_value']) && isset($element['#default_value']['index'])) {
402+
$index = $element['#default_value']['index'];
403+
}
404+
405+
if (!empty($index)) {
406+
$links[] = array(
407+
'title' => t('Edit @index index', array('@index' => $index)),
408+
'href' => 'admin/config/elasticsearch-connector/clusters/' .$cluster_id . '/indices/' . $index . '/edit',
409+
'attributes' => array('target' => '_blank', 'class' => 'ec-index-dialog ec-index-dialog-edit'),
340410
'query' => array(
341411
'render' => 'elasticsearch-connector-dialog',
342412
'index_element_id' => $element_id . '-index',
@@ -345,6 +415,10 @@ function _elasticsearch_connector_ec_index_process($element, &$form_state, $form
345415
);
346416
}
347417

418+
if (!empty($manage_indices)) {
419+
$links[] = $manage_indices;
420+
}
421+
348422
$element['index'] = array(
349423
'#type' => 'select',
350424
'#title' => t('Select index'),
@@ -356,8 +430,14 @@ function _elasticsearch_connector_ec_index_process($element, &$form_state, $form
356430
? $element['#default_value']['index'] : '',
357431
'#description' => t('Select the index.'),
358432
'#options' => $index_options,
433+
'#ajax' => array(
434+
'callback' => '_elasticsearch_connector_ec_index_links_ajax',
435+
'wrapper' => $element['#id'] . '-dialog-links',
436+
'method' => 'replace',
437+
'effect' => 'fade',
438+
),
359439
'#prefix' => '<div id="' . $wrapper_id . '">',
360-
'#suffix' =>'<div class="dialog-links ' . $element['#id'] . '">'
440+
'#suffix' =>'<div id="' . $element['#id'] . '-dialog-links" class="dialog-links ' . $element['#id'] . '">'
361441
. theme('links__es_index_links', array(
362442
'links' => $links,
363443
'attributes' => array('class' => 'index-dialog-links')
@@ -462,6 +542,14 @@ function _elasticsearch_connector_ec_index_ajax($form, $form_state) {
462542
return $index_element;
463543
}
464544

545+
function _elasticsearch_connector_ec_index_links_ajax($form, $form_state) {
546+
$element_name = $form_state['triggering_element']['#name'];
547+
$parents = $form_state['triggering_element']['#parents'];
548+
$index_element = drupal_array_get_nested_value($form, $parents);
549+
550+
return $index_element['#suffix'];
551+
}
552+
465553
/**
466554
* Return the main path of the elasticsearch connector module.
467555
* @return string

img/edit.png

229 Bytes
Loading

js/ec-index.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
// Trigger change to update the form cache.
1616
$('#' + settings.cluster_element_id).trigger('change');
17+
$('#' + settings.index_element_id).trigger('change');
1718
}
1819

1920
return false;

modules/elasticsearch_connector_search_api/elasticsearch_connector_search_api.drush.inc

+3-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function drush_elasticsearch_connector_search_api_reset_elasticsearch($search_ap
4848
$affected_indexes[] = $index;
4949
}
5050
}
51-
51+
5252
// List Search API indexes this opperation will affect.
5353
drush_print('The following Search API indexes will be affected:');
5454
foreach ($affected_indexes as $index) {
@@ -66,12 +66,9 @@ function drush_elasticsearch_connector_search_api_reset_elasticsearch($search_ap
6666
// Remove the existing indexes.
6767
foreach ($affected_indexes as $index) {
6868
try {
69-
$index->server()->removeIndex($index);
70-
}
71-
catch (Exception $e) {
72-
// The removeIndex() method throws an exception when the index does not
73-
// exist, but that is ok in this situation.
69+
$index->server()->deleteItems('all', $index);
7470
}
71+
catch (Exception $e) {}
7572
}
7673

7774
// Re-create and re-index the indexes.

0 commit comments

Comments
 (0)