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
![]() |
|||
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 |