Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support replica index configuration #275

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,33 @@ The IndexSettings object provides autocompletion for all Algolia's settings
)
```

### Replicas

Replicas can be created with the `replicas` function on `IndexSettings`. To configure replicas, include them in the `indices` array and set their `replicaIndex` to `true` so that they are not included in any syncing operations.

Replica indices can have their configuration updated using the `./craft scout/settings/update` console command.

```php
<?php

return [
'indices' => [
\rias\scout\ScoutIndex::create('Products')
// ...
->indexSettings(
\rias\scout\IndexSettings::create()
->minWordSizefor1Typo(4)
->replicas(['virtual(Products_desc)'])
)
],
[
\rias\scout\ScoutIndex::create('Products_desc')
->replicaIndex(true)
->indexSettings(IndexSettings::create()->customRanking(['desc(price)'])),
],
];
```

## Twig variables
You can access the Algolia settings set in your config file through the following Twig variables.

Expand Down
14 changes: 14 additions & 0 deletions src/ScoutIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class ScoutIndex extends BaseObject
/** @var array */
public $splitElementsOn = [];

/** @var bool */
public $replicaIndex = false;

/** @var callable|ElementQuery */
private $_criteria;

Expand Down Expand Up @@ -134,4 +137,15 @@ public function indexSettings(IndexSettings $indexSettings): self

return $this;
}

/**
* @param bool $replicaIndex Whether to mark this index as a replica index and skip syncing.
* @return $this
*/
public function replicaIndex(bool $replicaIndex): self
{
$this->replicaIndex = $replicaIndex;

return $this;
}
}
4 changes: 2 additions & 2 deletions src/console/controllers/scout/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function actionFlush($index = '')

$engines = Scout::$plugin->getSettings()->getEngines();
$engines->filter(function (Engine $engine) use ($index) {
return $index === '' || $engine->scoutIndex->indexName === $index;
return !$engine->scoutIndex->replicaIndex && ($index === '' || $engine->scoutIndex->indexName === $index);
})->each(function (Engine $engine) {
$engine->flush();
$this->stdout("Flushed index {$engine->scoutIndex->indexName}\n", Console::FG_GREEN);
Expand All @@ -50,7 +50,7 @@ public function actionImport($index = '')
$engines = Scout::$plugin->getSettings()->getEngines();

$engines->filter(function (Engine $engine) use ($index) {
return $index === '' || $engine->scoutIndex->indexName === $index;
return !$engine->scoutIndex->replicaIndex && ($index === '' || $engine->scoutIndex->indexName === $index);
})->each(function (Engine $engine) {
if ($this->queue) {
Craft::$app->getQueue()
Expand Down
12 changes: 12 additions & 0 deletions src/engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public function __construct(ScoutIndex $scoutIndex, Algolia $algolia)
*/
public function update($elements)
{
if ($this->scoutIndex->replicaIndex) {
return;
}

$elements = new Collection(Arr::wrap($elements));

$elements = $elements->filter(function (Element $element) {
Expand All @@ -51,6 +55,10 @@ public function update($elements)

public function delete($elements)
{
if ($this->scoutIndex->replicaIndex) {
return;
}

$elements = new Collection(Arr::wrap($elements));

$index = $this->algolia->initIndex($this->scoutIndex->indexName);
Expand Down Expand Up @@ -78,6 +86,10 @@ public function delete($elements)

public function flush()
{
if ($this->scoutIndex->replicaIndex) {
return;
}

$index = $this->algolia->initIndex($this->scoutIndex->indexName);
$index->clearObjects();
}
Expand Down
82 changes: 45 additions & 37 deletions src/templates/utility.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,62 @@
{% for index in stats %}
<h2 style="width: 100%;">
{{ index.name }}
{% if index.site == "all" %}
<span class="light">&ndash; {{ "All sites"|t('scout') }}</span>&nbsp;
{% if index.replicaIndex == false %}
{% if index.site == "all" %}
<span class="light">&ndash; {{ "All sites"|t('scout') }}</span>&nbsp;
{% else %}
<span class="light">&ndash; {{ index.site.name }}</span>&nbsp;
{% endif %}
<span class="light">&ndash; </span><small class="light">{{ index.elementType }}</small>
{% else %}
<span class="light">&ndash; {{ index.site.name }}</span>&nbsp;
<span class="light">&ndash; {{ "Replica index"|t('scout') }}</span>&nbsp;
{% endif %}
<span class="light">&ndash; </span><small class="light">{{ index.elementType }}</small>
</h2>
<table class="data fullwidth fixed-layout">
<tbody>
<tr>
<th class="light">{{ "Elements in Craft"|t("scout") }}</th>
<td>{{ index.elements }}</td>
</tr>
<tr>
<th class="light">{{ "Records in Index"|t("scout") }}</th>
<td>{{ index.indexed }}</td>
</tr>
{% if index.replicaIndex == false %}
<tr>
<th class="light">{{ "Elements in Craft"|t("scout") }}</th>
<td>{{ index.elements }}</td>
</tr>
<tr>
<th class="light">{{ "Records in Index"|t("scout") }}</th>
<td>{{ index.indexed }}</td>
</tr>
{% endif %}
<tr>
<th class="light">
{{ "Actions"|t("scout") }}<br>
<small style="font-weight: normal;">{{ "These can take a while to process."|t("scout") }}</small>
</th>
<td>
<div style="display: flex; justify-content: left; flex-wrap: wrap;">
{% include 'scout/_partials/action' with {
action: {
name: 'Refresh' | t('scout'),
slug: 'refresh',
index: index.name
}
} %}
&nbsp;
{% include 'scout/_partials/action' with {
action: {
name: 'Import' | t('scout'),
slug: 'import',
index: index.name
}
} %}
&nbsp;
{% include 'scout/_partials/action' with {
action: {
name: 'Flush' | t('scout'),
slug: 'flush',
index: index.name
}
} %}
</div>
{% if index.replicaIndex == false %}
<div style="display: flex; justify-content: left; flex-wrap: wrap;">
{% include 'scout/_partials/action' with {
action: {
name: 'Refresh' | t('scout'),
slug: 'refresh',
index: index.name
}
} %}
&nbsp;
{% include 'scout/_partials/action' with {
action: {
name: 'Import' | t('scout'),
slug: 'import',
index: index.name
}
} %}
&nbsp;
{% include 'scout/_partials/action' with {
action: {
name: 'Flush' | t('scout'),
slug: 'flush',
index: index.name
}
} %}
</div>
{% endif %}
<div style="display: flex; justify-content: left; flex-wrap: wrap;">
{% if index.hasSettings %}
{% include 'scout/_partials/action' with {
Expand Down
20 changes: 13 additions & 7 deletions src/utilities/ScoutUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ public static function contentHtml(): string
$engines = Scout::$plugin->getSettings()->getEngines();

$stats = $engines->map(function (Engine $engine) {
return [
'name' => $engine->scoutIndex->indexName,
'elementType' => $engine->scoutIndex->elementType,
'site' => $engine->scoutIndex->criteria->siteId === '*' ? 'all' : Craft::$app->getSites()->getSiteById($engine->scoutIndex->criteria->siteId),
'indexed' => $engine->getTotalRecords(),
'elements' => $engine->scoutIndex->criteria->count(),
'hasSettings' => $engine->scoutIndex->indexSettings ?? null,
$stats = [
'name' => $engine->scoutIndex->indexName,
'replicaIndex' => $engine->scoutIndex->replicaIndex,
'hasSettings' => $engine->scoutIndex->indexSettings ?? null,
];
if (!$engine->scoutIndex->replicaIndex) {
$stats = array_merge($stats, [
'elementType' => $engine->scoutIndex->elementType,
'site' => $engine->scoutIndex->criteria->siteId === '*' ? 'all' : Craft::$app->getSites()->getSiteById($engine->scoutIndex->criteria->siteId),
'indexed' => $engine->getTotalRecords(),
'elements' => $engine->scoutIndex->criteria->count(),
]);
}
return $stats;
});

return $view->renderTemplate('scout/utility', [
Expand Down