From a66101a2cbacfb229e92ed471693a3d86184ae9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Raddaoui=20Mar=C3=ADn?= Date: Wed, 4 Dec 2024 02:19:38 +0100 Subject: [PATCH] Tweak flush/refresh operation --- ...arElasticSearchMultiIndexWrapper.class.php | 11 +--- .../lib/arElasticSearchPlugin.class.php | 60 ++++++++----------- 2 files changed, 27 insertions(+), 44 deletions(-) diff --git a/plugins/arElasticSearchPlugin/lib/arElasticSearchMultiIndexWrapper.class.php b/plugins/arElasticSearchPlugin/lib/arElasticSearchMultiIndexWrapper.class.php index a8937a6ce3..61f3b5ecdb 100644 --- a/plugins/arElasticSearchPlugin/lib/arElasticSearchMultiIndexWrapper.class.php +++ b/plugins/arElasticSearchPlugin/lib/arElasticSearchMultiIndexWrapper.class.php @@ -84,16 +84,9 @@ public function deleteById($name, $documentId) * * @param string $name Index name to be refreshed (optional) */ - public function refresh($name = null) + public function refresh($name) { - if ($name && $this->indices[$name]) { - $this->indices[$name]->refresh(); - - return; - } - foreach ($this->indices as $index) { - $index->refresh(); - } + $this->indices[$name]->refresh(); } // Return the index element from the array of indices diff --git a/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php b/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php index 998468d76b..c24f817088 100644 --- a/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php +++ b/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php @@ -125,7 +125,6 @@ public function __destruct() } $this->flushBatch(); - $this->index->refresh(); } public static function loadMappings() @@ -185,35 +184,39 @@ public function optimize($args = []) */ public function flushBatch() { - if ($this->batchMode) { - // Batch add documents, if any - if (count($this->batchAddDocs) > 0) { - try { - $this->index->addDocuments($this->currentBatchIndexName, $this->batchAddDocs); - } catch (Exception $e) { - // Clear batchAddDocs if something went wrong too - $this->batchAddDocs = []; - - throw $e; - } + if (!$this->batchMode) { + return; + } + // Batch add documents, if any + if (count($this->batchAddDocs) > 0) { + try { + $this->index->addDocuments($this->currentBatchIndexName, $this->batchAddDocs); + } catch (Exception $e) { + // Clear batchAddDocs if something went wrong too $this->batchAddDocs = []; - } - // Batch delete documents, if any - if (count($this->batchDeleteDocs) > 0) { - try { - $this->index->deleteDocuments($this->currentBatchIndexName, $this->batchDeleteDocs); - } catch (Exception $e) { - // Clear batchDeleteDocs if something went wrong too - $this->batchDeleteDocs = []; + throw $e; + } - throw $e; - } + $this->batchAddDocs = []; + } + // Batch delete documents, if any + if (count($this->batchDeleteDocs) > 0) { + try { + $this->index->deleteDocuments($this->currentBatchIndexName, $this->batchDeleteDocs); + } catch (Exception $e) { + // Clear batchDeleteDocs if something went wrong too $this->batchDeleteDocs = []; + + throw $e; } + + $this->batchDeleteDocs = []; } + + $this->index->refresh($this->currentBatchIndexName); } /** @@ -361,9 +364,6 @@ public function addDocument($data, $indexName) if ($this->batchMode) { if ($this->currentBatchIndexName != $indexName) { $this->flushBatch(); - - // Refresh only previous index - $this->index->refresh($this->currentBatchIndexName); $this->currentBatchIndexName = $indexName; } @@ -373,9 +373,6 @@ public function addDocument($data, $indexName) // If we have a full batch, send additions and deletions in bulk if (count($this->batchAddDocs) >= $this->batchSize) { $this->flushBatch(); - - // Refresh current index - $this->index->refresh($this->currentBatchIndexName); } } else { $this->index->addDocuments($indexName, [$document]); @@ -470,10 +467,6 @@ public function delete($object) if ($this->currentBatchIndexName != $indexName) { $this->flushBatch(); - - // Refresh only previous index - $this->index->refresh($this->currentBatchIndexName); - $this->currentBatchIndexName = $indexName; } @@ -482,9 +475,6 @@ public function delete($object) // If we have a full batch, send additions and deletions in bulk if (count($this->batchDeleteDocs) >= $this->batchSize) { $this->flushBatch(); - - // Refresh current index - $this->index->refresh($this->currentBatchIndexName); } } else { try {