Skip to content

Commit

Permalink
NEW Add support for custom index prefixes via ALGOLIA_PREFIX_INDEX_NAME
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Jul 12, 2021
1 parent 5f9f836 commit 9e0c1de
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand All @@ -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
Expand Down
11 changes: 5 additions & 6 deletions src/Service/AlgoliaQuerier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -38,15 +38,15 @@ function array_key_first(array $arr)

$selectedIndex = array_key_first($service->indexes);
}

try {
$selectedIndex = $service->environmentizeIndex($selectedIndex);
$index = $service->getClient()->initIndex($selectedIndex);
$results = $index->search($query, $searchParameters);
} catch (Exception $e) {
Injector::inst()->get(LoggerInterface::class)->error($e);
}

$records = ArrayList::create();

if ($results && isset($results['hits'])) {
Expand All @@ -55,8 +55,7 @@ function array_key_first(array $arr)
$id = isset($hit['objectSilverstripeID']) ? $hit['objectSilverstripeID'] : null;

if (!$id || !$className) {
// ignore.
return;
continue;
}

try {
Expand All @@ -66,7 +65,7 @@ function array_key_first(array $arr)
$records->push($record);
}
} catch (Exception $e) {
//
Injector::inst()->get(LoggerInterface::class)->notice($e);
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion src/Service/AlgoliaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}


Expand Down

0 comments on commit 9e0c1de

Please sign in to comment.