Passed
Pull Request — master (#166)
by
unknown
22:16
created

ElasticSearch::initializeIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 78
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 48
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 78
rs 9.1344

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace EWW\Dpf\Services\ElasticSearch;
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 Elasticsearch\ClientBuilder;
18
use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
19
use EWW\Dpf\Helper\ElasticsearchMapper;
20
use TYPO3\CMS\Extbase\Object\ObjectManager;
21
use EWW\Dpf\Configuration\ClientConfigurationManager;
22
use EWW\Dpf\Domain\Model\Document;
23
use EWW\Dpf\Helper\Mods;
24
use TYPO3\CMS\Core\Utility\GeneralUtility;
25
use TYPO3\CMS\Core\Log\LogManager;
26
27
class ElasticSearch
28
{
29
    /**
30
     * frontendUserHelper
31
     *
32
     * @var \EWW\Dpf\Helper\FrontendUserHelper
33
     * @inject
34
     */
35
    protected $frontendUserHelper = null;
36
37
38
    protected $client;
39
40
    protected $server = 'host.docker.internal'; //127.0.0.1';
41
42
    protected $port = '9200';
43
44
    protected $indexName = 'kitodo_publication';
45
46
    //protected $mapping = '';
47
48
    //protected $hits;
49
50
    protected $results;
51
52
    protected $elasticsearchMapper;
53
54
55
    /**
56
     * elasticsearch client constructor
57
     */
58
    public function __construct()
59
    {
60
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(ObjectManager::class);
61
62
        $this->elasticsearchMapper = $objectManager->get(ElasticsearchMapper::class);
63
64
        $clientConfigurationManager = $objectManager->get(ClientConfigurationManager::class);
65
66
        $this->server = $clientConfigurationManager->getElasticSearchHost();
67
        $this->port = $clientConfigurationManager->getElasticSearchPort();
68
69
        $hosts = array(
70
            $this->server . ':' . $this->port,
71
        );
72
73
        $clientBuilder = ClientBuilder::create();
74
        $clientBuilder->setHosts($hosts);
75
        $this->client = $clientBuilder->build();
76
77
        $this->initializeIndex($this->indexName);
78
79
    }
80
81
    /**
82
     * Creates an index named by $indexName if it doesn't exist.
83
     *
84
     * @param $indexName
85
     */
86
    protected function initializeIndex($indexName)
87
    {
88
        $paramsIndex = [
89
            'index' => $indexName,
90
            'body' => [
91
                'settings' => [
92
                    //'index.requests.cache.enable' => false,
93
                    'analysis' => [
94
                        'filter' => [
95
                            'ngram' => [
96
                                'type' => 'ngram',
97
                                'min_gram' => 3,
98
                                'max_gram' => 3,
99
                                'token_chars' => [
100
                                    'letter',
101
                                    'digit'
102
                                ],
103
                            ]
104
                        ],
105
                        'analyzer' => [
106
                            'keyword_lowercase' => [
107
                                'tokenizer' => 'keyword',
108
                                'filter' => ['lowercase']
109
                            ]
110
                        ]
111
                    ]
112
                ],
113
                'mappings' => [
114
                    '_source' => [
115
                        'enabled' => true
116
                    ],
117
                    //'dynamic' => 'strict',
118
                    'properties' => [
119
                        'title' => [
120
                            'type' => 'text',
121
                            'fields' => [
122
                                'keyword' => [
123
                                    'type' => 'text',
124
                                    'analyzer' => 'keyword_lowercase',
125
                                    'fielddata' => true
126
                                ]
127
                            ]
128
                        ],
129
                        'state' => [
130
                            'type' => 'keyword'
131
                        ],
132
                        'simpleState' => [
133
                            'type' => 'keyword'
134
                        ],
135
                        'year' => [
136
                            'type' => 'integer'
137
                        ],
138
                        'authorAndPublisher' => [
139
                            'type' => 'keyword'
140
                        ],
141
                        'doctype' => [
142
                            'type' => 'keyword'
143
                        ],
144
                        'collections' => [
145
                            'type' => 'keyword'
146
                        ],
147
                        'hasFiles' => [
148
                            'type' => 'keyword'
149
                        ],
150
                        'creator' => [
151
                            'type' => 'keyword'
152
                        ],
153
                        'creatorRole' => [
154
                            'type' => 'keyword'
155
                        ]
156
157
                    ]
158
                ]
159
            ]
160
        ];
161
162
        if (!$this->client->indices()->exists(['index' => $indexName])) {
163
            $this->client->indices()->create($paramsIndex);
164
        }
165
166
    }
167
168
    /**
169
     * Adds an document to the index.
170
     *
171
     * @param Document $document
172
     */
173
    public function index($document)
174
    {
175
        $data = json_decode($this->elasticsearchMapper->getElasticsearchJson($document));
176
177
        if ($data) {
178
179
            $data->state = $document->getState();
180
            $data->simpleState = DocumentWorkflow::STATE_TO_SIMPLESTATE_MAPPING[$document->getState()];
181
            $data->objectIdentifier = $document->getObjectIdentifier();
182
183
            $data->creator = $document->getCreator();
184
185
            $data->creatorRole = $this->frontendUserHelper->getUserRole($document->getCreator());
186
187
            $data->year = $document->getPublicationYear();
188
189
            $notes = $document->getNotes();
190
191
            if ($notes && is_array($notes)) {
192
                $data->notes = $notes;
193
            } else {
194
                $data->notes = array();
195
            }
196
197
            $files = $document->getFile();
198
            if ($files->count() > 0) {
199
                $data->hasFiles = true;
200
            } else {
201
                $data->hasFiles = false;
202
            }
203
204
205
            /** @var @var Mods $mods */
206
            $mods = new Mods($document->getXmlData());
207
208
            $authors = $mods->getAuthors();
209
            $publishers = $mods->getPublishers();
210
211
            $data->authorAndPublisher = array_merge($authors, $publishers);
212
213
            $data->originalSourceTitle = $mods->getOriginalSourceTitle();
214
215
            $this->client->index([
216
                'refresh' => 'wait_for',
217
                'index' => $this->indexName,
218
                'id' => $document->getDocumentIdentifier(),
219
                'body' => $data
220
            ]);
221
222
        }
223
224
    }
225
226
227
    /**
228
     * Deletes a document from the index
229
     *
230
     * @param string $identifier
231
     */
232
    public function delete($identifier)
233
    {
234
        try {
235
236
            $params = [
237
                'refresh' => 'wait_for',
238
                'index' => $this->indexName,
239
                'id' => $identifier
240
            ];
241
242
            $this->client->delete($params);
243
244
        } catch (\Exception $e) {
245
            /** @var $logger \TYPO3\CMS\Core\Log\Logger */
246
            $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
247
            $logger->warning('Document could not be deleted from the index.',
248
                [
249
                    'Document identifier' => $identifier
250
                ]
251
            );
252
        }
253
    }
254
255
256
    /**
257
     * @param $identifier
258
     */
259
    public function getDocument($identifier)
260
    {
261
        $params = [
262
            'index' => $this->indexName,
263
            'id'    => $identifier
264
        ];
265
266
        return $this->client->get($params);
267
    }
268
269
270
    /**
271
     * performs the
272
     * @param  array $query search query
273
     * @return array        result list
274
     */
275
    public function search($query, $type = null)
276
    {
277
        try {
278
            // define type and index
279
            if (empty($query['index'])) {
280
                $query['index'] = $this->indexName;
281
            }
282
            if (!empty($type)) {
283
                //$query['type'] = $type;
284
                // $query['type'] = $this->type;
285
            }
286
287
            // Search request
288
            $results = $this->client->search($query);
289
290
            //$this->hits = $results['hits']['total'];
291
292
            //$this->resultList = $results['hits'];
293
294
            $this->results = $results;
295
296
            return $this->results;
297
        } catch ( \Elasticsearch\Common\Exceptions\Curl\CouldNotConnectToHost $exception) {
298
            throw new \EWW\Dpf\Exceptions\ElasticSearchConnectionErrorException("Could not connect to repository server.");
299
        } catch (\Elasticsearch\Common\Exceptions\Curl\CouldNotResolveHostException $exception) {
300
            throw new \EWW\Dpf\Exceptions\ElasticSearchConnectionErrorException("Could not connect to repository server.");
301
        }
302
    }
303
304
    /**
305
     * Get the results
306
     * @return mixed
307
     */
308
    public function getResults()
309
    {
310
        // return results from the last search request
311
        return $this->results;
312
    }
313
314
315
    
316
317
318
}
319
320