transformBaseSynonyms()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * Class ElasticConfigureSynonymsTask|Firesphere\ElasticSearch\Tasks\ElasticConfigureSynonymsTask Index Elastic cores
4
 *
5
 * @package Firesphere\Elastic\Search
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\ElasticSearch\Tasks;
11
12
use Elastic\Elasticsearch\Exception\ClientResponseException;
13
use Elastic\Elasticsearch\Exception\MissingParameterException;
14
use Elastic\Elasticsearch\Exception\ServerResponseException;
15
use Firesphere\ElasticSearch\Models\SynonymSet;
16
use Firesphere\ElasticSearch\Services\ElasticCoreService;
17
use Firesphere\SearchBackend\Helpers\Synonyms as BaseSynonyms;
18
use Firesphere\SearchBackend\Models\SearchSynonym;
19
use SilverStripe\Core\Injector\Injector;
20
use SilverStripe\Dev\BuildTask;
21
22
/**
23
 * Class ElasticConfigureSynonymsTask
24
 *
25
 * @description Index synonyms to Elastic through a tasks
26
 * @package Firesphere\Elastic\Search
27
 */
28
class ElasticConfigureSynonymsTask extends BuildTask
29
{
30
    /**
31
     * URLSegment of this task
32
     *
33
     * @var string
34
     */
35
    private static $segment = 'ElasticSynonymTask';
0 ignored issues
show
introduced by
The private property $segment is not used, and could be removed.
Loading history...
36
    /**
37
     * My name
38
     *
39
     * @var string
40
     */
41
    protected $title = 'Add/update synonyms in Elasticsearch';
42
    /**
43
     * What do I do?
44
     *
45
     * @var string
46
     */
47
    protected $description = 'Add or update synonyms to Elastic.';
48
49
    /**
50
     * @throws ClientResponseException
51
     * @throws ServerResponseException
52
     * @throws MissingParameterException
53
     */
54
    public function run($request)
55
    {
56
        $service = Injector::inst()->get(ElasticCoreService::class);
57
        $client = $service->getClient();
58
        $baseSynonyms = BaseSynonyms::getSynonymsFlattened();
59
        $baseSynonyms = $this->transformBaseSynonyms($baseSynonyms);
60
        $configuredSynonyms = SearchSynonym::get()->map('getModifiedID', 'getCombinedSynonym')->toArray();
61
        $configuredSynonyms = $this->transformBaseSynonyms($configuredSynonyms, '');
62
        // Note, the Elastic synonym class is not suitable for a bulk import
63
        $client->synonyms()->putSynonym([
64
            'id'   => SynonymSet::get()->first()->Key,
65
            'body' => ["synonyms_set" => array_merge($baseSynonyms, $configuredSynonyms)]
66
        ]);
67
    }
68
69
    private function transformBaseSynonyms(array $synonyms, string $prefix = 'base')
70
    {
71
        $return = [];
72
        foreach ($synonyms as $key => $synonym) {
73
            if (strlen($prefix) > 0) {
74
                $key = sprintf("%s-%s", $prefix, $key);
75
            }
76
            $return[] = ["id" => $key, "synonyms" => $synonym];
77
        }
78
79
        return $return;
80
    }
81
}
82