Skip to content

Commit

Permalink
Interface changes for SearchService & Suggestions. Support spelling s…
Browse files Browse the repository at this point in the history
…uggestions (#21)
  • Loading branch information
chrispenny authored Nov 6, 2024
1 parent 47e7feb commit 1ebe7fc
Show file tree
Hide file tree
Showing 16 changed files with 209 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{*.yml}]
[*.yml]
indent_size = 2
indent_style = space

Expand Down
12 changes: 12 additions & 0 deletions _config/adaptors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
Name: discoverer-adaptors
---
SilverStripe\Core\Injector\Injector:
SilverStripe\Discoverer\Service\Interfaces\ProcessAnalyticsAdaptor:
class: SilverStripe\Discoverer\Service\Adaptors\ProcessAnalyticsAdaptor
SilverStripe\Discoverer\Service\Interfaces\QuerySuggestionAdaptor:
class: SilverStripe\Discoverer\Service\Adaptors\QuerySuggestionAdaptor
SilverStripe\Discoverer\Service\Interfaces\SearchAdaptor:
class: SilverStripe\Discoverer\Service\Adaptors\SearchAdaptor
SilverStripe\Discoverer\Service\Interfaces\SpellingSuggestionAdaptor:
class: SilverStripe\Discoverer\Service\Adaptors\SpellingSuggestionAdaptor
2 changes: 1 addition & 1 deletion _config/analytics.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
Name: search-analytics
Name: discoverer-analytics
After:
- requestprocessors
Only:
Expand Down
20 changes: 18 additions & 2 deletions src/Query/Suggestion.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class Suggestion

use Injectable;

public function __construct(private string $queryString, private ?int $limit = null, private array $fields = [])
{
public function __construct(
private string $queryString,
private ?int $limit = null,
private array $fields = [],
private bool $formatted = false
) {
}

public function getQueryString(): string
Expand Down Expand Up @@ -54,4 +58,16 @@ public function addField(string $fieldName): self
return $this;
}

public function isFormatted(): bool
{
return $this->formatted;
}

public function setFormatted(bool $formatted): Suggestion
{
$this->formatted = $formatted;

return $this;
}

}
17 changes: 17 additions & 0 deletions src/Service/Adaptors/ProcessAnalyticsAdaptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace SilverStripe\Discoverer\Service\Adaptors;

use BadMethodCallException;
use SilverStripe\Discoverer\Analytics\AnalyticsData;
use SilverStripe\Discoverer\Service\Interfaces\ProcessAnalyticsAdaptor as ProcessAnalyticsAdaptorInterface;

class ProcessAnalyticsAdaptor implements ProcessAnalyticsAdaptorInterface
{

public function process(AnalyticsData $analyticsData): void
{
throw new BadMethodCallException('Analytics adaptor has not been implemented');
}

}
18 changes: 18 additions & 0 deletions src/Service/Adaptors/QuerySuggestionAdaptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SilverStripe\Discoverer\Service\Adaptors;

use BadMethodCallException;
use SilverStripe\Discoverer\Query\Suggestion;
use SilverStripe\Discoverer\Service\Interfaces\QuerySuggestionAdaptor as QuerySuggestionAdaptorInterface;
use SilverStripe\Discoverer\Service\Results\Suggestions;

class QuerySuggestionAdaptor implements QuerySuggestionAdaptorInterface
{

public function process(Suggestion $suggestion, string $indexName): Suggestions
{
throw new BadMethodCallException('Query suggestion adaptor has not been implemented');
}

}
18 changes: 18 additions & 0 deletions src/Service/Adaptors/SearchAdaptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SilverStripe\Discoverer\Service\Adaptors;

use BadMethodCallException;
use SilverStripe\Discoverer\Query\Query;
use SilverStripe\Discoverer\Service\Interfaces\SearchAdaptor as SearchAdaptorInterface;
use SilverStripe\Discoverer\Service\Results\Results;

class SearchAdaptor implements SearchAdaptorInterface
{

public function process(Query $query, string $indexName): Results
{
throw new BadMethodCallException('Search adaptor has not been implemented');
}

}
18 changes: 18 additions & 0 deletions src/Service/Adaptors/SpellingSuggestionAdaptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SilverStripe\Discoverer\Service\Adaptors;

use BadMethodCallException;
use SilverStripe\Discoverer\Query\Suggestion;
use SilverStripe\Discoverer\Service\Interfaces\SpellingSuggestionAdaptor as SpellingSuggestionAdaptorInterface;
use SilverStripe\Discoverer\Service\Results\Suggestions;

class SpellingSuggestionAdaptor implements SpellingSuggestionAdaptorInterface
{

public function process(Suggestion $suggestion, string $indexName): Suggestions
{
throw new BadMethodCallException('Spelling suggestion adaptor has not been implemented');
}

}
12 changes: 12 additions & 0 deletions src/Service/Interfaces/ProcessAnalyticsAdaptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace SilverStripe\Discoverer\Service\Interfaces;

use SilverStripe\Discoverer\Analytics\AnalyticsData;

interface ProcessAnalyticsAdaptor
{

public function process(AnalyticsData $analyticsData): void;

}
13 changes: 13 additions & 0 deletions src/Service/Interfaces/QuerySuggestionAdaptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SilverStripe\Discoverer\Service\Interfaces;

use SilverStripe\Discoverer\Query\Suggestion;
use SilverStripe\Discoverer\Service\Results\Suggestions;

interface QuerySuggestionAdaptor
{

public function process(Suggestion $suggestion, string $indexName): Suggestions;

}
13 changes: 13 additions & 0 deletions src/Service/Interfaces/SearchAdaptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SilverStripe\Discoverer\Service\Interfaces;

use SilverStripe\Discoverer\Query\Query;
use SilverStripe\Discoverer\Service\Results\Results;

interface SearchAdaptor
{

public function process(Query $query, string $indexName): Results;

}
13 changes: 13 additions & 0 deletions src/Service/Interfaces/SpellingSuggestionAdaptor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace SilverStripe\Discoverer\Service\Interfaces;

use SilverStripe\Discoverer\Query\Suggestion;
use SilverStripe\Discoverer\Service\Results\Suggestions;

interface SpellingSuggestionAdaptor
{

public function process(Suggestion $suggestion, string $indexName): Suggestions;

}
38 changes: 14 additions & 24 deletions src/Service/Results/Suggestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use ArrayIterator;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\FieldType\DBText;
use SilverStripe\View\ViewableData;
use Traversable;

Expand All @@ -28,6 +27,9 @@ class Suggestions extends ViewableData

private bool $success = false;

/**
* @var Field[]
*/
private array $suggestions = [];

public function forTemplate(): DBHTMLText
Expand Down Expand Up @@ -71,9 +73,9 @@ public function setSuccess(bool $success): self
return $this;
}

public function addSuggestion(string $suggestions): self
public function addSuggestion(Field $suggestion): self
{
$this->suggestions[] = $suggestions;
$this->suggestions[] = $suggestion;

return $this;
}
Expand All @@ -83,35 +85,23 @@ public function getSuggestions(): array
return $this->suggestions;
}

public function getIterator(): Traversable
public function setSuggestions(array $suggestions): self
{
if (!$this->suggestions) {
return new ArrayIterator();
// Specifically using a loop and addSuggestion() to make sure that all items are of type Field
foreach ($suggestions as $suggestion) {
$this->addSuggestion($suggestion);
}

return new ArrayIterator($this->convertArrayForTemplate());
return $this;
}

/**
* Silverstripe 5.3 will have native support for looping primitives in templates:
* https://github.com/silverstripe/silverstripe-framework/issues/11196
*
* We need to support versions of Silverstripe below 5.3 though, so we need this polyfill. It emulates the
* template implementation method from Silverstripe 5.3, so we should be able to remove this later without
* negatively impacting any project's template implementation
*/
private function convertArrayForTemplate(): array
public function getIterator(): Traversable
{
$arrayList = [];

foreach ($this->suggestions as $suggestion) {
$text = DBText::create('suggestion');
$text->setValue($suggestion);

$arrayList[] = $text;
if (!$this->suggestions) {
return new ArrayIterator();
}

return $arrayList;
return new ArrayIterator($this->getSuggestions());
}

}
47 changes: 40 additions & 7 deletions src/Service/SearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
use SilverStripe\Discoverer\Analytics\AnalyticsData;
use SilverStripe\Discoverer\Query\Query;
use SilverStripe\Discoverer\Query\Suggestion;
use SilverStripe\Discoverer\Service\Interfaces\ProcessAnalyticsAdaptor;
use SilverStripe\Discoverer\Service\Interfaces\QuerySuggestionAdaptor;
use SilverStripe\Discoverer\Service\Interfaces\SearchAdaptor;
use SilverStripe\Discoverer\Service\Interfaces\SpellingSuggestionAdaptor;
use SilverStripe\Discoverer\Service\Results\Results;
use SilverStripe\Discoverer\Service\Results\Suggestions;

Expand All @@ -14,30 +18,59 @@ class SearchService

use Injectable;

private ?SearchServiceAdaptor $adaptor = null;
private ?SearchAdaptor $searchAdaptor = null;

private ?QuerySuggestionAdaptor $querySuggestionAdaptor = null;

private ?SpellingSuggestionAdaptor $spellingSuggestionAdaptor = null;

private ?ProcessAnalyticsAdaptor $processAnalyticsAdaptor = null;

private static array $dependencies = [
'adaptor' => '%$' . SearchServiceAdaptor::class,
'searchAdaptor' => '%$' . SearchAdaptor::class,
'querySuggestionAdaptor' => '%$' . QuerySuggestionAdaptor::class,
'spellingSuggestionAdaptor' => '%$' . SpellingSuggestionAdaptor::class,
'processAnalyticsAdaptor' => '%$' . ProcessAnalyticsAdaptor::class,
];

public function setAdaptor(?SearchServiceAdaptor $adaptor): void
public function setSearchAdaptor(?SearchAdaptor $searchAdaptor): void
{
$this->searchAdaptor = $searchAdaptor;
}

public function setQuerySuggestionAdaptor(?QuerySuggestionAdaptor $querySuggestionAdaptor): void
{
$this->adaptor = $adaptor;
$this->querySuggestionAdaptor = $querySuggestionAdaptor;
}

public function setSpellingSuggestionAdaptor(?SpellingSuggestionAdaptor $spellingSuggestionAdaptor): void
{
$this->spellingSuggestionAdaptor = $spellingSuggestionAdaptor;
}

public function setProcessAnalyticsAdaptor(?ProcessAnalyticsAdaptor $processAnalyticsAdaptor): void
{
$this->processAnalyticsAdaptor = $processAnalyticsAdaptor;
}

public function search(Query $query, string $indexName): Results
{
return $this->adaptor->search($query, $indexName);
return $this->searchAdaptor->process($query, $indexName);
}

public function querySuggestion(Suggestion $suggestion, string $indexName): Suggestions
{
return $this->adaptor->querySuggestion($suggestion, $indexName);
return $this->querySuggestionAdaptor->process($suggestion, $indexName);
}

public function spellingSuggestion(Suggestion $suggestion, string $indexName): Suggestions
{
return $this->spellingSuggestionAdaptor->process($suggestion, $indexName);
}

public function processAnalytics(AnalyticsData $analyticsData): void
{
$this->adaptor->processAnalytics($analyticsData);
$this->processAnalyticsAdaptor->process($analyticsData);
}

}
20 changes: 0 additions & 20 deletions src/Service/SearchServiceAdaptor.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<ul>
<% loop $Me %>
<% if $Up.TargetQueryUrl && $Up.TargetQueryStringField %>
<li><a href="{$Up.TargetQueryUrl}?{$Up.TargetQueryStringField}={$Me}">$Me</a></li>
<li><a href="{$Up.TargetQueryUrl}?{$Up.TargetQueryStringField}={$Me.Raw}">$Me</a></li>
<% else %>
<li>$Me</li>
<% end_if %>
Expand Down

0 comments on commit 1ebe7fc

Please sign in to comment.