Skip to content

Commit

Permalink
Tweak flush/refresh operation
Browse files Browse the repository at this point in the history
  • Loading branch information
jraddaoui committed Dec 4, 2024
1 parent 5c04228 commit a66101a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 25 additions & 35 deletions plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public function __destruct()
}

$this->flushBatch();
$this->index->refresh();
}

public static function loadMappings()
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}

Expand All @@ -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]);
Expand Down Expand Up @@ -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;
}

Expand All @@ -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 {
Expand Down

0 comments on commit a66101a

Please sign in to comment.