Skip to content

Commit

Permalink
Add ability to exclude replica indexes from getIndexes()
Browse files Browse the repository at this point in the history
  • Loading branch information
wilr committed Oct 12, 2021
1 parent dc97808 commit 398bfe3
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/Service/AlgoliaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,46 @@ public function getClient()
return $this->client;
}


public function getIndexes($excludeReplicas = true)
{
if (!$excludeReplicas) {
return $this->indexes;
}

$replicas = [];
$output = [];

foreach ($this->indexes as $indexName => $data) {
if (isset($data['indexSettings']) && isset($data['indexSettings']['replicas'])) {
foreach ($data['indexSettings']['replicas'] as $replicaName) {
$replicas[$replicaName] = $replicaName;
}
}
}

foreach ($this->indexes as $indexName => $data) {
if (in_array($indexName, $replicas)) {
continue;
}

$output[$indexName] = $data;
}

return $output;
}


/**
* Returns an array of all the indexes which need the given item or item
* class. If no item provided, returns a list of all the indexes defined.
*
* @param DataObject|string|null $item
* @param bool $excludeReplicas
*
* @return \Algolia\AlgoliaSearch\SearchIndex[]
*/
public function initIndexes($item = null)
public function initIndexes($item = null, $excludeReplicas = true)
{
if (!Security::database_is_ready()) {
return [];
Expand All @@ -80,11 +111,13 @@ public function initIndexes($item = null)
}

if (!$item) {
$indexes = $this->getIndexes($excludeReplicas);

return array_map(
function ($indexName) use ($client) {
return $client->initIndex($this->environmentizeIndex($indexName));
},
array_keys($this->indexes)
array_keys($indexes)
);
}

Expand All @@ -96,6 +129,8 @@ function ($indexName) use ($client) {

$matches = [];

$replicas = [];

foreach ($this->indexes as $indexName => $data) {
$classes = (isset($data['includeClasses'])) ? $data['includeClasses'] : null;

Expand All @@ -108,11 +143,21 @@ function ($indexName) use ($client) {
}
}
}

if (isset($data['indexSettings']) && isset($data['indexSettings']['replicas'])) {
foreach ($data['indexSettings']['replicas'] as $replicaName) {
$replicas[$replicaName] = $replicaName;
}
}
}

$output = [];

foreach ($matches as $index) {
if (in_array($index, array_keys($replicas)) && $excludeReplicas) {
continue;
}

$output[$index] = $client->initIndex($this->environmentizeIndex($index));
}

Expand Down

0 comments on commit 398bfe3

Please sign in to comment.