-
Notifications
You must be signed in to change notification settings - Fork 8
/
tripal_elasticsearch.api.inc
422 lines (375 loc) · 12 KB
/
tripal_elasticsearch.api.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
<?php
function tripal_elasticsearch_get_tokenizer_options() {
return drupal_map_assoc([
'standard',
'letter',
'lowercase',
'whitespace',
'uax_url_email',
'classic',
'ngram',
'edge_ngram',
'keyword',
'pattern',
'path_hierarchy',
]);
}
function tripal_elasticsearch_get_token_filter_options() {
return drupal_map_assoc([
'standard',
'asciifolding',
'length',
'lowercase',
'uppercase',
]);
}
/**
* Return an array of Elasticsearch field mapping types.
*/
function tripal_elasticsearch_get_field_mapping_types() {
return drupal_map_assoc([
'text',
'keyword',
'date',
'long',
'double',
'boolean',
'ip',
'object',
'nested',
'geo_point',
'geo_shape',
'completion',
]);
}
/**
* This function takes a table name and return all the column names
*/
function tripal_elasticsearch_get_column_list($table_name) {
// Detect which schema the table belongs to. If it has prefix 'chado.', it is
// the chado schema, otherwise it is from the public schema.
if (preg_match('/^chado\./', $table_name)) {
$table_name = preg_replace('/^chado\./', '', $table_name);
$sql_column_list = "SELECT column_name FROM information_schema.columns WHERE table_schema = 'chado' AND table_name = :table_name";
}
else {
$sql_column_list = "SELECT column_name FROM information_schema.columns WHERE table_schema = 'public' AND table_name = :table_name";
}
$column_list_results = db_query($sql_column_list, [':table_name' => $table_name])->fetchAll();
$column_list = [];
foreach ($column_list_results as $value) {
$column_list[] = $value->column_name;
}
return $column_list;
}
/**
* This function return an array containing a list of table names from the
* public and chado schema.
*/
function tripal_elasticsearch_get_table_list() {
$sql_public_table_list = "SELECT table_name FROM information_schema.tables WHERE (table_schema = 'public') ORDER BY table_name";
$sql_chado_table_list = "SELECT table_name FROM information_schema.tables WHERE (table_schema = 'chado') ORDER BY table_name";
$public_table_results = db_query($sql_public_table_list)->fetchAll();
$chado_table_results = db_query($sql_chado_table_list)->fetchAll();
$public_tables = [];
$chado_tables = [];
foreach ($public_table_results as $value) {
$public_tables[] = $value->table_name;
}
foreach ($chado_table_results as $value) {
$chado_tables[] = 'chado.' . $value->table_name;
}
return array_merge($public_tables, $chado_tables);
}
/*
* Escape special characters for elasticsearch
*/
function _tripal_elasticsearch_remove_special_chars($keyword) {
return stripslashes($keyword);
}
/**
* Build search query from field content pairs.
*/
function tripal_elasticsearch_build_search_query_from_field_content_pairs(array $field_content_pairs, $query_method = 'query_string') {
$queries = [];
foreach ($field_content_pairs as $field => $content) {
if (!empty($content)) {
$queries[] = [
$query_method => [
"default_field" => $field,
"query" => _tripal_elasticsearch_remove_special_chars($content),
"default_operator" => "OR",
],
];
}
}
$query = [
"bool" => [
"must" => $queries,
],
];
return $query;
}
/**
* Function to theme search results count by node type.
* This function returns an html list.
*
* @throws \Exception
* @return string
*/
function tripal_elasticsearch_get_website_search_results_category_list($keyword) {
try {
$es = new ESInstance();
$indices = $es->getIndices();
$types = $es->getAllCategories(NULL, TRUE, $keyword);
} catch (Exception $exception) {
tripal_report_error('tripal_elasticsearch', 'TRIPAL_WARNING', $exception->getMessage());
watchdog('tripal_elasticsearch', $exception->getMessage());
return '';
}
$index_name = [];
if (in_array('website', $indices)) {
$index_name[] = 'website';
}
if (in_array('entities', $indices)) {
$index_name[] = 'entities';
}
// Save search results count to an associative array with node types as keys.
$search_website_count_by_category = [];
foreach ($types['types'] as $type => $label) {
try {
$count = $types['count'][$type];
if ($count > 0) {
$search_website_count_by_category[$type] = $count;
}
} catch (Exception $exception) {
drupal_set_message('The search service is currently unavailable. Please try again later.', 'error');
watchdog('tripal_elasticsearch', $exception->getMessage(), [], 'error');
}
}
// Add total count to the $search_website_count_by_category array.
$total_count = ['all categories' => array_sum($search_website_count_by_category)];
$search_website_count_by_category = $total_count + $search_website_count_by_category;
$website_search_by_node_type = [
'keyword' => $keyword,
'count' => $search_website_count_by_category,
];
$output = '<h4>Filter by Category</h4>';
$all_categories_text = 'All categories (' . $website_search_by_node_type['count']['all categories'] . ')';
$all_categories_link = 'tripal_elasticsearch/search_website';
$output .= l($all_categories_text, $all_categories_link, [
'query' => [
'search_box' => $website_search_by_node_type['keyword'],
],
'attributes' => [
'class' => ['es-category-item', 'es-category-item__all'],
],
]);
unset($website_search_by_node_type['count']['all categories']);
$items = [];
foreach ($website_search_by_node_type['count'] as $category => $count) {
$text = $types['types'][$category] . ' (' . $count . ')';
$url = 'tripal_elasticsearch/search_website/' . $category;
$items[] = l($text, $url, [
'query' => [
'search_box' => $website_search_by_node_type['keyword'],
'category' => $category,
],
'attributes' => [
'class' => ['es-category-item'],
],
]);
}
$output .= theme('item_list', ['items' => $items]);
return $output;
}
/**
* Get website search result table.
*
* @param array $search_results an associative array with three keys: nid,
* title, and highlight.
* @param boolean $pager Whether to render a pager
* @param string $base_url Specify the site that owns the results.
*
* @return string
* @throws Exception
*/
function tripal_elasticsearch_get_website_search_result_table($search_results, $pager = TRUE, $base_url = NULL) {
// Get all labels
$types = [];
if (function_exists('tripal_load_entity')) {
$entity_labels = db_query('SELECT name, label FROM {tripal_bundle}')->fetchAll();
foreach ($entity_labels as $label) {
$types[$label->name] = $label->label;
}
}
$node_labels = db_query('SELECT type, name FROM {node_type}')->fetchAll();
foreach ($node_labels as $label) {
$types[$label->type] = $label->name;
}
// grab page title and highlight content from search results.
$rows = [];
foreach ($search_results as $item) {
$item = (object) $item;
$row = new stdClass();
$row->title = $item->title;
if (isset($item->nid)) {
// $row = '<h3>' . l($item->title, 'node/' . $item->nid) . '</h3>';
// if ($base_url) {
// $row = '<h3><a href="' . $base_url . '/node/' . $item->nid . '">' . $item->title . '</a></h3>';
// }
if ($base_url) {
$row->url = "{$base_url}/node/{$item->nid}";
}
else {
$row->url = "/node/{$item->nid}";
}
}
else {
// $row = '<h3>' . l($item->title, 'bio_data/' . $item->entity_id) . '</h3>';
// if ($base_url) {
// $row = '<h3><a href="' . $base_url . '/bio_data/' . $item->entity_id . '">' . $item->title . '</a></h3>';
// }
if ($base_url) {
$row->url = "{$base_url}/bio_data/{$item->entity_id}";
}
else {
$row->url = "/bio_data/{$item->entity_id}";
}
}
$row->type = '';
if (property_exists($item, 'bundle_label')) {
$type = isset($types[$item->bundle_label]) ? $types[$item->bundle_label] : $item->bundle_label;
// $row .= '<div>Type: <i>' . $type . '</i></div>';
$row->type = $type;
}
elseif (property_exists($item, 'type')) {
$type = isset($types[$item->type]) ? $types[$item->type] : $item->type;
// $row .= '<div>Type: <i>' . $type . '</i></div>';
$row->type = $type;
}
$row->content = '';
if (property_exists($item, 'highlight')) {
// $row .= '<p>' . $item->highlight . '</p>';
$row->content = $item->highlight;
}
$rows[] = $row;
}
// $output = theme('table', [
// 'header' => [],
// 'rows' => $rows,
// 'attributes' => [
// 'id' => 'es-search-results-table',
// ],
// ]);
$output = theme('elasticsearch_results', [
'rows' => $rows,
'base_url' => $base_url
]);
if ($pager) {
$output .= theme('pager', ['quantity', count($rows)]);
}
return $output;
}
/**
* Get table search result table.
*
* @param array $search_results the trimmed search results
* @param string $index_name
* @param int $total the total number of results
*
* @return mixed
*/
function tripal_elasticsearch_get_table_search_result_table($search_results, $index_name, $total) {
// Get field url
$sql = "SELECT * FROM {tripal_elasticsearch_links} WHERE index_name = :index_name";
$result = db_query($sql, [':index_name' => $index_name]);
$sql = "SELECT form_field_title, index_field FROM {tripal_elasticsearch} WHERE index_name = :index_name";
$header_values = db_query($sql, [':index_name' => $index_name]);
$titles = [];
foreach ($header_values as $title) {
$titles[$title->index_field] = $title->form_field_title;
}
$field_url_pairs = [];
foreach ($result as $record) {
$field = $record->index_field;
$url = $record->field_url;
$field_url_pairs[$field] = $url;
// $field_url_pairs[$field] = preg_replace('/\[(.+)\]/U', '\$${1}', $url);
}
$filtered_results = array_filter($field_url_pairs, function ($item) {
return !empty($item);
});
$rows = [];
foreach ($search_results as $result) {
$row = [];
foreach ($result as $key => $value) {
if (!isset($titles[$key])) {
continue;
}
$row[$key] = $value;
}
$rows[] = $row;
}
// Add links to search results.
if (!empty($filtered_results)) {
$processed_search_results = [];
foreach ($search_results as $row) {
$row = (array) $row;
foreach ($field_url_pairs as $field => $url) {
if ($url != '') {
// replace field variables in url with field values.
preg_match_all('/\[.+?\]/', $url, $matches);
$field_variables = $matches[0];
array_walk($field_variables, function (&$value) {
$value = preg_replace('/\[(.+)\]/U', '${1}', $value);
});
$replacement = [];
foreach ($field_variables as $field_variable) {
$replacement[] = $row[$field_variable];
}
$url = str_replace($matches[0], $replacement, $url);
// theme search results.
$field_search_result = $row[$field];
$row[$field] = l($field_search_result, $url);
}
}
$processed_search_results[] = $row;
}
$rows = $processed_search_results;
}
// theme search results as a table.
$output = '';
$headers = [];
$i = 0;
if (!empty($search_results)) {
foreach ($search_results[0] as $key => $value) {
if (!isset($titles[$key])) {
continue;
}
$title = $titles[$key];
$header = [
'field' => $key,
'data' => $key,
'data-elastic-title' => $title,
];
if (isset($_GET['order']) && $_GET['order'] === $key) {
$header['sort'] = isset($_GET['sort']) ? $_GET['sort'] : 'asc';
}
else {
if ($i === 0) {
$header['sort'] = 'asc';
}
}
$headers[] = $header;
$i++;
}
}
$output .= theme('table', [
'header' => $headers,
'rows' => $rows,
'attributes' => ['id' => ['tripal-elastic-search-results-table']],
]);
$output .= theme('pager', ['quantity', $total]);
return $output;
}