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\System\Logging\SolrLogManager; |
21
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter; |
22
|
|
|
use Solarium\QueryType\Extract\Query; |
23
|
|
|
use Solarium\QueryType\Update\Result; |
24
|
|
|
use Throwable; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class SolrWriteService |
28
|
|
|
* |
29
|
|
|
* @author Timo Hund <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class SolrWriteService extends AbstractSolrService |
32
|
|
|
{ |
33
|
|
|
const EXTRACT_SERVLET = 'update/extract'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Performs a content and meta data extraction request. |
37
|
|
|
* |
38
|
|
|
* @param Query $query An extraction query |
39
|
|
|
* @return array An array containing the extracted content [0] and metadata [1] |
40
|
|
|
*/ |
41
|
1 |
|
public function extractByQuery(Query $query): array |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
1 |
|
$response = $this->createAndExecuteRequest($query); |
45
|
1 |
|
return [$response->file, $response->file_metadata]; |
46
|
|
|
} catch (Throwable $e) { |
47
|
|
|
$param = $query->getRequestBuilder()->build($query)->getParams(); |
48
|
|
|
$this->logger->log( |
49
|
|
|
SolrLogManager::ERROR, |
50
|
|
|
'Extracting text and meta data through Solr Cell over HTTP POST', |
51
|
|
|
[ |
52
|
|
|
'query' => (array)$query, |
53
|
|
|
'parameters' => $param, |
54
|
|
|
'file' => $query->getFile(), |
55
|
|
|
'query url' => self::EXTRACT_SERVLET, |
56
|
|
|
'exception' => $e->getMessage(), |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Deletes all index documents of a certain type and does a commit |
66
|
|
|
* afterwards. |
67
|
|
|
* |
68
|
|
|
* @param string $type The type of documents to delete, usually a table name. |
69
|
|
|
* @param bool $commit Will commit immediately after deleting the documents if set, defaults to TRUE |
70
|
|
|
*/ |
71
|
|
|
public function deleteByType(string $type, bool $commit = true) |
72
|
|
|
{ |
73
|
|
|
$this->deleteByQuery('type:' . trim($type)); |
74
|
|
|
|
75
|
|
|
if ($commit) { |
76
|
|
|
$this->commit(false, false); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create the delete-query, which is document based on a query and submit it |
82
|
|
|
* |
83
|
|
|
* @param string $rawQuery Expected to be utf-8 encoded |
84
|
|
|
* @return ResponseAdapter |
85
|
|
|
*/ |
86
|
22 |
|
public function deleteByQuery(string $rawQuery): ResponseAdapter |
87
|
|
|
{ |
88
|
22 |
|
$query = $this->client->createUpdate(); |
89
|
22 |
|
$query->addDeleteQuery($rawQuery); |
90
|
22 |
|
return $this->createAndExecuteRequest($query); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Add an array of Solr Documents to the index all at once |
95
|
|
|
* |
96
|
|
|
* @param array $documents Should be an array of \ApacheSolrForTypo3\Solr\System\Solr\Document\Document instances |
97
|
|
|
* @return ResponseAdapter |
98
|
|
|
*/ |
99
|
89 |
|
public function addDocuments(array $documents): ResponseAdapter |
100
|
|
|
{ |
101
|
89 |
|
$update = $this->client->createUpdate(); |
102
|
89 |
|
$update->addDocuments($documents); |
103
|
89 |
|
return $this->createAndExecuteRequest($update); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Send a commit command. Will be synchronous unless both wait parameters are set to false. |
108
|
|
|
* |
109
|
|
|
* @param bool $expungeDeletes Defaults to false, merge segments with deletes away |
110
|
|
|
* @param bool $waitSearcher Defaults to true, block until a new searcher is opened and registered as the main query searcher, making the changes visible |
111
|
|
|
* @return ResponseAdapter |
112
|
|
|
*/ |
113
|
23 |
|
public function commit(bool $expungeDeletes = false, bool $waitSearcher = true): ResponseAdapter |
114
|
|
|
{ |
115
|
23 |
|
$update = $this->client->createUpdate(); |
116
|
23 |
|
$update->addCommit(false, $waitSearcher, $expungeDeletes); |
117
|
23 |
|
return $this->createAndExecuteRequest($update); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Optimize the solr index |
122
|
|
|
* |
123
|
|
|
* @return Result |
124
|
|
|
*/ |
125
|
1 |
|
public function optimizeIndex(): Result |
126
|
|
|
{ |
127
|
1 |
|
$update = $this->client->createUpdate(); |
128
|
1 |
|
$update->addOptimize(true, false, 5); |
129
|
1 |
|
return $this->client->update($update); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|