Skip to content

Commit 24cf018

Browse files
committed
Update to include synonym support
1 parent 42fd9a3 commit 24cf018

13 files changed

Lines changed: 111 additions & 161 deletions

_config/synonyms.yml

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ Name: forager-bifrost-synonyms
33
Only:
44
envvarset: 'BIFROST_MANAGEMENT_API_KEY'
55
After:
6-
- 'silverstripe-forager-elastic-enterprise-synonyms'
76
- 'silverstripe-forager-synonyms'
87
---
98
SilverStripe\Core\Injector\Injector:
10-
# Adaptors provided by this module
119
SilverStripe\Forager\Interfaces\Requests\CreateSynonymRuleAdaptor:
1210
class: SilverStripe\ForagerBifrost\Adaptors\Requests\CreateSynonymRuleAdaptor
1311
SilverStripe\Forager\Interfaces\Requests\GetSynonymRuleAdaptor:
@@ -18,17 +16,5 @@ SilverStripe\Core\Injector\Injector:
1816
class: SilverStripe\ForagerBifrost\Adaptors\Requests\UpdateSynonymRuleAdaptor
1917
SilverStripe\Forager\Interfaces\Requests\DeleteSynonymRuleAdaptor:
2018
class: SilverStripe\ForagerBifrost\Adaptors\Requests\DeleteSynonymRuleAdaptor
21-
# Adaptors provided by the ElasticEnterprise dependency
2219
SilverStripe\Forager\Interfaces\Requests\GetSynonymCollectionsAdaptor:
23-
class: SilverStripe\ForagerElasticEnterprise\Adaptors\Requests\GetSynonymCollectionsAdaptor
24-
# Request overrides to work with the Bifröst API
25-
Elastic\EnterpriseSearch\AppSearch\Request\DeleteSynonymSet:
26-
class: SilverStripe\ForagerBifrost\Service\Requests\DeleteSynonymRule
27-
Elastic\EnterpriseSearch\AppSearch\Request\GetSynonymSet:
28-
class: SilverStripe\ForagerBifrost\Service\Requests\GetSynonymRule
29-
Elastic\EnterpriseSearch\AppSearch\Request\ListSynonymSets:
30-
class: SilverStripe\ForagerBifrost\Service\Requests\GetSynonymRules
31-
Elastic\EnterpriseSearch\AppSearch\Request\CreateSynonymSet:
32-
class: SilverStripe\ForagerBifrost\Service\Requests\CreateSynonymRule
33-
Elastic\EnterpriseSearch\AppSearch\Request\PutSynonymSet:
34-
class: SilverStripe\ForagerBifrost\Service\Requests\UpdateSynonymRule
20+
class: SilverStripe\ForagerBifrost\Adaptors\Requests\GetSynonymCollectionsAdaptor

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"silverstripe/framework": "^6",
2222
"silverstripe/reports": "^6",
2323
"silverstripe/silverstripe-forager": "dev-feature/v2-silverstripe-6",
24-
"silverstripe/silverstripe-search-client-php": "^0.0.1-alpha",
24+
"silverstripe/silverstripe-search-client-php": "dev-main",
2525
"guzzlehttp/guzzle": "^7.9"
2626
},
2727
"require-dev": {

src/Adaptors/Requests/CreateSynonymRuleAdaptor.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace SilverStripe\ForagerBifrost\Adaptors\Requests;
44

5-
use Elastic\EnterpriseSearch\Client;
6-
use SilverStripe\Core\Injector\Injector;
75
use SilverStripe\Forager\Interfaces\Requests\CreateSynonymRuleAdaptor as PostSynonymRuleAdaptorInterface;
6+
use SilverStripe\Forager\Service\IndexConfiguration;
87
use SilverStripe\Forager\Service\Query\SynonymRule as SynonymRuleQuery;
98
use SilverStripe\Forager\Service\Results\SynonymRule as SynonymRuleResult;
109
use SilverStripe\ForagerBifrost\Processors\SynonymRuleProcessor;
11-
use SilverStripe\ForagerBifrost\Service\Requests\CreateSynonymRule;
10+
use Silverstripe\Search\Client\Client;
11+
use Silverstripe\Search\Client\Exception\SynonymRulePostNotFoundException;
12+
use Silverstripe\Search\Client\Exception\SynonymRulePostUnprocessableEntityException;
13+
use Silverstripe\Search\Client\Model\SynonymRuleRequest;
1214

1315
class CreateSynonymRuleAdaptor implements PostSynonymRuleAdaptorInterface
1416
{
@@ -24,16 +26,24 @@ public function setClient(?Client $client): void
2426
$this->client = $client;
2527
}
2628

29+
/**
30+
* @throws SynonymRulePostNotFoundException
31+
* @throws SynonymRulePostUnprocessableEntityException
32+
*/
2733
public function process(int|string $synonymCollectionId, SynonymRuleQuery $synonymRule): SynonymRuleResult
2834
{
29-
$request = Injector::inst()->create(CreateSynonymRule::class, $synonymCollectionId, $synonymRule);
35+
// Silverstripe Search simply uses the engine name as the Synonym Collection ID
36+
$engineName = IndexConfiguration::singleton()->environmentizeIndex($synonymCollectionId);
37+
// Convert the query into a Silverstripe Search synonym rule string
38+
$synonyms = SynonymRuleProcessor::getStringFromQuery($synonymRule);
39+
$request = new SynonymRuleRequest();
40+
$request->setSynonyms($synonyms);
3041

3142
// Should either be successful or throw an exception, which we'll let fly
32-
$body = $this->client->appSearch()->createSynonymSet($request)->asString();
33-
$body = json_decode($body, true);
43+
$response = $this->client->synonymRulePost($engineName, $request);
3444

35-
$synonymRuleResult = SynonymRuleResult::create($body['id']);
36-
SynonymRuleProcessor::applyStringToResult($synonymRuleResult, $body['synonyms']);
45+
$synonymRuleResult = SynonymRuleResult::create($response->getId());
46+
SynonymRuleProcessor::applyStringToResult($synonymRuleResult, $response->getSynonyms());
3747

3848
return $synonymRuleResult;
3949
}

src/Adaptors/Requests/DeleteSynonymRuleAdaptor.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace SilverStripe\ForagerBifrost\Adaptors\Requests;
44

5-
use Elastic\EnterpriseSearch\AppSearch\Request\DeleteSynonymSet;
6-
use Elastic\EnterpriseSearch\Client;
7-
use SilverStripe\Core\Injector\Injector;
85
use SilverStripe\Forager\Interfaces\Requests\DeleteSynonymRuleAdaptor as DeleteSynonymRuleAdaptorInterface;
6+
use SilverStripe\Forager\Service\IndexConfiguration;
7+
use Silverstripe\Search\Client\Client;
8+
use Silverstripe\Search\Client\Exception\SynonymRuleDeleteNotFoundException;
9+
use Silverstripe\Search\Client\Exception\SynonymRuleDeleteUnprocessableEntityException;
910

1011
class DeleteSynonymRuleAdaptor implements DeleteSynonymRuleAdaptorInterface
1112
{
@@ -21,15 +22,19 @@ public function setClient(?Client $client): void
2122
$this->client = $client;
2223
}
2324

25+
/**
26+
* @throws SynonymRuleDeleteNotFoundException
27+
* @throws SynonymRuleDeleteUnprocessableEntityException
28+
*/
2429
public function process(int|string $synonymCollectionId, int|string $synonymRuleId): bool
2530
{
26-
$request = Injector::inst()->create(DeleteSynonymSet::class, $synonymCollectionId, $synonymRuleId);
31+
// Silverstripe Search simply uses the engine name as the Synonym Collection ID
32+
$engineName = IndexConfiguration::singleton()->environmentizeIndex($synonymCollectionId);
2733

2834
// Should either be successful or throw an exception, which we'll let fly
29-
$body = $this->client->appSearch()->deleteSynonymSet($request)->asString();
30-
$body = json_decode($body, true);
35+
$response = $this->client->synonymRuleDelete($synonymRuleId, $engineName);
3136

32-
return $body['success'];
37+
return $response->getSuccess();
3338
}
3439

3540
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace SilverStripe\ForagerBifrost\Adaptors\Requests;
4+
5+
use SilverStripe\Forager\Interfaces\Requests\GetSynonymCollectionsAdaptor as GetSynonymSetsAdaptorInterface;
6+
use SilverStripe\Forager\Service\IndexConfiguration;
7+
use SilverStripe\Forager\Service\Results\SynonymCollection;
8+
use SilverStripe\Forager\Service\Results\SynonymCollections;
9+
10+
class GetSynonymCollectionsAdaptor implements GetSynonymSetsAdaptorInterface
11+
{
12+
13+
private ?IndexConfiguration $configuration = null;
14+
15+
private static array $dependencies = [
16+
'configuration' => '%$' . IndexConfiguration::class,
17+
];
18+
19+
public function setConfiguration(IndexConfiguration $configuration): void
20+
{
21+
$this->configuration = $configuration;
22+
}
23+
24+
public function process(): SynonymCollections
25+
{
26+
$synonymCollections = SynonymCollections::create();
27+
28+
foreach (array_keys($this->configuration->getIndexes()) as $engineSuffix) {
29+
$synonymCollections->add(SynonymCollection::create($engineSuffix));
30+
}
31+
32+
return $synonymCollections;
33+
}
34+
35+
}

src/Adaptors/Requests/GetSynonymRuleAdaptor.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
namespace SilverStripe\ForagerBifrost\Adaptors\Requests;
44

5-
use Elastic\EnterpriseSearch\AppSearch\Request\GetSynonymSet;
6-
use Elastic\EnterpriseSearch\Client;
7-
use SilverStripe\Core\Injector\Injector;
85
use SilverStripe\Forager\Interfaces\Requests\GetSynonymRuleAdaptor as GetSynonymRuleAdaptorInterface;
6+
use SilverStripe\Forager\Service\IndexConfiguration;
97
use SilverStripe\Forager\Service\Results\SynonymRule;
108
use SilverStripe\ForagerBifrost\Processors\SynonymRuleProcessor;
9+
use Silverstripe\Search\Client\Client;
1110

1211
class GetSynonymRuleAdaptor implements GetSynonymRuleAdaptorInterface
1312
{
@@ -25,14 +24,14 @@ public function setClient(?Client $client): void
2524

2625
public function process(int|string $synonymCollectionId, int|string $synonymRuleId): SynonymRule
2726
{
28-
$request = Injector::inst()->create(GetSynonymSet::class, $synonymCollectionId, $synonymRuleId);
27+
// Silverstripe Search simply uses the engine name as the Synonym Collection ID
28+
$engineName = IndexConfiguration::singleton()->environmentizeIndex($synonymCollectionId);
2929

3030
// Should either be successful or throw an exception, which we'll let fly
31-
$body = $this->client->appSearch()->getSynonymSet($request)->asString();
32-
$body = json_decode($body, true);
31+
$response = $this->client->synonymRuleGet($synonymRuleId, $engineName);
3332

34-
$synonymRule = SynonymRule::create($body['id']);
35-
SynonymRuleProcessor::applyStringToResult($synonymRule, $body['synonyms']);
33+
$synonymRule = SynonymRule::create($response->getId());
34+
SynonymRuleProcessor::applyStringToResult($synonymRule, $response->getSynonyms());
3635

3736
return $synonymRule;
3837
}

src/Adaptors/Requests/GetSynonymRulesAdaptor.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace SilverStripe\ForagerBifrost\Adaptors\Requests;
44

5-
use Elastic\EnterpriseSearch\AppSearch\Request\ListSynonymSets;
6-
use Elastic\EnterpriseSearch\Client;
7-
use SilverStripe\Core\Injector\Injector;
85
use SilverStripe\Forager\Interfaces\Requests\GetSynonymRulesAdaptor as GetSynonymRulesAdaptorInterface;
6+
use SilverStripe\Forager\Service\IndexConfiguration;
97
use SilverStripe\Forager\Service\Results\SynonymRule;
108
use SilverStripe\Forager\Service\Results\SynonymRules;
119
use SilverStripe\ForagerBifrost\Processors\SynonymRuleProcessor;
10+
use Silverstripe\Search\Client\Client;
11+
use Silverstripe\Search\Client\Exception\SynonymRulesGetNotFoundException;
12+
use Silverstripe\Search\Client\Exception\SynonymRulesGetUnprocessableEntityException;
1213

1314
class GetSynonymRulesAdaptor implements GetSynonymRulesAdaptorInterface
1415
{
@@ -24,19 +25,27 @@ public function setClient(?Client $client): void
2425
$this->client = $client;
2526
}
2627

28+
/**
29+
* @throws SynonymRulesGetNotFoundException
30+
* @throws SynonymRulesGetUnprocessableEntityException
31+
*/
2732
public function process(int|string $synonymCollectionId): SynonymRules
2833
{
29-
$request = Injector::inst()->create(ListSynonymSets::class, $synonymCollectionId);
34+
// Silverstripe Search simply uses the engine name as the Synonym Collection ID
35+
$engineName = IndexConfiguration::singleton()->environmentizeIndex($synonymCollectionId);
3036

3137
// Should either be successful or throw an exception, which we'll let fly
32-
$body = $this->client->appSearch()->listSynonymSets($request)->asString();
33-
$body = json_decode($body, true);
34-
38+
$response = $this->client->synonymRulesGet($engineName);
3539
$synonymRules = SynonymRules::create();
3640

37-
foreach ($body as $result) {
38-
$synonymRule = SynonymRule::create($result['id']);
39-
SynonymRuleProcessor::applyStringToResult($synonymRule, $result['synonyms']);
41+
// Covers for either null being returned or an empty array
42+
if (!$response) {
43+
return $synonymRules;
44+
}
45+
46+
foreach ($response as $result) {
47+
$synonymRule = SynonymRule::create($result->getId());
48+
SynonymRuleProcessor::applyStringToResult($synonymRule, $result->getSynonyms());
4049

4150
$synonymRules->add($synonymRule);
4251
}

src/Adaptors/Requests/UpdateSynonymRuleAdaptor.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace SilverStripe\ForagerBifrost\Adaptors\Requests;
44

5-
use Elastic\EnterpriseSearch\Client;
6-
use SilverStripe\Core\Injector\Injector;
75
use SilverStripe\Forager\Interfaces\Requests\UpdateSynonymRuleAdaptor as PatchSynonymRuleAdaptorInterface;
6+
use SilverStripe\Forager\Service\IndexConfiguration;
87
use SilverStripe\Forager\Service\Query\SynonymRule as SynonymRuleQuery;
98
use SilverStripe\Forager\Service\Results\SynonymRule as SynonymRuleResult;
109
use SilverStripe\ForagerBifrost\Processors\SynonymRuleProcessor;
11-
use SilverStripe\ForagerBifrost\Service\Requests\UpdateSynonymRule;
10+
use Silverstripe\Search\Client\Client;
11+
use Silverstripe\Search\Client\Exception\SynonymRulePutNotFoundException;
12+
use Silverstripe\Search\Client\Exception\SynonymRulePutUnprocessableEntityException;
13+
use Silverstripe\Search\Client\Model\SynonymRuleRequest;
1214

1315
class UpdateSynonymRuleAdaptor implements PatchSynonymRuleAdaptorInterface
1416
{
@@ -24,24 +26,27 @@ public function setClient(?Client $client): void
2426
$this->client = $client;
2527
}
2628

29+
/**
30+
* @throws SynonymRulePutNotFoundException
31+
* @throws SynonymRulePutUnprocessableEntityException
32+
*/
2733
public function process(
2834
int|string $synonymCollectionId,
2935
int|string $synonymRuleId,
3036
SynonymRuleQuery $synonymRule
3137
): SynonymRuleResult {
32-
$request = Injector::inst()->create(
33-
UpdateSynonymRule::class,
34-
$synonymCollectionId,
35-
$synonymRuleId,
36-
$synonymRule
37-
);
38+
// Silverstripe Search simply uses the engine name as the Synonym Collection ID
39+
$engineName = IndexConfiguration::singleton()->environmentizeIndex($synonymCollectionId);
40+
// Convert the query into a Silverstripe Search synonym rule string
41+
$synonyms = SynonymRuleProcessor::getStringFromQuery($synonymRule);
42+
$request = new SynonymRuleRequest();
43+
$request->setSynonyms($synonyms);
3844

3945
// Should either be successful or throw an exception, which we'll let fly
40-
$body = $this->client->appSearch()->createSynonymSet($request)->asString();
41-
$body = json_decode($body, true);
46+
$response = $this->client->synonymRulePut($synonymRuleId, $engineName, $request);
4247

43-
$synonymRuleResult = SynonymRuleResult::create($body['id']);
44-
SynonymRuleProcessor::applyStringToResult($synonymRuleResult, $body['synonyms']);
48+
$synonymRuleResult = SynonymRuleResult::create($response->getId());
49+
SynonymRuleProcessor::applyStringToResult($synonymRuleResult, $response->getSynonyms());
4550

4651
return $synonymRuleResult;
4752
}

src/Service/Requests/CreateSynonymRule.php

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/Service/Requests/DeleteSynonymRule.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)