-
Notifications
You must be signed in to change notification settings - Fork 8
/
tripal_elasticsearch.install
594 lines (548 loc) · 14.7 KB
/
tripal_elasticsearch.install
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
<?php
/**
* @file
* Input, update, and delete data for build search blocks.
*/
/**
* Implements hook_schema().
*/
function tripal_elasticsearch_schema() {
$schema['tripal_elasticsearch'] = [
'description' => 'The table for store data for building search blocks',
'fields' => [
'index_name' => [
'type' => 'varchar',
'length' => '255',
],
'table_name' => [
'type' => 'varchar',
'length' => '255',
],
'index_field' => [
'type' => 'varchar',
'length' => '255',
],
'form_field_type' => [
'type' => 'varchar',
'length' => '255',
],
'form_field_title' => [
'type' => 'varchar',
'length' => '255',
],
'form_field_description' => [
'type' => 'text',
],
'form_field_options' => [
'type' => 'text',
],
'form_field_weight' => [
'type' => 'varchar',
'length' => '255',
],
],
];
$schema['tripal_elasticsearch_links'] = [
'description' => t(
'A table for storing data for adding page links to search results'
),
'fields' => [
'index_name' => [
'type' => 'varchar',
'length' => '255',
],
'table_name' => [
'type' => 'varchar',
'length' => '255',
],
'index_field' => [
'type' => 'varchar',
'length' => '255',
],
'field_url' => [
'type' => 'varchar',
'length' => '255',
],
],
];
$schema['tripal_elasticsearch_servers'] = [
'description' => t(
'Stores URLs and configuration for remote elasticsearch servers.'
),
'fields' => [
'id' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
'url' => ['type' => 'varchar', 'length' => '255'],
'label' => ['type' => 'varchar', 'length' => '255'],
'description' => ['type' => 'text'],
'logo' => ['type' => 'text'],
],
'primary key' => ['id'],
];
$schema['tripal_elasticsearch_queues'] = [
'description' => 'Store elasticsearch queue information',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'index_name' => [
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
],
'total' => [
'type' => 'int',
'default' => 0,
'not null' => TRUE,
],
'completed' => [
'type' => 'int',
'default' => 0,
'not null' => TRUE,
],
'last_run_at' => [
'type' => 'int',
'not null' => TRUE,
],
],
'primary key' => ['id'],
'indexes' => [
'index_name_index' => ['index_name'],
],
];
$schema['tripal_elasticsearch_priority'] = [
'description' => t('Stores TripalEntity field priority settings.'),
'fields' => [
'id' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
'field_id' => ['type' => 'int', 'not null' => TRUE],
'priority' => ['type' => 'int', 'not null' => TRUE],
],
'primary key' => ['id'],
];
$schema['tripal_elasticsearch_indices'] = [
'description' => 'Store elasticsearch indices',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'index_name' => [
'type' => 'varchar',
'length' => '255',
],
'table_name' => [
'type' => 'varchar',
'length' => '255',
],
],
'primary key' => ['id'],
'unique keys' => [
'index_name_unique_constraint' => ['index_name'],
],
];
return $schema;
}
/**
* Uninstall
*/
function tripal_elasticsearch_uninstall() {
// Get the schema
$schema = tripal_elasticsearch_schema();
// Auto drop anything in the schema
foreach ($schema as $table => $fields) {
if (db_table_exists($table)) {
db_drop_table($table);
}
}
// Drop tables that are created in the update functions
if (db_table_exists('tripal_elasticsearch_indices')) {
db_drop_table('tripal_elasticsearch_indices');
}
}
/**
* Run updates on install
*/
function tripal_elasticsearch_install() {
tripal_elasticsearch_update_7200();
tripal_elasticsearch_update_7201();
tripal_elasticsearch_update_7202();
tripal_elasticsearch_update_7203();
tripal_elasticsearch_update_7204();
tripal_elasticsearch_update_7205();
tripal_elasticsearch_update_7206();
tripal_elasticsearch_update_7207();
tripal_elasticsearch_update_7208();
}
/**
* create tripal_elasticsearch_indices table to store indices information.
*/
function tripal_elasticsearch_update_7200() {
if (!db_table_exists('tripal_elasticsearch_indices')) {
// Create new table
$schema = tripal_elasticsearch_schema();
$tripal_elasticsearch_indices = $schema['tripal_elasticsearch_indices'];
db_create_table(
'tripal_elasticsearch_indices',
$tripal_elasticsearch_indices
);
}
// Add a new column to the old table
if (!db_field_exists('tripal_elasticsearch', 'index_id')) {
$spec = [
'type' => 'int',
'unsigned' => TRUE,
'description' => "Foreign key references id on tripal_elasticsearch_indices",
'not null' => TRUE,
'default' => 1,
];
db_add_field('tripal_elasticsearch', 'index_id', $spec);
}
// Assign the right values to the new id field in tripal_elasticsearch_indices
$fields = db_query('SELECT * FROM {tripal_elasticsearch_indices}')->fetchAll(
);
foreach ($fields as $field) {
db_query(
'UPDATE {tripal_elasticsearch} SET index_id=:id WHERE index_name=:index_name',
[
':index_name' => $field->index_name,
':id' => $field->id,
]
);
}
}
/**
* Add field to tripal_elasticsearch_indices table to indicate whether an index
* is exposed via elasticsearch or not
*/
function tripal_elasticsearch_update_7201() {
if (!db_field_exists('tripal_elasticsearch_indices', 'exposed')) {
$spec = [
'type' => 'int',
'size' => 'small',
'description' => "Exposes index to other sites for cross-site search",
'not NULL' => TRUE,
'default' => 0,
];
db_add_field('tripal_elasticsearch_indices', 'exposed', $spec);
}
}
/**
* Create the tripal_elasticsearch_queues table to track queue progress.
*/
function tripal_elasticsearch_update_7202() {
if (!db_table_exists('tripal_elasticsearch_queues')) {
$tripal_elasticsearch_queues = [
'description' => 'Store elasticsearch queue information',
'fields' => [
'id' => [
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'index_name' => [
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
],
'total' => [
'type' => 'int',
'default' => 0,
'not null' => TRUE,
],
'completed' => [
'type' => 'int',
'default' => 0,
'not null' => TRUE,
],
'last_run_at' => [
'type' => 'int',
'not null' => TRUE,
],
],
'primary key' => ['id'],
'indexes' => [
'index_name_index' => ['index_name'],
],
];
db_create_table(
'tripal_elasticsearch_queues',
$tripal_elasticsearch_queues
);
}
}
/**
* Add type field to tripal_elasticsearch_queues table.
*/
function tripal_elasticsearch_update_7203() {
if (!db_field_exists('tripal_elasticsearch_queues', 'type')) {
$spec = [
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
];
db_add_field('tripal_elasticsearch_queues', 'type', $spec);
}
}
/**
* Add started_at field to tripal_elasticsearch_queues table.
*/
function tripal_elasticsearch_update_7204() {
if (!db_field_exists('tripal_elasticsearch_queues', 'started_at')) {
$spec = [
'type' => 'int',
'not null' => FALSE,
];
db_add_field('tripal_elasticsearch_queues', 'started_at', $spec);
}
}
/**
* Add url field to tripal_elasticsearch_indices table.
*/
function tripal_elasticsearch_update_7205() {
if (!db_field_exists('tripal_elasticsearch_indices', 'url')) {
$spec = [
'type' => 'varchar',
'length' => '255',
'not null' => FALSE,
];
db_add_field('tripal_elasticsearch_indices', 'url', $spec);
}
}
/**
* Add tripal elasticsearch servers table.
*/
function tripal_elasticsearch_update_7206() {
if (!db_table_exists('tripal_elasticsearch_servers')) {
$spec = [
'description' => t(
'Stores URLs and configuration for remote elasticsearch servers.'
),
'fields' => [
'id' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
'url' => ['type' => 'varchar', 'length' => '255'],
'label' => ['type' => 'varchar', 'length' => '255'],
'description' => ['type' => 'text'],
],
'primary key' => ['id'],
];
db_create_table('tripal_elasticsearch_servers', $spec);
}
}
/**
* Create tripal elasticsearch priority table.
*/
function tripal_elasticsearch_update_7207() {
if (!db_table_exists('tripal_elasticsearch_priority')) {
$spec = [
'description' => t('Stores TripalEntity field priority settings.'),
'fields' => [
'id' => ['type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE],
'field_id' => ['type' => 'int'],
'priority' => ['type' => 'int'],
],
'primary key' => ['id'],
];
db_create_table('tripal_elasticsearch_priority', $spec);
}
}
/**
* Add priority field to tripal_elasticsearch_queues table.
*/
function tripal_elasticsearch_update_7208() {
if (!db_field_exists('tripal_elasticsearch_queues', 'priority')) {
$spec = [
'type' => 'int',
'not null' => TRUE,
'default' => 1,
];
db_add_field('tripal_elasticsearch_queues', 'priority', $spec);
}
}
/**
* Create new gene search elasticsearch fields.
*/
function tripal_elasticsearch_update_7209() {
try {
$es = new ESInstance();
$es->putMapping('gene_search_index', 'entity_id', 'integer');
$es->putMapping('gene_search_index', 'node_id', 'integer');
} catch (Exception $exception) {
print "ERROR: failed to add new elasticsearch fields due to the following error\n";
print $exception->getMessage();
throw $exception;
}
}
/**
* Remove blast database content type from website index.
*/
function tripal_elasticsearch_update_7210() {
try {
$es = new ESInstance();
if (in_array('website', $es->getIndices())) {
return;
}
$count = $es->setWebsiteSearchParams('*', 'Blast Database', 'website')
->count();
$client = $es->client;
if ($count === 0) {
print "No blast database content was found\n";
return;
}
for ($i = 0; $i < $count; $i += 100) {
$results = $es->setWebsiteSearchParams(
'*',
'Blast Database',
'website',
'website',
[
$i,
100,
]
)->search(FALSE);
foreach ($results as $result) {
$client->deleteByQuery(
[
'index' => 'website',
'type' => 'website',
'body' => [
'query' => [
'match' => [
'nid' => $result['nid'],
],
],
],
]
);
}
}
print "Successfully removed all blast database nodes from website index.\n";
} catch (Exception $exception) {
print "ERROR: failed to remove blast database nodes from website index due to the following exception\n";
print $exception->getMessage();
throw $exception;
}
}
/**
* Remove blastdb content type from website index.
*/
function tripal_elasticsearch_update_7211() {
try {
$es = new ESInstance();
if (!in_array('website', $es->getIndices())) {
return;
}
$count = $es->setWebsiteSearchParams('*', 'blastdb', 'website')->count();
$client = $es->client;
if ($count === 0) {
print "No blastdb content was found\n";
return;
}
for ($i = 0; $i < $count; $i += 100) {
$results = $es->setWebsiteSearchParams(
'*',
'blastdb',
'website',
'website',
[
$i,
100,
]
)->search(FALSE);
foreach ($results as $result) {
$client->deleteByQuery(
[
'index' => 'website',
'type' => 'website',
'body' => [
'query' => [
'match' => [
'nid' => $result['nid'],
],
],
],
]
);
}
}
print "Successfully removed all blastdb nodes from website index.\n";
} catch (Exception $exception) {
print "ERROR: failed to remove blastdb nodes from website index due to the following exception\n";
print $exception->getMessage();
throw $exception;
}
}
/**
* Add the related_features field to the gene search index.
*/
function tripal_elasticsearch_update_7212() {
try {
$es = new ESInstance();
if (in_array('gene_search_index', $es->getIndices())) {
$es->putMapping(
'gene_search_index',
'related_features',
'text',
'chado.feature'
);
}
} catch (Exception $exception) {
// Ignore failure since it's not important. The field gets auto created on
// index update. We don't want to remove this function to maintain the
// increment of the update functions.
}
}
/**
* Add logo column to tripal_elasticsearch_servers table
*/
function tripal_elasticsearch_update_7213() {
$table = 'tripal_elasticsearch_servers';
$field = 'logo';
if (!db_field_exists($table, $field)) {
db_add_field($table, $field, ['type' => 'text']);
}
}
/**
* Remove duplicate indices.
*/
function tripal_elasticsearch_delete_duplicate_indices() {
$visited = [];
$query = db_select('tripal_elasticsearch_indices', 't');
$query->fields('t');
$query->orderBy('id', 'DESC');
$indices = $query->execute()->fetchAll();
foreach ($indices as $index) {
if (isset($visited[$index->index_name])) {
continue;
}
$visited[$index->index_name] = $index;
$query = db_update('tripal_elasticsearch');
$query->condition('index_name', $index->name);
$query->fields([
'index_id' => $index->id
]);
$query->execute();
$query = db_delete('tripal_elasticsearch_indices');
$query->condition('index_name', $index->index_name);
$query->condition('id', $index->id, '!=');
$query->execute();
}
}
/**
* Delete duplicate indices and add unique name constraint.
*/
function tripal_elasticsearch_update_7214() {
tripal_elasticsearch_delete_duplicate_indices();
try {
db_add_unique_key(
'tripal_elasticsearch_indices',
'index_name_unique_constraint',
[
'index_name',
]
);
} catch (Exception $exception) {
print $exception->getMessage();
}
}