Passed
Pull Request — master (#2666)
by Rafael
50:52 queued 08:22
created

SolrWriteService::deleteByQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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