/
controller.php
78 lines (68 loc) · 1.98 KB
/
controller.php
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
<?php
namespace Plugin\Elasticsearch;
class Controller extends \Controller
{
protected $client = null;
/**
* GET /search
*
* @param \Base $f3
* @return void
*/
public function search(\Base $f3)
{
$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 $e) {
$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
*
* @param \Base $f3
* @return void
*/
public function reindex(\Base $f3)
{
$this->_requireAdmin();
$base = Base::instance();
try {
$base->truncate();
$base->indexAll();
} catch (Exception $e) {
$this->_printJson(['error' => $e->getMessage()]);
}
$this->_printJson(['success' => true]);
}
}