Passed
Push — master ( cc3f84...4a930e )
by Timo
23:43
created

SolrWriteService::deleteByQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\Solr\Service;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2017 Timo Hund <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
28
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
29
use Solarium\QueryType\Extract\Query;
30
31
/**
32
 * Class SolrReadService
33
 * @package ApacheSolrForTypo3\System\Solr\Service
34
 */
35
class SolrWriteService extends AbstractSolrService
36
{
37
    const EXTRACT_SERVLET = 'update/extract';
38
39
    /**
40
     * Performs a content and meta data extraction request.
41
     *
42
     * @param Query $query An extraction query
43
     * @return array An array containing the extracted content [0] and meta data [1]
44
     */
45 1
    public function extractByQuery(Query $query)
46
    {
47
        try {
48 1
            $response = $this->createAndExecuteRequest($query);
49 1
            $fileName = basename($query->getFile());
50 1
            $metaKey = $fileName . '_metadata';
51 1
            return [$response->{$fileName}, (array)$response->{$metaKey}];
52
        } catch (\Exception $e) {
53
            $param = $query->getRequestBuilder()->build($query)->getParams();
54
            $this->logger->log(
55
                SolrLogManager::ERROR,
56
                'Extracting text and meta data through Solr Cell over HTTP POST',
57
                [
58
                    'query' => (array)$query,
59
                    'parameters' => $param,
60
                    'file' => $query->getFile(),
61
                    'query url' => self::EXTRACT_SERVLET,
62
                    'exception' => $e->getMessage()
63
                ]
64
            );
65
        }
66
67
        return [];
68
    }
69
70
    /**
71
     * Deletes all index documents of a certain type and does a commit
72
     * afterwards.
73
     *
74
     * @param string $type The type of documents to delete, usually a table name.
75
     * @param bool $commit Will commit immediately after deleting the documents if set, defaults to TRUE
76
     */
77
    public function deleteByType($type, $commit = true)
78
    {
79
        $this->deleteByQuery('type:' . trim($type));
80
81
        if ($commit) {
82
            $this->commit(false, false);
83
        }
84
    }
85
86
    /**
87
     * Create a delete document based on a query and submit it
88
     *
89
     * @param string $rawQuery Expected to be utf-8 encoded
90
     * @return ResponseAdapter
91
     */
92 11
    public function deleteByQuery($rawQuery) {
93 11
        $query = $this->client->createUpdate();
94 11
        $query->addDeleteQuery($rawQuery);
95 11
        return $this->createAndExecuteRequest($query);
96
    }
97
98
    /**
99
     * Add an array of Solr Documents to the index all at once
100
     *
101
     * @param array $documents Should be an array of Apache_Solr_Document instances
102
     * @return ResponseAdapter
103
     */
104 86
    public function addDocuments($documents)
105
    {
106 86
        $update = $this->client->createUpdate();
107 86
        $update->addDocuments($documents);
108 86
        return $this->createAndExecuteRequest($update);
109
    }
110
111
    /**
112
     * Send a commit command.  Will be synchronous unless both wait parameters are set to false.
113
     *
114
     * @param boolean $expungeDeletes Defaults to false, merge segments with deletes away
115
     * @param boolean $waitSearcher Defaults to true, block until a new searcher is opened and registered as the main query searcher, making the changes visible
116
     * @return ResponseAdapter
117
     */
118 15
    public function commit($expungeDeletes = false, $waitSearcher = true)
119
    {
120 15
        $update = $this->client->createUpdate();
121 15
        $update->addCommit(false, $waitSearcher, $expungeDeletes);
122 15
        return $this->createAndExecuteRequest($update);
123
    }
124
}