-
Notifications
You must be signed in to change notification settings - Fork 8
/
tripal_elasticsearch.indices.form.inc
1168 lines (1035 loc) · 36 KB
/
tripal_elasticsearch.indices.form.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Administrative forms for management of Elasticsearch indices.
*/
/**
* Render array for tripal_elasticsearch_index_website_form.
*
* @param array $form
* @param array $form_state
*
* @return mixed
*/
function tripal_elasticsearch_indexing_form($form, &$form_state) {
try {
$es = new ESInstance();
// associate index name with indexed table.
$indices = $es->getIndices();
} catch (\Exception $exception) {
drupal_set_message($exception->getMessage(), 'warning');
drupal_set_message("Please check your Elasticsearch Connection.",
'warning');
return $form;
}
$form['instructions'] = [
'#type' => 'markup',
'#markup' => '<h1>Create New Index</h1>
<p>You can create indices for your local Elasticsearch server
here. There are three types of indices: website-wide
<b>node</b>, website-wide <b>Tripal entity</b>, and
<b>database table</b>. The website-wide indices are intended
to index all Tripal 2 or Tripal 3 content, respectively, and
cannot be configured beyond Token settings. <b>Database table</b>
indices can be customized by indexing and displaying specific
fields. </p><p>Please note that that the indices of remote
Elasticsearch servers cannot be created or edited.</p>',
];
$index_types = [
'database' => 'Database Table',
];
// Only one of each index type below is allowed.
if (!in_array('website', $indices)) {
$index_types['website'] = 'Website Nodes';
}
if (!in_array('gene_search_index', $indices)) {
$index_types['gene_search'] = 'Gene Search';
}
if (!in_array('entities', $indices)) {
$index_types['entities'] = 'Tripal Entities';
}
// Index website or a database table
$form['index_type'] = [
'#type' => 'select',
'#title' => t('Index type'),
'#description' => t('If you already have a website-wide node or tripal
entities index, it must be edited or deleted in the indices list page. Website Tripal entities index is available for Tripal version 3 only.'),
'#options' => $index_types,
'#default_value' => isset($index_types['website']) ? 'website' : 'database',
];
$form['chunk_size'] = [
'#type' => 'textfield',
'#title' => t('How many records to index per job?'),
'#description' => t('A higher number requires higher memory but will be faster to complete.'),
'#default_value' => '100',
'#required' => TRUE,
'#states' => [
'visible' => [
':input[name="index_type"]' => [
['value' => 'gene_search'],
['value' => 'entities'],
],
],
],
];
$form['index_name'] = [
'#type' => 'textfield',
'#title' => t('Enter a unique Elasticsearch index name'),
'#field_suffix' => 'less than 28 characters',
'#description' => t('Elasticsearch index name can only contain lowercase letters, numbers and underscores. It must start with a letter.'),
// This field is only visible to table indexing.
'#states' => [
'visible' => [
':input[name="index_type"]' => ['value' => 'database'],
],
],
'#size' => 25,
];
// Elasticsearch index settings
$form['index_settings'] = [
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Elasticsearch index settings'),
'#description' => t('These settings determine how your data will be indexed and made searchable.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#states' => [
'visible' => [
':input[name="index_type"]' => [
['value' => 'database'],
],
],
],
];
// $form['index_settings']['character_filters'] = array(
// '#type' => 'checkboxes',
// '#title' => t('Character filters'),
// '#options' => get_character_filter_options(),
// );
$form['index_settings']['tokenizer'] = [
'#type' => 'select',
'#title' => t('Tokenizer'),
'#options' => tripal_elasticsearch_get_tokenizer_options(),
];
$form['index_settings']['token_filters'] = [
'#type' => 'checkboxes',
'#title' => t('Token filters'),
'#options' => tripal_elasticsearch_get_token_filter_options(),
];
// Table fields and mapping types
$form['index_table'] = [
'#type' => 'select',
'#title' => t('Select a table and fields to index'),
'#options' => array_merge(['' => 'Select a table'],
drupal_map_assoc(tripal_elasticsearch_get_table_list())),
'#states' => [
'visible' => [
':input[name="index_type"]' => [
['value' => 'database'],
],
],
],
'#ajax' => [
'callback' => 'tripal_elasticsearch_table_fields_ajax_callback',
'wrapper' => 'tripal_elasticsearch_table_fields_wrapper',
],
];
$table_name = isset($form_state['values']['index_table']) ? $form_state['values']['index_table'] : '';
$columns = tripal_elasticsearch_get_column_list($table_name);
$form['table_fields'] = [
'#type' => 'fieldset',
'#title' => t('Select fields to index'),
'#tree' => TRUE,
'#options' => drupal_map_assoc($columns),
'#states' => [
'invisible' => [
':input[name="index_table"]' => ['value' => ''],
],
],
'#prefix' => '<div id="tripal_elasticsearch_table_fields_wrapper">',
'#suffix' => '</div>',
];
foreach ($columns as $field) {
$form['table_fields'][$field] = [
'#type' => 'select',
'#title' => t('Field name: ' . $field),
'#description' => t('Please select a mapping type for each field. If no
mapping type is selected for a field, that field will
not be indexed. This can be used to selectively index
table fields.'),
'#options' => array_merge(['' => 'Select mapping type'],
tripal_elasticsearch_get_field_mapping_types()),
];
}
$form['exposed'] = [
'#type' => 'checkbox',
'#title' => t('Expose Index to Cross-Site Search'),
'#description' => t("Check this box to expose your index to cross-site search. If exposed, other Tripal sites using this module will be able to search your sites and display the results on their own. Exposing an index is READ ONLY."),
'#default_value' => FALSE,
];
$form['url'] = [
'#type' => 'textfield',
'#title' => 'Results URL Path',
'#description' => t('In order for other sites to link back to your results page, you must specify a path where the form for this index can be reached.'),
'#attributes' => [
'placeholder' => 'E.g, elasticsearch/index-name',
],
'#states' => [
'visible' => [
':input[name="index_type"]' => [
['value' => 'database'],
['value' => 'gene_search'],
],
':input[name="exposed"]' => ['checked' => TRUE],
],
],
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Create New Index'),
];
return $form;
}
/**
* AJAX callback for table fields.
*
* @param $form
* @param $form_state
*
* @return mixed
*/
function tripal_elasticsearch_table_fields_ajax_callback($form, &$form_state) {
return $form['table_fields'];
}
/**
* tripal_elasticsearch_indexing_form validation
*
* @param $form
* @param $form_state
*/
function tripal_elasticsearch_indexing_form_validate($form, &$form_state) {
$type = $form_state['values']['index_type'];
$url = $form_state['values']['url'];
$exposed = $form_state['values']['exposed'];
$index_name = $form_state['values']['index_name'];
$chunk_size = $form_state['values']['chunk_size'];
if (empty($chunk_size) || !is_int(intval($chunk_size))) {
form_set_error('chunk_size',
t('Please enter a valid number. Only integers are accepted.'));
}
if ($type === "database") {
// Index name validation.
if (strlen($index_name) > 28) {
form_set_error('index_name',
t('String length cannot be greater than 28.'));
}
if (!preg_match('/^[A-Za-z][A-Za-z0-9_]+$/', $index_name)) {
form_set_error('index_name', t('index name can only contain lowercase letters,
numbers and underscores, and must start with a letter.'));
}
// 'website' is reserved for website indexing and search, use a different name
// as index name for table search.
if ($index_name === 'website') {
form_set_error('index_name', t('"website" is reserved for the website nodes index. Please
use a different name.'));
}
// At least one table field need to be selected.
if (!is_array($form_state['values']['table_fields']) || empty($form_state['values']['table_fields'])) {
form_set_error('table_fields',
t('Please specify a mapping type for at least one field.'));
}
else {
$table_fields = array_filter($form_state['values']['table_fields']);
if (empty($table_fields)) {
form_set_error('table_fields',
t('Please specify a mapping type for at least one field.'));
}
}
}
elseif ($type === "gene_search_index") {
if (!$url && $exposed) {
form_set_error('url', t('Please provide a valid url'));
}
}
else {
if ($type === 'entities') {
$count = db_query('SELECT COUNT(id) FROM {tripal_elasticsearch_priority}')->fetchField();
if (intval($count) === 0) {
form_set_error(0,
t('Please configure and submit ') . l('the tuning form',
'admin/tripal/extension/tripal_elasticsearch/tuning') . t(' before creating the entities index.'));
}
}
}
}
/**
* tripal_elasticsearch_indexing_form submission.
*
* @param $form
* @param $form_state
*/
function tripal_elasticsearch_indexing_form_submit($form, &$form_state) {
//Create the new index
tripal_elasticsearch_create_index($form_state['values']);
$form_state['redirect'] = ['admin/tripal/extension/tripal_elasticsearch/indices'];
}
/**
* Render array for tripal_elasticsearch_delete_indices_form.
*
* @param $form
* @param $form_state
*
* @return mixed|null
*/
function tripal_elasticsearch_delete_indices_form($form, &$form_state) {
try {
$client = (new ESInstance())->client;
// associate index name with indexed table.
$mappings = $client->indices()->getMapping();
} catch (\Exception $exception) {
drupal_set_message($exception->getMessage(), 'warning');
drupal_set_message("Please check your Elasticsearch Connection.",
'warning');
return NULL;
}
$existing_indices = [];
foreach (array_keys($mappings) as $index) {
if (isset(array_keys($mappings[$index]['mappings'])[1])) {
if (!in_array($index, ['entities', 'website'])) {
$existing_indices[$index] = $index . ' (indexed table: <b>' . array_keys($mappings[$index]['mappings'])[1] . '</b>)';
}
else {
$existing_indices[$index] = $index . ' (index for site-wide search)';
}
}
else {
$existing_indices[$index] = $index . ' (empty index)';
}
}
$form['indices'] = [
'#type' => 'checkboxes',
'#title' => t('Existing indices'),
'#description' => t('delete indexed data from Eleasticsearch. Please make sure you know what the data is. This process can NOT be undone.'),
'#options' => $existing_indices,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Delete'),
];
drupal_add_js(drupal_get_path('module',
'tripal_elasticsearch') . '/js/indices_delete_confirm.js');
return $form;
}
/**
* Submit function for the tripal_elasticsearch_delete_indices_form.
*
* @param $form
* @param $form_state
*/
function tripal_elasticsearch_delete_indices_form_submit($form, &$form_state) {
$delete_indices = array_filter($form_state['values']['indices']);
if (!empty($delete_indices)) {
foreach ($delete_indices as $index) {
try {
$es = new ESInstance();
$es->deleteIndex($index);
db_query('DELETE FROM {tripal_elasticsearch_indices} WHERE index_name=:index_name',
[
':index_name' => $index,
]);
} catch (\Exception $exception) {
drupal_set_message($exception->getMessage(), 'warning');
}
}
}
}
/**
* Indices List Page.
*
* @return array|mixed
*/
function tripal_elasticsearch_indices_list_page() {
try {
$es = new ESInstance();
// associate index name with indexed table.
$indices = $es->getIndices();
} catch (\Exception $exception) {
drupal_set_message($exception->getMessage(), 'warning');
drupal_set_message("Please check your Elasticsearch Connection.",
'warning');
return '';
}
// Get table indices
$sql = 'SELECT DISTINCT(index_name), table_name, exposed FROM {tripal_elasticsearch_indices}';
$table_indices = db_query($sql)->fetchAll();
$tables = [];
foreach ($table_indices as $index) {
$tables[$index->index_name]["table"] = $index->table_name;
$tables[$index->index_name]["exposed"] = (bool) $index->exposed;
}
$rows = [];
foreach ($indices as $index) {
if ($index === 'entities') {
$table = 'Indexes Tripal Entities';
}
elseif ($index === 'website') {
$table = 'Indexes Drupal Nodes';
}
elseif (isset($tables[$index])) {
$table = $tables[$index]["table"];
}
else {
$table = 'Unknown';
}
if (isset($tables[$index])) {
$exposed = $tables[$index]["exposed"];
}
$exposed_text = "private";
if (isset($exposed) && $exposed) {
$exposed_text = "public";
}
$edit = l('Edit',
'admin/tripal/extension/tripal_elasticsearch/indices/edit/' . $index);
$delete = l('Delete',
'admin/tripal/extension/tripal_elasticsearch/indices/delete/' . $index);
$row = [$index, $table, $exposed_text, $edit, $delete];
if (in_array($index, ['entities', 'gene_search_index'])) {
$row[] = l('Update',
'admin/tripal/extension/tripal_elasticsearch/indices/update/' . $index);
}
else {
$row[] = t('Update not available');
}
$rows[] = $row;
}
$output = '<h2>List of Available Indices</h2>';
$output .= theme('table', [
'header' => [
'Index Name',
'Indexed Table',
'Exposed',
'Edit',
'Delete',
'update',
],
'rows' => $rows,
]);
$link = l('Create Index',
'admin/tripal/extension/tripal_elasticsearch/indices/create');
$output .= '<p>To create a new index, click the ' . $link . ' tab above.</p>';
return $output;
}
/**
* @param $form
* @param $form_state
* @param $index_name
*
* @return mixed
*/
function tripal_elasticsearch_index_edit_confirm($form, &$form_state, $index_name) {
$form = [];
$exposed = FALSE;
$url = NULL;
//Get table name from index name.
if ($index_name == 'website') {
$index_type = "website";
$table_name = 'node';
$form['table_name'] = [
'#type' => 'value',
'#value' => 'website',
];
}
elseif ($index_name == 'entities') {
$index_type = "entities";
$table_name = 'tripal_entity';
$form['table_name'] = [
'#type' => 'value',
'#value' => 'entities',
];
}
elseif ($index_name == 'gene_search_index') {
$index_type = "gene_search";
$table_name = 'tripal_entity';
$form['table_name'] = [
'#type' => 'value',
'#value' => 'chado.feature',
];
}
else {
$index_type = "table";
$sql = "SELECT DISTINCT * FROM {tripal_elasticsearch_indices} WHERE index_name =:index_name";
$result = db_query($sql, [':index_name' => $index_name])->fetchObject();
if (!$result) {
try {
$es = new ESInstance();
$mappings = $es->getIndexMappings($index_name);
$table_name = array_keys($mappings);
if (count($table_name) > 1) {
$table_name = $table_name[1];
}
else {
$table_name = $table_name[0];
}
db_query("INSERT INTO {tripal_elasticsearch_indices} (index_name, table_name, exposed, url) VALUES(:index_name, :table_name, 0, '')",
[
':index_name' => $index_name,
':table_name' => $table_name,
]);
$result = db_query($sql, [':index_name' => $index_name])->fetchObject();
} catch (Exception $exception) {
drupal_set_message($exception->getMessage(), 'error');
return [];
}
}
$table_name = $result->table_name;
$form['table_name'] = [
'#type' => 'value',
'#value' => $table_name,
];
}
// need to query ALL types for exposed setting.
$sql = "SELECT DISTINCT * FROM {tripal_elasticsearch_indices} WHERE index_name =:index_name";
$result = db_query($sql, [':index_name' => $index_name])->fetchObject();
$exposed = $result ? $result->exposed : 0;
$url = $result ? $result->url : NULL;
$form['index_name'] = [
'#type' => 'value',
'#value' => $index_name,
];
$form['index_type'] = [
'#type' => 'value',
'#value' => $index_type,
];
// Get existing index settings and mappings.
// TODO: what about default tables?
try {
$es = new ESInstance();
$index_settings = $es->getIndexSettings($index_name);
$index_mappings = $es->getIndexMappings($index_name);
} catch (Exception $exception) {
drupal_set_message($exception->getMessage(), 'error');
return [];
}
$preset_token_settings = $index_settings[$index_name]["settings"]["index"]["analysis"]["analyzer"][$index_name];
$preset_mappings = [];
if ($index_type == 'table') {
if (array_key_exists($table_name,
$index_mappings[$index_name]["mappings"])) {
$preset_mappings = $index_mappings[$index_name]["mappings"][$table_name]["properties"];
}
}
if (!$preset_mappings && $index_type == "table") {
// there is no dbtable mapping settings yet. Cron still needs to run!
drupal_set_message("The index $index_name has no database mapping. Please make sure that your CRON job has finished running.",
'warning');
}
$index_settings = ["tokenizer" => "standard", "token_filters" => ""];
if (array_key_exists("tokenizer", $preset_token_settings)) {
$index_settings["tokenizer"] = $preset_token_settings["tokenizer"];
if (array_key_exists("filter", $preset_token_settings)) {
$index_settings["token_filters"] = $preset_token_settings["filter"];
}
}
// Snapshot the settings that require indexing so we can figure out if we need
// to re-index need to build it the same way the options are built so they match
$previous_token_filters = tripal_elasticsearch_get_token_filter_options();
$previous_token_filters = array_map(function ($value) {
return 0;
}, $previous_token_filters); //empty the array values
foreach ($index_settings["token_filters"] as $setting) {
$previous_token_filters[$setting] = $setting;
}
$previous_index_settings = [
'tokenizer' => $index_settings["tokenizer"],//tokenizer is just a string
'token_filters' => $previous_token_filters,
];
//TODO: DEAL WITH FIELDS
//store modified values in array so we can check against them upon submission
$form['previous_values'] = [
'#type' => 'value',
'#value' => [
'index_settings' => $previous_index_settings,
'index_name' => $index_name,
],
];
$form['instructions'] = [
'#type' => 'markup',
'#markup' => '<h1>Edit Index: ' . $index_name . '</h1><p>This form will allow you to edit the tokenizer settings and field settings for the index <b>' . $index_name . '.</b> Please note that changing settings will automatically rebuild the index.</p>',
];
// Elasticsearch index settings
$form['index_settings'] = [
'#type' => 'fieldset',
'#tree' => TRUE,
'#title' => t('Elasticsearch index settings'),
'#description' => t('These settings determine how your data will be indexed and made searchable.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form['index_settings']['tokenizer'] = [
'#type' => 'select',
'#title' => t('Tokenizer'),
'#options' => tripal_elasticsearch_get_tokenizer_options(),
'#default_value' => $index_settings["tokenizer"],
];
$form['index_settings']['token_filters'] = [
'#type' => 'checkboxes',
'#title' => t('Token filters'),
'#options' => tripal_elasticsearch_get_token_filter_options(),
'#default_value' => $index_settings["token_filters"] ? $index_settings["token_filters"] : [],
];
// If this is website nodes or entities, return here.
$form['exposed'] = [
'#type' => 'checkbox',
'#title' => t('Expose Index to Cross-Site Search'),
'#description' => t("Check this box to expose your index to cross-site search. If exposed, other Tripal sites using this module will be able to search your sites and display the results on their own. Exposing an index is READ ONLY."),
'#default_value' => $exposed == 1 ? TRUE : FALSE,
];
// URL should be set for exposed genes and data tables only
if ($index_type == "gene_search" || $index_type == "table") {
$form['url'] = [
'#type' => 'textfield',
'#title' => 'Results URL Path',
'#description' => t('In order for other sites to link back to your results page, you must specify a path where the form for this index can be reached.'),
'#attributes' => [
'placeholder' => 'E.g, elasticsearch/index-name',
],
'#default_value' => $url,
'#states' => [
'visible' => [
':input[name="exposed"]' => ['checked' => TRUE],
],
],
];
}
if ($index_name == 'website' || $index_name == 'entities' || $index_name == 'gene_search_index') {
$cancel_path = 'admin/tripal/extension/tripal_elasticsearch/indices';
return confirm_form($form, 'Edit Index', $cancel_path, '', 'Submit Edits');
}
$columns = tripal_elasticsearch_get_column_list($table_name);
$form['table_fields'] = [
'#type' => 'fieldset',
'#title' => t('Select fields to index'),
'#description' => t('Please select a mapping type for each field. If no
mapping type is selected for a field, that field will
not be indexed. This can be used to selectively index
table fields.'),
'#tree' => TRUE,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#options' => drupal_map_assoc($columns),
];
foreach ($columns as $field) {
$preset = '';
if ($preset_mappings && array_key_exists($field, $preset_mappings)) {
$preset = $preset_mappings[$field]["type"];
}
$form['table_fields'][$field] = [
'#type' => 'select',
'#title' => t('Field name: ' . $field),
'#default_value' => $preset,
'#options' => array_merge(['' => 'Select mapping type'],
tripal_elasticsearch_get_field_mapping_types()),
];
}
$description = 'Edit settings for ' . $index_name;
$cancel_path = 'admin/tripal/extension/tripal_elasticsearch/indices';
return confirm_form($form, 'Edit Index', $cancel_path, $description,
'Submit Edits');
}
/**
* Submit editing for index. Reindex if index settings are changed or for
* custom data tables. Only update the table for minor changes.
*
* @param $form
* @param $form_state
*/
function tripal_elasticsearch_index_edit_confirm_submit($form, &$form_state) {
$index = $form_state['values']['index_name'];
$type = $form_state['values']['index_type'];
// Check if index settings or name has change
$previous_values = $form_state['values']['previous_values'];
$previous_index_settings = $previous_values['index_settings'];
$new_index_settings = $form_state['values']['index_settings'];
// Delete/Create for custom tables since the field settings might have changed.
if ($index == $previous_values['index_name'] && $previous_index_settings['tokenizer'] && $new_index_settings['tokenizer']) {
if (empty(array_diff($previous_index_settings['token_filters'],
$new_index_settings['token_filters']))) {
if ($type != 'table') {
// No need to delete the index, just need to update the table.
tripal_elasticsearch_update_index_table($form_state['values']);
drupal_set_message("Your index has been updated. Re-indexing is not necessary.");
$form_state['redirect'] = 'admin/tripal/extension/tripal_elasticsearch/indices';
return;
}
}
}
// Delete the existing index in ES
tripal_elasticsearch_delete_index($index);
// Create the new index
tripal_elasticsearch_create_index($form_state['values']);
$form_state['redirect'] = 'admin/tripal/extension/tripal_elasticsearch/indices';
}
/**
* Index Deletion Confirmation Form.
*
* @param $form
* @param $form_state
* @param $index_name
*
* @return mixed
*/
function tripal_elasticsearch_index_delete_confirm($form, &$form_state, $index_name) {
$form = [];
$description = 'Are you sure you want to delete the ' . $index_name . ' index?';
$cancel_path = 'admin/tripal/extension/tripal_elasticsearch/indices';
$form['index_name'] = [
'#type' => 'hidden',
'#value' => $index_name,
];
return confirm_form($form, 'Are you sure?', $cancel_path, $description,
'Delete');
}
/**
* Index deletion.
*
* @param $form
* @param $form_state
*/
function tripal_elasticsearch_index_delete_confirm_submit($form, &$form_state) {
$index = $form_state['values']['index_name'];
tripal_elasticsearch_delete_index($index);
drupal_set_message('Index "' . $index . '" deleted successfully.');
$form_state['redirect'] = 'admin/tripal/extension/tripal_elasticsearch/indices';
}
/** Creates an index. Used by the create and edit index functions.
*
* @param $values
*/
function tripal_elasticsearch_create_index(&$values) {
$index_type = $values['index_type'];
$exposed = $values['exposed'] ? 1 : 0;
$url = isset($values['url']) ? $values['url'] : '';
$chunk_size = $values['chunk_size'] ?? 500;
// Populate settings for website or entities.
if ($index_type === 'website') {
// Indexing website: has default index settings.
$index_name = 'website';
$index_table = 'node';
$tokenizer = 'standard';
$token_filters = drupal_map_assoc([
'standard',
'lowercase',
]);
$field_mapping_types = [
'nid' => 'integer',
'type' => 'text',
'title' => 'text',
'content' => 'text',
];
}
elseif ($index_type === 'entities') {
$index_name = 'entities';
$index_table = 'tripal_entity';
$tokenizer = 'standard';
$token_filters = drupal_map_assoc([
'standard',
'lowercase',
]);
$field_mapping_types = [
'entity_id' => 'integer',
'bundle_label' => 'text',
'title' => 'text',
'content' => 'text',
];
}
elseif ($index_type === 'gene_search') {
$index_name = 'gene_search_index';
$index_table = 'chado.feature';
$tokenizer = 'standard';
$token_filters = drupal_map_assoc([
'standard',
'lowercase',
]);
$field_mapping_types = [
'node_id' => 'integer',
'entity_id' => 'integer',
'uniquename' => 'text',
'feature_id' => 'integer',
'organism_genus' => 'text',
'organism_species' => 'text',
'organism_common_name' => 'text',
'annotations' => 'text',
'blast_hit_descriptions' => 'text',
'type' => 'text',
'sequence' => 'text',
'sequence_length' => 'integer',
'related_features' => 'text',
];
}
else { //checks and defaults for custom index type
$index_name = $values['index_name'];
$index_table = isset($values['index_table']) ? $values['index_table'] : NULL;
$tokenizer = $values['index_settings']['tokenizer'];
$token_filters = array_filter($values['index_settings']['token_filters']);
$field_mapping_types = array_filter($values['table_fields']);
if (!$index_name) {
form_set_error('Error: No index name supplied for custom index');
return;
}
if (!$field_mapping_types) {
form_set_error('No field mapping types supplied for custom index');
}
if (!$tokenizer) {
// Use standard tokenizer and token filters if none supplied
$tokenizer = 'standard';
}
if (!$token_filters) {
$token_filters = drupal_map_assoc([
'standard',
'lowercase',
]);
}
}
// Insert index record into database for all types.
$insert = [
':index_name' => $index_name,
':table_name' => $index_table,
':exposed' => $exposed,
':url' => $url,
];
db_query('INSERT INTO {tripal_elasticsearch_indices} (index_name, table_name, exposed, url) VALUES (:index_name, :table_name, :exposed, :url)',
$insert);
// Create the index.
try {
$es = new ESInstance();
$es->setIndexParams($index_name, 5, 0, $tokenizer, $token_filters,
$field_mapping_types)->createIndex();
} catch (Exception $exception) {
form_set_error(0, $exception->getMessage());
watchdog('tripal_elasticsearch', $exception->getMessage());
return;
}
// Dispatch jobs to populate the index
$job = NULL;
switch ($index_type) {
case 'website':
$job = new NodesIndexJob();
break;
case 'gene_search':
GeneSearchIndexJob::generateDispatcherJobs(FALSE, intval($chunk_size));
break;
case 'entities':
EntitiesIndexJob::generateDispatcherJobs(1, FALSE, NULL,
intval($chunk_size));
break;
default:
$fields = array_keys($field_mapping_types);
$job = new TableIndexJob($index_name, $index_table, $fields);
}
// All jobs other than entities can be dispatched directly.
// Tripal entities must be dispatched by bundle type.
if ($job !== NULL) {
$dispatcher = new DispatcherJob($job);
$dispatcher->dispatch();
}
$base_url = variable_get('website_base_url');
$cron_url = l($base_url . '/admin/config/system/cron',
'admin/config/system/cron');
drupal_set_message("The indexing job for {$index_name} has been submitted to your CRON queue. You can view the status of your CRON jobs at {$cron_url}");
if ($index_type === 'database') {
drupal_set_message("You may create a search block for this index using the " . l('form management',
'admin/tripal/extension/tripal_elasticsearch/forms') . " section.");
}
}
/**
* Deletes an index from elasticsearch and from the db
*
* @param $index
*/
function tripal_elasticsearch_delete_index($index) {
// Find the index and delete it from ES
try {
$es = new ESInstance();
$es->deleteIndex($index);
} catch (Exception $exception) {
drupal_set_message($exception->getMessage(), 'error');
return;
}
// Find the index and delete it from the DB. this db is for the search blocks
$result = db_query("DELETE FROM {tripal_elasticsearch} WHERE index_name=:index_name",
[':index_name' => $index])->fetchObject();
if (!$result) {
// It's not in the DB so there is nothing to delete here.
return;
}
// Now delete the table info
db_query("DELETE FROM {tripal_elasticsearch_indices} WHERE index_name=:index_name",
[':index_name' => $index]);
// Delete any progress indicator
db_query('DELETE FROM {tripal_elasticsearch_queues} WHERE index_name=:index_name',
[':index_name' => $index]);
}
/**
* Update an index.
*
* @param string $index index name
*/
function tripal_elasticsearch_update_index($index) {
$result = db_query("SELECT * FROM {tripal_elasticsearch_indices} WHERE index_name=:index_name",
[':index_name' => $index])->fetchObject();
if (!$result) {
drupal_set_message("Index \"{$index}\" not found", 'error');
return drupal_goto('admin/tripal/extension/tripal_elasticsearch/indices');
}
if (!in_array($index, ['entities', 'gene_search_index'])) {
drupal_set_message('Update functionality is not available for this index. Please delete and recreate the index.',
'error');
return drupal_goto('admin/tripal/extension/tripal_elasticsearch/indices');
}
switch ($index) {
case 'gene_search_index':
GeneSearchIndexJob::generateDispatcherJobs(TRUE);
break;
case 'entities':
return drupal_goto('admin/tripal/extension/tripal_elasticsearch/indices/update_entities');
break;
}
drupal_set_message("{$index} updating jobs have been dispatched successfully. Please run `drush cron` to process items in the queue.");