|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the TYPO3 CMS project. |
|
7
|
|
|
* |
|
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
10
|
|
|
* of the License, or any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please read the |
|
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* The TYPO3 project - inspiring people to share! |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Solr\Service; |
|
19
|
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query; |
|
21
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter; |
|
22
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrCommunicationException; |
|
23
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrInternalServerErrorException; |
|
24
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrUnavailableException; |
|
25
|
|
|
use RuntimeException; |
|
26
|
|
|
use Solarium\Exception\HttpException; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class SolrReadService |
|
30
|
|
|
*/ |
|
31
|
|
|
class SolrReadService extends AbstractSolrService |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var bool |
|
35
|
|
|
*/ |
|
36
|
|
|
protected bool $hasSearched = false; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ResponseAdapter|null |
|
40
|
|
|
*/ |
|
41
|
|
|
protected ?ResponseAdapter $responseCache = null; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Performs a search. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Query $query |
|
47
|
|
|
* @return ResponseAdapter Solr response |
|
48
|
|
|
* @throws RuntimeException if Solr returns a HTTP status code other than 200 |
|
49
|
|
|
*/ |
|
50
|
49 |
|
public function search(Query $query): ResponseAdapter |
|
51
|
|
|
{ |
|
52
|
49 |
|
$request = $this->client->createRequest($query); |
|
53
|
49 |
|
$response = $this->executeRequest($request); |
|
54
|
|
|
|
|
55
|
49 |
|
if ($response->getHttpStatus() === 200) { |
|
56
|
46 |
|
$this->hasSearched = true; |
|
57
|
46 |
|
$this->responseCache = $response; |
|
58
|
|
|
} else { |
|
59
|
3 |
|
$this->handleErrorResponse($response); |
|
60
|
|
|
} |
|
61
|
46 |
|
return $response; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns whether a search has been executed or not. |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool TRUE if a search has been executed, FALSE otherwise |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function hasSearched(): bool |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->hasSearched; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Gets the most recent response (if any) |
|
76
|
|
|
* |
|
77
|
|
|
* @return ResponseAdapter|null Most recent response, or NULL if a search has not been executed yet. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getResponse(): ?ResponseAdapter |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->responseCache; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* This method maps the failed solr requests to a meaningful exception. |
|
86
|
|
|
* |
|
87
|
|
|
* @param HttpException $exception |
|
88
|
|
|
* @throws SolrCommunicationException |
|
89
|
|
|
* @deprecated handleErrorResponses is deprecated and will be removed in v12, use handleErrorResponse() instead |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function handleErrorResponses(HttpException $exception) |
|
92
|
|
|
{ |
|
93
|
|
|
trigger_error( |
|
94
|
|
|
'handleErrorResponses() is deprecated and will be removed in v12, use handleErrorResponse() instead', |
|
95
|
|
|
E_USER_DEPRECATED |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
$solrResponse = new ResponseAdapter($exception->getBody(), $exception->getCode(), $exception->getStatusMessage()); |
|
99
|
|
|
$this->handleErrorResponse($solrResponse); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* This method handles a failed Solr request and maps it to a meaningful exception. |
|
104
|
|
|
* |
|
105
|
|
|
* @return ResponseAdapter $response |
|
106
|
|
|
* @throws SolrCommunicationException |
|
107
|
|
|
*/ |
|
108
|
3 |
|
protected function handleErrorResponse(ResponseAdapter $response): void |
|
109
|
|
|
{ |
|
110
|
3 |
|
$status = $response->getHttpStatus(); |
|
111
|
3 |
|
$message = $response->getHttpStatusMessage(); |
|
112
|
|
|
|
|
113
|
3 |
|
if ($status === 0 || $status === 502) { |
|
114
|
1 |
|
$e = new SolrUnavailableException('Solr Server not available: ' . $message, 1505989391); |
|
115
|
1 |
|
$e->setSolrResponse($response); |
|
116
|
1 |
|
throw $e; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
2 |
|
if ($status === 500) { |
|
120
|
1 |
|
$e = new SolrInternalServerErrorException('Internal Server error during search: ' . $message, 1505989897); |
|
121
|
1 |
|
$e->setSolrResponse($response); |
|
122
|
1 |
|
throw $e; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
$e = new SolrCommunicationException('Invalid query. Solr returned an error: ' . $status . ' ' . $message, 1293109870); |
|
126
|
1 |
|
$e->setSolrResponse($response); |
|
127
|
|
|
|
|
128
|
1 |
|
throw $e; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|