Skip to content

Commit

Permalink
#39 Avoid adding the same snippet version into index multiple times
Browse files Browse the repository at this point in the history
This commit introduces a check during the re/indexing of manual
snippets. It verifies whether the processed version is already assigned
to a snippet within the index. If it is, the system will not add it again.
  • Loading branch information
Marcin Sągol committed Dec 18, 2023
1 parent 91b5951 commit 1667e95
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Repository/ElasticRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ public function addOrUpdateDocument(array $snippet): void
$urlFragment = str_replace('/', '-', $snippet['manual_title'] . '-' . $snippet['relative_url'] . '-' . $snippet['content_hash']);
$documentId = $urlFragment . '-' . $snippet['fragment'];

$script = new Script('ctx._source.manual_version.add(params.manual_version)');
$scriptCode = <<<EOD
if (!ctx._source.manual_version.contains(params.manual_version)) {
ctx._source.manual_version.add(params.manual_version);
}
EOD;
$script = new Script($scriptCode);
$script->setParam('manual_version', $snippet['manual_version']);
$snippet['manual_version'] = [$snippet['manual_version']];
$script->setUpsert($snippet);
Expand Down Expand Up @@ -126,7 +131,11 @@ protected function getDeleteQueryScript(): string
{
$script = <<<EOD
if (ctx._source.manual_version.contains(params.manual_version)) {
ctx._source.manual_version.remove(ctx._source.manual_version.indexOf(params.manual_version));
for (int i=ctx._source.manual_version.length-1; i>=0; i--) {
if (ctx._source.manual_version[i] == params.manual_version) {
ctx._source.manual_version.remove(i);
}
}
}
if (ctx._source.manual_version.size() == 0) {
ctx.op = "delete";
Expand Down

0 comments on commit 1667e95

Please sign in to comment.