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