-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for query suggestions (aka "did you mean") (#7)
- Loading branch information
1 parent
f4c6e83
commit d902a6f
Showing
4 changed files
with
139 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -23,20 +23,14 @@ | |
"require": { | ||
"php": "^8.1", | ||
"silverstripe/framework": "^5", | ||
"silverstripe/silverstripe-discoverer": "^1", | ||
"silverstripe/silverstripe-discoverer": "^1.1", | ||
"elastic/enterprise-search": "^8.10", | ||
"guzzlehttp/guzzle": "^7.5" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^9.6.19", | ||
"slevomat/coding-standard": "^8.8" | ||
}, | ||
"repositories": [ | ||
{ | ||
"type": "vcs", | ||
"url": "[email protected]:silverstripeltd/silverstripe-discoverer.git" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"SilverStripe\\DiscovererElasticEnterprise\\": "src/", | ||
|
This file contains 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,38 @@ | ||
<?php | ||
|
||
namespace SilverStripe\DiscovererElasticEnterprise\Processors; | ||
|
||
use Elastic\EnterpriseSearch\AppSearch\Schema\QuerySuggestionRequest; | ||
use SilverStripe\Core\Injector\Injectable; | ||
use SilverStripe\Discoverer\Query\Suggestion; | ||
use stdClass; | ||
|
||
class SuggestionParamsProcessor | ||
{ | ||
|
||
use Injectable; | ||
|
||
public function getQueryParams(Suggestion $suggestion): QuerySuggestionRequest | ||
{ | ||
$querySuggestionParams = new QuerySuggestionRequest(); | ||
$querySuggestionParams->query = $suggestion->getQueryString(); | ||
|
||
$limit = $suggestion->getLimit(); | ||
$fields = $suggestion->getFields(); | ||
|
||
if ($limit) { | ||
$querySuggestionParams->size = $limit; | ||
} | ||
|
||
if ($fields) { | ||
$types = new stdClass(); | ||
$types->documents = new stdClass(); | ||
$types->documents->fields = $fields; | ||
|
||
$querySuggestionParams->types = $types; | ||
} | ||
|
||
return $querySuggestionParams; | ||
} | ||
|
||
} |
This file contains 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,64 @@ | ||
<?php | ||
|
||
namespace SilverStripe\DiscovererElasticEnterprise\Processors; | ||
|
||
use Exception; | ||
use SilverStripe\Core\Injector\Injectable; | ||
use SilverStripe\Discoverer\Service\Results\Suggestions; | ||
|
||
class SuggestionsProcessor | ||
{ | ||
|
||
use Injectable; | ||
|
||
public function getProcessedSuggestions(Suggestions $suggestions, array $response): void | ||
{ | ||
// Check that we have all critical fields in our Elastic response | ||
$this->validateResponse($response); | ||
|
||
$documentSuggestions = $response['results']['documents'] ?? []; | ||
|
||
foreach ($documentSuggestions as $documentSuggestion) { | ||
$suggestion = $documentSuggestion['suggestion'] ?? null; | ||
|
||
if (!$suggestion) { | ||
continue; | ||
} | ||
|
||
$suggestions->addSuggestion($suggestion); | ||
} | ||
} | ||
|
||
private function validateResponse(array $response): void | ||
{ | ||
// If any errors are present, then let's throw and track what they were | ||
if (array_key_exists('errors', $response)) { | ||
throw new Exception(sprintf('Elastic response contained errors: %s', json_encode($response['errors']))); | ||
} | ||
|
||
// The top level fields that we expect to receive from Elastic for each search | ||
$meta = $response['meta'] ?? null; | ||
$results = $response['results'] ?? null; | ||
// Check if any required fields are missing | ||
$missingTopLevelFields = []; | ||
|
||
// Basic falsy check is fine here. An empty `meta` would still be an error | ||
if (!$meta) { | ||
$missingTopLevelFields[] = 'meta'; | ||
} | ||
|
||
// Specifically checking for null, because an empty results array is a valid response | ||
if ($results === null) { | ||
$missingTopLevelFields[] = 'results'; | ||
} | ||
|
||
// We were missing one or more required top level fields | ||
if ($missingTopLevelFields) { | ||
throw new Exception(sprintf( | ||
'Missing required top level fields for query suggestions: %s', | ||
implode(', ', $missingTopLevelFields) | ||
)); | ||
} | ||
} | ||
|
||
} |
This file contains 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