From 9e0c1dee1b7419e86064a33fd77cc5279f95780d Mon Sep 17 00:00:00 2001 From: Will Rossiter Date: Mon, 12 Jul 2021 12:24:39 +1200 Subject: [PATCH] NEW Add support for custom index prefixes via ALGOLIA_PREFIX_INDEX_NAME --- README.md | 20 ++++++++++++++++---- src/Service/AlgoliaQuerier.php | 11 +++++------ src/Service/AlgoliaService.php | 12 +++++++++++- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 298d4f0..3472b62 100644 --- a/README.md +++ b/README.md @@ -47,10 +47,6 @@ DataObjects. First, sign up for Algolia.com account and install this module. Once installed, Configure the API keys via YAML (environment variables recommended). -** :warning: this module will assume your indexes are setup as `dev_{IndexName}`, -`test_{IndexName}` and `live_{IndexName}` where the result of your environment -type is prefixed** - *app/_config/algolia.yml* ```yml --- @@ -76,6 +72,22 @@ Once the indexes and API keys are configured, run a `dev/build` to update the database and refresh the indexSettings. Alternatively you can run `AlgoliaConfigure` to manually rebuild the indexSettings. +### Configuring the index names + +This module will assume your indexes are setup as `dev_{IndexName}`, +`test_{IndexName}` and `live_{IndexName}` where the result of your environment +type is prefixed to the names listed in the main YAML config. + +If you explictly want to disable the environment prefix (or use a custom +approach) use the `ALGOLIA_PREFIX_INDEX_NAME` environment variable. + +``` +ALGOLIA_PREFIX_INDEX_NAME='dev_will' +``` + +Or for testing with live data on dev use `ALGOLIA_PREFIX_INDEX_NAME='live'` + + ### Defining Replica Indexes If your search form provides a sort option (e.g latest or relevance) then you diff --git a/src/Service/AlgoliaQuerier.php b/src/Service/AlgoliaQuerier.php index b9719e8..eaab6e6 100644 --- a/src/Service/AlgoliaQuerier.php +++ b/src/Service/AlgoliaQuerier.php @@ -24,7 +24,7 @@ public function fetchResults($selectedIndex = null, $query = '', $searchParamete { $service = Injector::inst()->get(AlgoliaService::class); $results = false; - + if (!$selectedIndex) { if (!function_exists('array_key_first')) { function array_key_first(array $arr) @@ -38,7 +38,7 @@ function array_key_first(array $arr) $selectedIndex = array_key_first($service->indexes); } - + try { $selectedIndex = $service->environmentizeIndex($selectedIndex); $index = $service->getClient()->initIndex($selectedIndex); @@ -46,7 +46,7 @@ function array_key_first(array $arr) } catch (Exception $e) { Injector::inst()->get(LoggerInterface::class)->error($e); } - + $records = ArrayList::create(); if ($results && isset($results['hits'])) { @@ -55,8 +55,7 @@ function array_key_first(array $arr) $id = isset($hit['objectSilverstripeID']) ? $hit['objectSilverstripeID'] : null; if (!$id || !$className) { - // ignore. - return; + continue; } try { @@ -66,7 +65,7 @@ function array_key_first(array $arr) $records->push($record); } } catch (Exception $e) { - // + Injector::inst()->get(LoggerInterface::class)->notice($e); } } } diff --git a/src/Service/AlgoliaService.php b/src/Service/AlgoliaService.php index a2a2c3e..cc5b370 100644 --- a/src/Service/AlgoliaService.php +++ b/src/Service/AlgoliaService.php @@ -6,6 +6,7 @@ use Exception; use Psr\Log\LoggerInterface; use SilverStripe\Control\Director; +use SilverStripe\Core\Environment; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; use SilverStripe\Dev\Debug; @@ -119,13 +120,22 @@ function ($indexName) use ($client) { } /** + * Prefixes the given indexName with the configured prefix, or environment + * type. + * * @param string $indexName * * @return string */ public function environmentizeIndex($indexName) { - return sprintf("%s_%s", Director::get_environment_type(), $indexName); + $prefix = Environment::getEnv('ALGOLIA_PREFIX_INDEX_NAME'); + + if ($prefix === false) { + $prefix = Director::get_environment_type(); + } + + return sprintf("%s_%s", $prefix, $indexName); }