Skip to content

Commit

Permalink
Add environmentizeIndex() to IndexingInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
chrispenny committed Mar 5, 2023
1 parent 9febb90 commit 51e9c10
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
12 changes: 6 additions & 6 deletions docs/en/customising_add_search_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function addDocument(DocumentInterface $document): ?string
foreach (array_keys($indexes) as $indexName) {
// your custom API call here
$mySearchClient->addDocuementToIndex(
static::environmentizeIndex($indexName),
$this->environmentizeIndex($indexName),
$fields
);
}
Expand Down Expand Up @@ -96,7 +96,7 @@ public function removeDocument(DocumentInterface $document): ?string
foreach (array_keys($indexes) as $indexName) {
// your custom API call here
$myAPI->removeDocumentFromIndex(
static::environmentizeIndex($indexName),
$this->environmentizeIndex($indexName),
$document->getIdentifier()
);
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public function getDocument(string $id): ?array
foreach (array_keys(IndexConfiguration::singleton()->getIndexes()) as $indexName) {
// Your API call here
$result = $myAPI->retrieveDocumentFromIndex(
static::environmentizeIndex($indexName),
$this->environmentizeIndex($indexName),
$id
);
Expand Down Expand Up @@ -169,7 +169,7 @@ return type should be an array of `DocumentInterface`.
public function listDocuments(string $indexName, ?int $pageSize = null, int $currentPage = 0): array
{
// Your API call here
$request = new ListDocuments(static::environmentizeIndex($indexName));
$request = new ListDocuments($this->environmentizeIndex($indexName));
$request->setPageSize($pageSize);
$request->setCurrentPage($currentPage);
Expand All @@ -193,7 +193,7 @@ public function getDocumentTotal(string $indexName): int
{
// Your API call here
$response = $myAPI->listDocuments(
static::environmentizeIndex($indexName)
$this->environmentizeIndex($indexName)
);
return $response['metadata']['total'];
Expand All @@ -217,7 +217,7 @@ Return value should be an array describing the current Schema for each index.
public function configure(): array
{
foreach ($indexesToCreate as $index) {
$myAPI->createIndex(static::environmentizeIndex($index));
$myAPI->createIndex($this->environmentizeIndex($index));
}
}
```
Expand Down
6 changes: 6 additions & 0 deletions src/Admin/IndexedDocumentsResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
class IndexedDocumentsResult extends ViewableData
{

public string $IndexName;

public int $DBDocs;

public int $RemoteDocs;

public function summaryFields(): array
{
return [
Expand Down
3 changes: 1 addition & 2 deletions src/Admin/SearchAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
use SilverStripe\SearchService\Jobs\ReindexJob;
use SilverStripe\SearchService\Jobs\RemoveDataObjectJob;
use SilverStripe\SearchService\Tasks\SearchReindex;
use SilverStripe\SearchServiceElastic\Service\EnterpriseSearchService;
use SilverStripe\Security\Permission;
use SilverStripe\Security\PermissionProvider;
use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor;
Expand Down Expand Up @@ -208,7 +207,7 @@ private function buildIndexedDocumentsList(): ArrayList
}

$result = new IndexedDocumentsResult();
$result->IndexName = EnterpriseSearchService::environmentizeIndex($index);
$result->IndexName = $indexer->environmentizeIndex($index);
$result->DBDocs = $localCount;
$result->RemoteDocs = $indexer->getDocumentTotal($index);
$list->push($result);
Expand Down
5 changes: 5 additions & 0 deletions src/Interfaces/IndexingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
interface IndexingInterface extends BatchDocumentInterface
{

/**
* Often used for resu
*/
public function environmentizeIndex(string $indexName): string;

/**
* @return string|null ID of the Document added
* @throws IndexingServiceException
Expand Down
12 changes: 12 additions & 0 deletions src/Service/Naive/NaiveSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@
use SilverStripe\SearchService\Interfaces\BatchDocumentRemovalInterface;
use SilverStripe\SearchService\Interfaces\DocumentInterface;
use SilverStripe\SearchService\Interfaces\IndexingInterface;
use SilverStripe\SearchService\Service\IndexConfiguration;

class NaiveSearchService implements IndexingInterface, BatchDocumentRemovalInterface
{

public function environmentizeIndex(string $indexName): string
{
$variant = IndexConfiguration::singleton()->getIndexVariant();

if ($variant) {
return sprintf('%s-%s', $variant, $indexName);
}

return $indexName;
}

public function addDocument(DocumentInterface $document): ?string
{
return null;
Expand Down

0 comments on commit 51e9c10

Please sign in to comment.