-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for synonym management #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2c40cb0
Add support for synonym management
chrispenny 72d9061
Peer review feedback
chrispenny 728df5d
Update response for create/update synonym
chrispenny 4f6b8a7
Remove explicit forager dependency
chrispenny 06c0051
Update forager minimum dependency
chrispenny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| --- | ||
| Name: forager-bifrost-synonyms | ||
| Only: | ||
| envvarset: 'BIFROST_MANAGEMENT_API_KEY' | ||
| After: | ||
| - 'silverstripe-forager-elastic-enterprise-synonyms' | ||
| - 'silverstripe-forager-synonyms' | ||
| --- | ||
| SilverStripe\Core\Injector\Injector: | ||
| # Adaptors provided by this module | ||
| SilverStripe\Forager\Interfaces\Requests\CreateSynonymRuleAdaptor: | ||
| class: SilverStripe\ForagerBifrost\Adaptors\Requests\CreateSynonymRuleAdaptor | ||
| SilverStripe\Forager\Interfaces\Requests\GetSynonymRuleAdaptor: | ||
| class: SilverStripe\ForagerBifrost\Adaptors\Requests\GetSynonymRuleAdaptor | ||
| SilverStripe\Forager\Interfaces\Requests\GetSynonymRulesAdaptor: | ||
| class: SilverStripe\ForagerBifrost\Adaptors\Requests\GetSynonymRulesAdaptor | ||
| SilverStripe\Forager\Interfaces\Requests\UpdateSynonymRuleAdaptor: | ||
| class: SilverStripe\ForagerBifrost\Adaptors\Requests\UpdateSynonymRuleAdaptor | ||
| SilverStripe\Forager\Interfaces\Requests\DeleteSynonymRuleAdaptor: | ||
| class: SilverStripe\ForagerBifrost\Adaptors\Requests\DeleteSynonymRuleAdaptor | ||
| # Adaptors provided by the ElasticEnterprise dependency | ||
| SilverStripe\Forager\Interfaces\Requests\GetSynonymCollectionsAdaptor: | ||
| class: SilverStripe\ForagerElasticEnterprise\Adaptors\Requests\GetSynonymCollectionsAdaptor | ||
| # Request overrides to work with the Bifröst API | ||
| Elastic\EnterpriseSearch\AppSearch\Request\DeleteSynonymSet: | ||
| class: SilverStripe\ForagerBifrost\Service\Requests\DeleteSynonymRule | ||
| Elastic\EnterpriseSearch\AppSearch\Request\GetSynonymSet: | ||
| class: SilverStripe\ForagerBifrost\Service\Requests\GetSynonymRule | ||
| Elastic\EnterpriseSearch\AppSearch\Request\ListSynonymSets: | ||
| class: SilverStripe\ForagerBifrost\Service\Requests\GetSynonymRules | ||
| Elastic\EnterpriseSearch\AppSearch\Request\CreateSynonymSet: | ||
| class: SilverStripe\ForagerBifrost\Service\Requests\CreateSynonymRule | ||
| Elastic\EnterpriseSearch\AppSearch\Request\PutSynonymSet: | ||
| class: SilverStripe\ForagerBifrost\Service\Requests\UpdateSynonymRule | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Adaptors\Requests; | ||
|
|
||
| use Elastic\EnterpriseSearch\Client; | ||
| use SilverStripe\Core\Injector\Injector; | ||
| use SilverStripe\Forager\Interfaces\Requests\CreateSynonymRuleAdaptor as PostSynonymRuleAdaptorInterface; | ||
| use SilverStripe\Forager\Service\Query\SynonymRule as SynonymRuleQuery; | ||
| use SilverStripe\Forager\Service\Results\SynonymRule as SynonymRuleResult; | ||
| use SilverStripe\ForagerBifrost\Processors\SynonymRuleProcessor; | ||
| use SilverStripe\ForagerBifrost\Service\Requests\CreateSynonymRule; | ||
|
|
||
| class CreateSynonymRuleAdaptor implements PostSynonymRuleAdaptorInterface | ||
| { | ||
|
|
||
| private ?Client $client = null; | ||
|
|
||
| private static array $dependencies = [ | ||
| 'client' => '%$' . Client::class . '.managementClient', | ||
| ]; | ||
|
|
||
| public function setClient(?Client $client): void | ||
| { | ||
| $this->client = $client; | ||
| } | ||
|
|
||
| public function process(int|string $synonymCollectionId, SynonymRuleQuery $synonymRule): SynonymRuleResult | ||
| { | ||
| $request = Injector::inst()->create(CreateSynonymRule::class, $synonymCollectionId, $synonymRule); | ||
|
|
||
| // Should either be successful or throw an exception, which we'll let fly | ||
| $body = $this->client->appSearch()->createSynonymSet($request)->asString(); | ||
| $body = json_decode($body, true); | ||
|
|
||
| $synonymRuleResult = SynonymRuleResult::create($body['id']); | ||
| SynonymRuleProcessor::applyStringToResult($synonymRuleResult, $body['synonyms']); | ||
|
|
||
| return $synonymRuleResult; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Adaptors\Requests; | ||
|
|
||
| use Elastic\EnterpriseSearch\AppSearch\Request\DeleteSynonymSet; | ||
| use Elastic\EnterpriseSearch\Client; | ||
| use SilverStripe\Core\Injector\Injector; | ||
| use SilverStripe\Forager\Interfaces\Requests\DeleteSynonymRuleAdaptor as DeleteSynonymRuleAdaptorInterface; | ||
|
|
||
| class DeleteSynonymRuleAdaptor implements DeleteSynonymRuleAdaptorInterface | ||
| { | ||
|
|
||
| private ?Client $client = null; | ||
|
|
||
| private static array $dependencies = [ | ||
| 'client' => '%$' . Client::class . '.managementClient', | ||
| ]; | ||
|
|
||
| public function setClient(?Client $client): void | ||
| { | ||
| $this->client = $client; | ||
| } | ||
|
|
||
| public function process(int|string $synonymCollectionId, int|string $synonymRuleId): bool | ||
| { | ||
| $request = Injector::inst()->create(DeleteSynonymSet::class, $synonymCollectionId, $synonymRuleId); | ||
|
|
||
| // Should either be successful or throw an exception, which we'll let fly | ||
| $body = $this->client->appSearch()->deleteSynonymSet($request)->asString(); | ||
| $body = json_decode($body, true); | ||
|
|
||
| return $body['success']; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Adaptors\Requests; | ||
|
|
||
| use Elastic\EnterpriseSearch\AppSearch\Request\GetSynonymSet; | ||
| use Elastic\EnterpriseSearch\Client; | ||
| use SilverStripe\Core\Injector\Injector; | ||
| use SilverStripe\Forager\Interfaces\Requests\GetSynonymRuleAdaptor as GetSynonymRuleAdaptorInterface; | ||
| use SilverStripe\Forager\Service\Results\SynonymRule; | ||
| use SilverStripe\ForagerBifrost\Processors\SynonymRuleProcessor; | ||
|
|
||
| class GetSynonymRuleAdaptor implements GetSynonymRuleAdaptorInterface | ||
| { | ||
|
|
||
| private ?Client $client = null; | ||
|
|
||
| private static array $dependencies = [ | ||
| 'client' => '%$' . Client::class . '.managementClient', | ||
| ]; | ||
|
|
||
| public function setClient(?Client $client): void | ||
| { | ||
| $this->client = $client; | ||
| } | ||
|
|
||
| public function process(int|string $synonymCollectionId, int|string $synonymRuleId): SynonymRule | ||
| { | ||
| $request = Injector::inst()->create(GetSynonymSet::class, $synonymCollectionId, $synonymRuleId); | ||
|
|
||
| // Should either be successful or throw an exception, which we'll let fly | ||
| $body = $this->client->appSearch()->getSynonymSet($request)->asString(); | ||
| $body = json_decode($body, true); | ||
|
|
||
| $synonymRule = SynonymRule::create($body['id']); | ||
| SynonymRuleProcessor::applyStringToResult($synonymRule, $body['synonyms']); | ||
|
|
||
| return $synonymRule; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Adaptors\Requests; | ||
|
|
||
| use Elastic\EnterpriseSearch\AppSearch\Request\ListSynonymSets; | ||
| use Elastic\EnterpriseSearch\Client; | ||
| use SilverStripe\Core\Injector\Injector; | ||
| use SilverStripe\Forager\Interfaces\Requests\GetSynonymRulesAdaptor as GetSynonymRulesAdaptorInterface; | ||
| use SilverStripe\Forager\Service\Results\SynonymRule; | ||
| use SilverStripe\Forager\Service\Results\SynonymRules; | ||
| use SilverStripe\ForagerBifrost\Processors\SynonymRuleProcessor; | ||
|
|
||
| class GetSynonymRulesAdaptor implements GetSynonymRulesAdaptorInterface | ||
| { | ||
|
|
||
| private ?Client $client = null; | ||
|
|
||
| private static array $dependencies = [ | ||
| 'client' => '%$' . Client::class . '.managementClient', | ||
| ]; | ||
|
|
||
| public function setClient(?Client $client): void | ||
| { | ||
| $this->client = $client; | ||
| } | ||
|
|
||
| public function process(int|string $synonymCollectionId): SynonymRules | ||
| { | ||
| $request = Injector::inst()->create(ListSynonymSets::class, $synonymCollectionId); | ||
|
|
||
| // Should either be successful or throw an exception, which we'll let fly | ||
| $body = $this->client->appSearch()->listSynonymSets($request)->asString(); | ||
| $body = json_decode($body, true); | ||
|
|
||
| $synonymRules = SynonymRules::create(); | ||
|
|
||
| foreach ($body as $result) { | ||
| $synonymRule = SynonymRule::create($result['id']); | ||
| SynonymRuleProcessor::applyStringToResult($synonymRule, $result['synonyms']); | ||
|
|
||
| $synonymRules->add($synonymRule); | ||
| } | ||
|
|
||
| return $synonymRules; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Adaptors\Requests; | ||
|
|
||
| use Elastic\EnterpriseSearch\Client; | ||
| use SilverStripe\Core\Injector\Injector; | ||
| use SilverStripe\Forager\Interfaces\Requests\UpdateSynonymRuleAdaptor as PatchSynonymRuleAdaptorInterface; | ||
| use SilverStripe\Forager\Service\Query\SynonymRule as SynonymRuleQuery; | ||
| use SilverStripe\Forager\Service\Results\SynonymRule as SynonymRuleResult; | ||
| use SilverStripe\ForagerBifrost\Processors\SynonymRuleProcessor; | ||
| use SilverStripe\ForagerBifrost\Service\Requests\UpdateSynonymRule; | ||
|
|
||
| class UpdateSynonymRuleAdaptor implements PatchSynonymRuleAdaptorInterface | ||
| { | ||
|
|
||
| private ?Client $client = null; | ||
|
|
||
| private static array $dependencies = [ | ||
| 'client' => '%$' . Client::class . '.managementClient', | ||
| ]; | ||
|
|
||
| public function setClient(?Client $client): void | ||
| { | ||
| $this->client = $client; | ||
| } | ||
|
|
||
| public function process( | ||
| int|string $synonymCollectionId, | ||
| int|string $synonymRuleId, | ||
| SynonymRuleQuery $synonymRule | ||
| ): SynonymRuleResult { | ||
| $request = Injector::inst()->create( | ||
| UpdateSynonymRule::class, | ||
| $synonymCollectionId, | ||
| $synonymRuleId, | ||
| $synonymRule | ||
| ); | ||
|
|
||
| // Should either be successful or throw an exception, which we'll let fly | ||
| $body = $this->client->appSearch()->createSynonymSet($request)->asString(); | ||
| $body = json_decode($body, true); | ||
|
|
||
| $synonymRuleResult = SynonymRuleResult::create($body['id']); | ||
| SynonymRuleProcessor::applyStringToResult($synonymRuleResult, $body['synonyms']); | ||
|
|
||
| return $synonymRuleResult; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Processors; | ||
|
|
||
| use SilverStripe\Core\Injector\Injectable; | ||
| use SilverStripe\Forager\Service\Query\SynonymRule as SynonymRuleQuery; | ||
| use SilverStripe\Forager\Service\Results\SynonymRule as SynonymRuleResult; | ||
|
|
||
| class SynonymRuleProcessor | ||
| { | ||
|
|
||
| use Injectable; | ||
|
|
||
| public static function getStringFromQuery(SynonymRuleQuery $synonymRule): string | ||
| { | ||
| if ($synonymRule->getType() === SynonymRuleResult::TYPE_EQUIVALENT) { | ||
| return implode(', ', $synonymRule->getSynonyms()); | ||
| } | ||
|
|
||
| return sprintf( | ||
| '%s => %s', | ||
| implode(', ', $synonymRule->getRoot()), | ||
| implode(', ', $synonymRule->getSynonyms()) | ||
| ); | ||
| } | ||
|
|
||
| public static function applyStringToResult(SynonymRuleResult $synonymRule, string $synonymsString): void | ||
| { | ||
| $type = str_contains($synonymsString, '=>') | ||
| ? SynonymRuleResult::TYPE_DIRECTIONAL | ||
| : SynonymRuleResult::TYPE_EQUIVALENT; | ||
|
|
||
| $synonymRule->setType($type); | ||
|
|
||
| if ($synonymRule->getType() === SynonymRuleResult::TYPE_DIRECTIONAL) { | ||
| $split = explode('=>', $synonymsString); | ||
| // We'd expect there to always be 2 items | ||
| $root = trim($split[0]); | ||
| $synonyms = trim($split[1]); | ||
|
|
||
| $synonymRule->setRoot(array_map('trim', explode(',', $root))); | ||
| $synonymRule->setSynonyms(array_map('trim', explode(',', $synonyms))); | ||
| } else { | ||
| $synonymRule->setSynonyms(array_map('trim', explode(',', $synonymsString))); | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Service\Requests; | ||
|
|
||
| use Elastic\EnterpriseSearch\AppSearch\Request\CreateSynonymSet; | ||
| use SilverStripe\Forager\Service\Query\SynonymRule as SynonymRuleQuery; | ||
| use SilverStripe\ForagerBifrost\Processors\SynonymRuleProcessor; | ||
| use stdClass; | ||
|
|
||
| class CreateSynonymRule extends CreateSynonymSet | ||
| { | ||
|
|
||
| public function __construct(string $synonymCollectionId, SynonymRuleQuery $synonymRule) | ||
| { | ||
| $body = new stdClass(); | ||
| $body->synonyms = SynonymRuleProcessor::singleton()->getStringFromQuery($synonymRule); | ||
|
|
||
| $this->method = 'POST'; | ||
| $this->headers['Content-Type'] = 'application/json'; | ||
| $this->path = sprintf('/api/v1/%s/synonyms', $synonymCollectionId); | ||
| $this->body = $body; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Service\Requests; | ||
|
|
||
| use Elastic\EnterpriseSearch\AppSearch\Request\DeleteSynonymSet; | ||
|
|
||
| class DeleteSynonymRule extends DeleteSynonymSet | ||
| { | ||
|
|
||
| public function __construct(string $synonymCollectionId, string $synonymRuleId) | ||
| { | ||
| parent::__construct($synonymCollectionId, $synonymRuleId); | ||
|
|
||
| $this->path = sprintf('/api/v1/%s/synonyms/%s', $synonymCollectionId, $synonymRuleId); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Service\Requests; | ||
|
|
||
| use Elastic\EnterpriseSearch\AppSearch\Request\GetSynonymSet; | ||
|
|
||
| class GetSynonymRule extends GetSynonymSet | ||
| { | ||
|
|
||
| public function __construct(string $synonymCollectionId, string $synonymRuleId) | ||
| { | ||
| parent::__construct($synonymCollectionId, $synonymRuleId); | ||
|
|
||
| $this->path = sprintf('/api/v1/%s/synonyms/%s', $synonymCollectionId, $synonymRuleId); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| namespace SilverStripe\ForagerBifrost\Service\Requests; | ||
|
|
||
| use Elastic\EnterpriseSearch\AppSearch\Request\ListSynonymSets; | ||
|
|
||
| class GetSynonymRules extends ListSynonymSets | ||
| { | ||
|
|
||
| public function __construct(string $synonymCollectionId) | ||
| { | ||
| parent::__construct($synonymCollectionId); | ||
|
|
||
| $this->path = sprintf('/api/v1/%s/synonyms', $synonymCollectionId); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.