-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.php
More file actions
71 lines (62 loc) · 1.88 KB
/
controller.php
File metadata and controls
71 lines (62 loc) · 1.88 KB
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
<?php
namespace Plugin\Elasticsearch;
class Controller extends \Controller
{
protected $client;
/**
* GET /search
*/
public function search(\Base $f3): void
{
$args = $f3->get("GET");
if (empty($args["page"])) {
$args["page"] = 0;
}
$client = Base::instance()->client();
try {
$result = $client->search([
'index' => Base::INDEX_NAME,
'type' => 'issue',
'size' => 20,
'from' => $args["page"],
'body' => [
'query' => [
'match' => [
'_all' => $f3->get('GET.q')
]
]
],
]);
$f3->set('result', $result);
} catch (Exception) {
$f3->set('error', 'Unable to load results from Elasticsearch.');
}
if (!empty($result['hits']['total'])) {
$ids = [];
foreach ($result['hits']['hits'] as $hit) {
$ids[] = $hit['_id'];
}
$db = $f3->get('db.instance');
$issues = $db->exec('SELECT * FROM issue_detail WHERE id IN (' . implode(',', $ids) . ') ORDER BY FIELD(id,' . implode(',', $ids) . ')');
$f3->set('issues', $issues);
$this->_render('elasticsearch/view/search.html');
} else {
$this->_render('elasticsearch/view/search-empty.html');
}
}
/**
* POST /search/reindex
*/
public function reindex(\Base $f3): void
{
$this->_requireAdmin();
$base = Base::instance();
try {
$base->truncate();
$base->indexAll();
} catch (Exception $e) {
$this->_printJson(['error' => $e->getMessage()]);
}
$this->_printJson(['success' => true]);
}
}