1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2009-2015 Ingo Renner <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser\DocumentEscapeService; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Search\FacetsModifier; |
29
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
30
|
|
|
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
31
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrCommunicationException; |
32
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection; |
33
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query as NewQuery; |
34
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Class to handle solr search requests |
38
|
|
|
* |
39
|
|
|
* @author Ingo Renner <[email protected]> |
40
|
|
|
*/ |
41
|
|
|
class Search |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* An instance of the Solr service |
46
|
|
|
* |
47
|
|
|
* @var SolrConnection |
48
|
|
|
*/ |
49
|
|
|
protected $solr = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The search query |
53
|
|
|
* |
54
|
|
|
* @var NewQuery |
55
|
|
|
*/ |
56
|
|
|
protected $query = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The search response |
60
|
|
|
* |
61
|
|
|
* @var \Apache_Solr_Response |
62
|
|
|
*/ |
63
|
|
|
protected $response = null; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var TypoScriptConfiguration |
67
|
|
|
*/ |
68
|
|
|
protected $configuration; |
69
|
|
|
|
70
|
|
|
// TODO Override __clone to reset $response and $hasSearched |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
74
|
|
|
*/ |
75
|
|
|
protected $logger = null; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Constructor |
79
|
|
|
* |
80
|
|
|
* @param SolrConnection $solrConnection The Solr connection to use for searching |
81
|
|
|
*/ |
82
|
54 |
|
public function __construct(SolrConnection $solrConnection = null) |
83
|
|
|
{ |
84
|
54 |
|
$this->logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__); |
85
|
|
|
|
86
|
54 |
|
$this->solr = $solrConnection; |
87
|
|
|
|
88
|
54 |
|
if (is_null($solrConnection)) { |
89
|
|
|
/** @var $connectionManager ConnectionManager */ |
90
|
7 |
|
$connectionManager = GeneralUtility::makeInstance(ConnectionManager::class); |
91
|
7 |
|
$this->solr = $connectionManager->getConnectionByPageId($GLOBALS['TSFE']->id, $GLOBALS['TSFE']->sys_language_uid); |
92
|
|
|
} |
93
|
|
|
|
94
|
54 |
|
$this->configuration = Util::getSolrConfiguration(); |
95
|
54 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Gets the Solr connection used by this search. |
99
|
|
|
* |
100
|
|
|
* @return SolrConnection Solr connection |
101
|
|
|
*/ |
102
|
|
|
public function getSolrConnection() |
103
|
|
|
{ |
104
|
|
|
return $this->solr; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets the Solr connection used by this search. |
109
|
|
|
* |
110
|
|
|
* Since ApacheSolrForTypo3\Solr\Search is a \TYPO3\CMS\Core\SingletonInterface, this is needed to |
111
|
|
|
* be able to switch between multiple cores/connections during |
112
|
|
|
* one request |
113
|
|
|
* |
114
|
|
|
* @param SolrConnection $solrConnection |
115
|
|
|
*/ |
116
|
|
|
public function setSolrConnection(SolrConnection $solrConnection) |
117
|
|
|
{ |
118
|
|
|
$this->solr = $solrConnection; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Executes a query against a Solr server. |
123
|
|
|
* |
124
|
|
|
* 1) Gets the query string |
125
|
|
|
* 2) Conducts the actual search |
126
|
|
|
* 3) Checks debug settings |
127
|
|
|
* |
128
|
|
|
* @param Query $query The query with keywords, filters, and so on. |
129
|
|
|
* @param int $offset Result offset for pagination. |
130
|
|
|
* @param int $limit Maximum number of results to return. If set to NULL, this value is taken from the query object. |
131
|
|
|
* @return \Apache_Solr_Response Solr response |
132
|
|
|
* @throws \Exception |
133
|
|
|
*/ |
134
|
48 |
|
public function search(NewQuery $query, $offset = 0, $limit = 10) |
135
|
|
|
{ |
136
|
48 |
|
$this->query = $query; |
137
|
|
|
|
138
|
48 |
|
if (empty($limit)) { |
139
|
37 |
|
$limit = $query->getRows(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
try { |
143
|
48 |
|
$response = $this->solr->getReadService()->search( |
144
|
48 |
|
(string)$query->getQueryStringContainer()->getQueryString(), |
145
|
48 |
|
$offset, |
146
|
48 |
|
$limit, |
147
|
48 |
|
$query->getQueryParameters() |
148
|
|
|
); |
149
|
|
|
|
150
|
46 |
|
if ($this->configuration->getLoggingQueryQueryString()) { |
151
|
|
|
$this->logger->log( |
152
|
|
|
SolrLogManager::INFO, |
153
|
|
|
'Querying Solr, getting result', |
154
|
|
|
[ |
155
|
|
|
'query string' => $query->getQueryStringContainer()->getQueryString(), |
156
|
|
|
'query parameters' => $query->getQueryParameters(), |
157
|
|
|
'response' => json_decode($response->getRawResponse(), |
158
|
46 |
|
true) |
159
|
|
|
] |
160
|
|
|
); |
161
|
|
|
} |
162
|
2 |
|
} catch (SolrCommunicationException $e) { |
163
|
2 |
|
if ($this->configuration->getLoggingExceptions()) { |
164
|
2 |
|
$this->logger->log( |
165
|
2 |
|
SolrLogManager::ERROR, |
166
|
2 |
|
'Exception while querying Solr', |
167
|
|
|
[ |
168
|
2 |
|
'exception' => $e->__toString(), |
169
|
2 |
|
'query' => (array)$query, |
170
|
2 |
|
'offset' => $offset, |
171
|
2 |
|
'limit' => $limit |
172
|
|
|
] |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
|
throw $e; |
177
|
|
|
} |
178
|
|
|
|
179
|
46 |
|
$this->response = $response; |
180
|
|
|
|
181
|
46 |
|
return $this->response; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Sends a ping to the solr server to see whether it is available. |
186
|
|
|
* |
187
|
|
|
* @param bool $useCache Set to true if the cache should be used. |
188
|
|
|
* @return bool Returns TRUE on successful ping. |
189
|
|
|
* @throws \Exception Throws an exception in case ping was not successful. |
190
|
|
|
*/ |
191
|
|
|
public function ping($useCache = true) |
192
|
|
|
{ |
193
|
|
|
$solrAvailable = false; |
194
|
|
|
|
195
|
|
|
try { |
196
|
|
|
if (!$this->solr->getReadService()->ping(2, $useCache)) { |
197
|
|
|
throw new \Exception('Solr Server not responding.', 1237475791); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$solrAvailable = true; |
201
|
|
|
} catch (\Exception $e) { |
202
|
|
|
if ($this->configuration->getLoggingExceptions()) { |
203
|
|
|
$this->logger->log( |
204
|
|
|
SolrLogManager::ERROR, |
205
|
|
|
'Exception while trying to ping the solr server', |
206
|
|
|
[ |
207
|
|
|
$e->__toString() |
208
|
|
|
] |
209
|
|
|
); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $solrAvailable; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Gets the query object. |
218
|
|
|
* |
219
|
|
|
* @return NewQuery Query |
220
|
|
|
*/ |
221
|
31 |
|
public function getQuery() |
222
|
|
|
{ |
223
|
31 |
|
return $this->query; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Gets the Solr response |
228
|
|
|
* |
229
|
|
|
* @return \Apache_Solr_Response |
230
|
|
|
*/ |
231
|
26 |
|
public function getResponse() |
232
|
|
|
{ |
233
|
26 |
|
return $this->response; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function getRawResponse() |
237
|
|
|
{ |
238
|
|
|
return $this->response->getRawResponse(); |
239
|
|
|
} |
240
|
|
|
|
241
|
26 |
|
public function getResponseHeader() |
242
|
|
|
{ |
243
|
26 |
|
return $this->getResponse()->responseHeader; |
244
|
|
|
} |
245
|
|
|
|
246
|
26 |
|
public function getResponseBody() |
247
|
|
|
{ |
248
|
26 |
|
return $this->getResponse()->response; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Gets the time Solr took to execute the query and return the result. |
253
|
|
|
* |
254
|
|
|
* @return int Query time in milliseconds |
255
|
|
|
*/ |
256
|
26 |
|
public function getQueryTime() |
257
|
|
|
{ |
258
|
26 |
|
return $this->getResponseHeader()->QTime; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Gets the number of results per page. |
263
|
|
|
* |
264
|
|
|
* @return int Number of results per page |
265
|
|
|
*/ |
266
|
|
|
public function getResultsPerPage() |
267
|
|
|
{ |
268
|
|
|
return $this->getResponseHeader()->params->rows; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @deprecated Since 9.0.0 will be removed in 10.0.0. Please use SearchResultSet::getAllResultCount now |
273
|
|
|
* @return int |
274
|
|
|
*/ |
275
|
|
|
public function getNumberOfResults() |
276
|
|
|
{ |
277
|
|
|
trigger_error('Search::getNumberOfResults is deprecated please use SearchResultSet::getAllResultCount now', E_USER_DEPRECATED); |
278
|
|
|
return $this->response->response->numFound; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Gets the result offset. |
283
|
|
|
* |
284
|
|
|
* @return int Result offset |
285
|
|
|
*/ |
286
|
|
|
public function getResultOffset() |
287
|
|
|
{ |
288
|
|
|
return $this->response->response->start; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @deprecated Since 9.0.0 will be removed in 10.0.0. Please use SearchResultSet::getMaxScore now. |
293
|
|
|
* @return mixed |
294
|
|
|
*/ |
295
|
|
|
public function getMaximumResultScore() |
296
|
|
|
{ |
297
|
|
|
trigger_error('Search::getMaximumResultScore is deprecated please use SearchResultSet::getMaximumScore now', E_USER_DEPRECATED); |
298
|
|
|
return $this->response->response->maxScore; |
299
|
|
|
} |
300
|
|
|
|
301
|
2 |
|
public function getDebugResponse() |
302
|
|
|
{ |
303
|
2 |
|
return $this->response->debug; |
304
|
|
|
} |
305
|
|
|
|
306
|
26 |
|
public function getHighlightedContent() |
307
|
|
|
{ |
308
|
26 |
|
$highlightedContent = false; |
309
|
|
|
|
310
|
26 |
|
if ($this->response->highlighting) { |
311
|
26 |
|
$highlightedContent = $this->response->highlighting; |
312
|
|
|
} |
313
|
|
|
|
314
|
26 |
|
return $highlightedContent; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|