From 1667e95e5ac0cb664ccd78f4e2c766c20ef82b38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20S=C4=85gol?= Date: Mon, 18 Dec 2023 10:41:37 +0100 Subject: [PATCH] #39 Avoid adding the same snippet version into index multiple times 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. --- src/Repository/ElasticRepository.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Repository/ElasticRepository.php b/src/Repository/ElasticRepository.php index c414cbf..6814a0b 100644 --- a/src/Repository/ElasticRepository.php +++ b/src/Repository/ElasticRepository.php @@ -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 = <<setParam('manual_version', $snippet['manual_version']); $snippet['manual_version'] = [$snippet['manual_version']]; $script->setUpsert($snippet); @@ -126,7 +131,11 @@ protected function getDeleteQueryScript(): string { $script = <<=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";