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 |
|
try { |
53
|
49 |
|
$request = $this->client->createRequest($query); |
54
|
|
|
$response = $this->executeRequest($request); |
55
|
49 |
|
$this->hasSearched = true; |
56
|
46 |
|
$this->responseCache = $response; |
57
|
46 |
|
} catch (HttpException $e) { |
58
|
|
|
$this->handleErrorResponses($e); |
59
|
3 |
|
} |
60
|
|
|
return $response; |
|
|
|
|
61
|
46 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns whether a search has been executed or not. |
65
|
|
|
* |
66
|
|
|
* @return bool TRUE if a search has been executed, FALSE otherwise |
67
|
|
|
*/ |
68
|
|
|
public function hasSearched(): bool |
69
|
1 |
|
{ |
70
|
|
|
return $this->hasSearched; |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the most recent response (if any) |
75
|
|
|
* |
76
|
|
|
* @return ResponseAdapter|null Most recent response, or NULL if a search has not been executed yet. |
77
|
|
|
*/ |
78
|
|
|
public function getResponse(): ?ResponseAdapter |
79
|
|
|
{ |
80
|
|
|
return $this->responseCache; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* This method maps the failed solr requests to a meaningful exception. |
85
|
|
|
* |
86
|
|
|
* @param HttpException $exception |
87
|
|
|
* @throws SolrCommunicationException |
88
|
|
|
*/ |
89
|
|
|
protected function handleErrorResponses(HttpException $exception) |
90
|
|
|
{ |
91
|
|
|
$status = $exception->getCode(); |
92
|
|
|
$message = $exception->getStatusMessage(); |
93
|
|
|
$solrResponse = new ResponseAdapter($exception->getBody()); |
94
|
|
|
|
95
|
|
|
if ($status === 0 || $status === 502) { |
96
|
|
|
$e = new SolrUnavailableException('Solr Server not available: ' . $message, 1505989391); |
97
|
|
|
$e->setSolrResponse($solrResponse); |
98
|
|
|
throw $e; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($status === 500) { |
102
|
|
|
$e = new SolrInternalServerErrorException('Internal Server error during search: ' . $message, 1505989897); |
103
|
|
|
$e->setSolrResponse($solrResponse); |
104
|
|
|
throw $e; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$e = new SolrCommunicationException('Invalid query. Solr returned an error: ' . $status . ' ' . $message, 1293109870); |
108
|
3 |
|
$e->setSolrResponse($solrResponse); |
109
|
|
|
|
110
|
3 |
|
throw $e; |
111
|
3 |
|
} |
112
|
|
|
} |
113
|
|
|
|