forked from nucknorris/fulltextsearch.ontowiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FulltextsearchController.php
267 lines (213 loc) · 8.98 KB
/
FulltextsearchController.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
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
<?php
/**
* This file is part of the {@link http://amsl.technology amsl} project.
*
* @author Sebastian Nuck
* @copyright Copyright (c) 2015, {@link http://ub.uni-leipzig.de Leipzig University Library}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
require_once realpath(dirname(__FILE__)) . '/classes/ElasticsearchHelper.php';
require_once realpath(dirname(__FILE__)) . '/classes/ElasticsearchUtils.php';
require_once realpath(dirname(__FILE__)) . '/classes/IndexServiceConnector.php';
/**
* Fulltextsearch component controller.
*
* @category OntoWiki
* @package Extensions_Fulltextsearch
* @author Sebastian Nuck
* @copyright Copyright (c) 2014, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
class FulltextsearchController extends OntoWiki_Controller_Component
{
public function fulltextsearchAction()
{
// tells the OntoWiki to not apply the template to this action
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$translate = $this->_owApp->translate;
$searchText = null;
if ($this->_request->query !== null) {
$searchText = htmlspecialchars(trim($this->_request->query));
}
$error = false;
$errorMsg = '';
// check if search already contains an error
if (!$error) {
$esHelper = new ElasticsearchHelper($this->_privateConfig);
// $esHelper = new ElasticsearchHelperOld();
$result = $esHelper->search($searchText);
}
// if error occured set output for error page
if ($error) {
$this->view->errorMsg = $errorMsg;
}
$this->_response->setBody(json_encode($result));
}
/**
*
*/
public function searchAction()
{
OntoWiki::getInstance()->logger->info('searchAction');
$translate = $this->_owApp->translate;
$this->view->placeholder('main.window.title')->set($translate->_('Fulltext Search'));
$this->addModuleContext('main.window.fulltextsearch.search');
$params = $this->_request->getParams();
$input = $params['input'];
$from = $params['from'];
$indices = '';
if (isset($params['indices'])) {
$indices = $params['indices'];
}
$this->view->input = $input;
$esHelper = new ElasticsearchHelper($this->_privateConfig);
$result = $esHelper->searchAndReturnEverything($input, $indices, $from);
$this->view->jsonResult = $result['resultSet'];
$this->view->availableIndices = $esHelper->getSearchableIndices();
// transform comma separated indices to array
$this->view->selectedIndices = str_getcsv($indices);
$this->view->resultArray = ElasticsearchUtils::extractResults($result['resultSet']);
$this->view->query = $result['query'];
$this->view->from = $from;
$this->view->input = $input;
$this->view->hits = $result['resultSet']['hits']['total'];
$this->view->titleHelper = new OntoWiki_Model_TitleHelper();
OntoWiki::getInstance()->getNavigation()->disableNavigation();
}
/**
* Displays an information page.
* @return [type]
*/
public function infoAction()
{
$_owApp = OntoWiki::getInstance();
$logger = $_owApp->logger;
$translate = $this->_owApp->translate;
$this->view->placeholder('main.window.title')->set($translate->_('Fulltext Search Info'));
$this->addModuleContext('main.window.fulltextsearch.info');
$_owApp->getNavigation()->disableNavigation();
$models = $this->getIndexableModels();
$esHelper = new ElasticsearchHelper($this->_privateConfig);
$indices = $esHelper->getAvailableIndicesWithMetadata();
}
public function availableindicesAction() {
// tells the OntoWiki to not apply the template to this action
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$models = $this->getIndexableModels();
$this->_response->setBody(json_encode($models));
}
/**
* Creates a new index, requires indexname parameter.
* @return [type]
*/
public function createindexAction()
{
$_owApp = OntoWiki::getInstance();
$logger = $_owApp->logger;
// tells the OntoWiki to not apply the template to this action
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$params = $this->_request->getParams();
$indexname = $params['indexname'];
$indexServiceConnector = new IndexServiceConnector($this->_privateConfig);
$response = $indexServiceConnector->triggerCreateIndex($indexname);
$indexServiceConnector->finish();
$_owApp->logger->debug('Fulltextsearch createindexAction response:' . $response);
$this->_response->setBody(json_encode($response));
}
/**
* Provides basic metadata to an index like number of documents per class
*/
public function countobjectsAction()
{
$_owApp = OntoWiki::getInstance();
$logger = $_owApp->logger;
// tells the OntoWiki to not apply the template to this action
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$params = $this->_request->getParams();
$indexname = $params['indexname'];
$esHelper = new ElasticsearchHelper($this->_privateConfig);
$metadata = array();
$specificConfigurations = $this->_privateConfig->fulltextsearch->specificconfigurations->toArray();
if ($key = ElasticsearchUtils::recursiveArraySearch($indexname, $specificConfigurations)){
$classes = $specificConfigurations[$key]['classes'];
} else {
$classes = $this->_privateConfig->fulltextsearch->classes->toArray();
}
if (is_array($classes)) {
foreach ($classes as $class) {
$count = $esHelper->countObjects($indexname, $class);
$metadata[$class][] = $count;
}
} else {
$count = $esHelper->countObjects($indexname, $classes);
$metadata[$classes][] = $count;
}
$this->_response->setBody(json_encode($metadata));
}
public function deleteindexAction()
{
$_owApp = OntoWiki::getInstance();
$logger = $_owApp->logger;
// tells the OntoWiki to not apply the template to this action
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$params = $this->_request->getParams();
$indexname = $params['indexname'];
$_owApp->logger->debug('Fulltextsearch deleteindexAction: deleting index: ' . $indexname);
$indexServiceConnector = new IndexServiceConnector($this->_privateConfig);
$response = $indexServiceConnector->triggerDeleteIndex($indexname);
$indexServiceConnector->finish();
$this->_response->setHeader('Content-Type', 'text/html');
$this->_response->setBody($response);
}
public function reindexAction()
{
$_owApp = OntoWiki::getInstance();
$logger = $_owApp->logger;
// tells the OntoWiki to not apply the template to this action
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
$params = $this->_request->getParams();
if (isset($params['indexname'])) {
$indexname = $params['indexname'];
} else {
$indexname = null;
}
// if index specified
if ($indexname !== null) {
$logger->debug('reindex action: ' . $indexname);
$indexServiceConnector = new IndexServiceConnector($this->_privateConfig);
$response = $indexServiceConnector->triggerReindexClass($indexname);
$indexServiceConnector->finish();
$this->_response->setBody($response->body);
} else {
// if no index specified --> full reindex
$logger->debug('full reindex');
$indexServiceConnector = new IndexServiceConnector($this->_privateConfig);
$response = $indexServiceConnector->triggerFullreindex($this->_privateConfig);
$indexServiceConnector->finish();
$this->_response->setBody($response);
}
}
/**
* @return array
* @throws Erfurt_Exception
* @throws Erfurt_Store_Exception
* @throws Exception
*/
public function getIndexableModels()
{
$allModels = Erfurt_App::getInstance()->getStore()->getAvailableModels($withHidden = true);
$ignoredModels = $this->_privateConfig->fulltextsearch->ignoredModels->toArray();
foreach ($ignoredModels as $model) {
if (array_key_exists($model, $allModels)) {
unset($allModels[$model]);
}
}
return $allModels;
}
}