Skip to content

Commit

Permalink
Tweak index recreation
Browse files Browse the repository at this point in the history
  • Loading branch information
jraddaoui committed Dec 4, 2024
1 parent bf282c8 commit 1525e40
Showing 1 changed file with 65 additions and 68 deletions.
133 changes: 65 additions & 68 deletions plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,52 +216,6 @@ public function flushBatch()
}
}

private function recreateIndex($indexName, $indexProperties) {
$index = $this->index->getIndex($indexName);
$prefixedIndexName = $this->config['index']['name'].'_'.strtolower($indexName);
try {
$index->open();
$index->delete();
} catch (Exception $e) {
// If the index has not been initialized, create it
if ($e instanceof \Elastica\Exception\ResponseException) {
$this->configureFilters();

// In ES 7.x if the mapping type is updated to a dummy type,
// this may need to include a param for include_type_name
// set to false in order to avoid automatically creating a
// type for the index that was just created
$index->create(
$this->config['index']['configuration'],
['recreate' => true]
);
}

// Define mapping in elasticsearch
$mapping = new \Elastica\Type\Mapping();

// Setting a dummy type since it is required in ES 6.x
// but it can be removed in 7.x when it becomes optional
$index = $this->index->getIndex($indexName);
$mapping->setType($index->getType(self::ES_TYPE));
$mapping->setProperties($indexProperties['properties']);

// Parse other parameters
unset($indexProperties['properties']);
foreach ($indexProperties as $key => $value) {
$mapping->setParam($key, $value);
}

$this->log(sprintf('Defining mapping for index %s...', $prefixedIndexName));

// In ES 7.x this should be changed to:
// $mapping->send($index, [ 'include_type_name' => false ])
// which can be removed in 8.x since that is the default behaviour
// and will have be removed by 9.x when it is discontinued
$mapping->send();
}
}

/**
* Populate index.
*
Expand All @@ -272,9 +226,6 @@ public function populate($options = [])
$excludeTypes = (!empty($options['excludeTypes'])) ? $options['excludeTypes'] : [];
$update = (!empty($options['update'])) ? $options['update'] : false;

// Initialize index if necessary
//$this->initialize();

if (sfConfig::get('app_diacritics')) {
$this->config['index']['configuration']['analysis']['char_filter']['diacritics_lowercase'] = $this->loadDiacriticsMappings();
}
Expand Down Expand Up @@ -313,28 +264,29 @@ public function populate($options = [])
$showErrors = false;

foreach ($this->mappings as $indexName => $indexProperties) {
if (!in_array(strtolower($indexName), $excludeTypes)) {
$camelizedTypeName = sfInflector::camelize($indexName);
$className = 'arElasticSearch'.$camelizedTypeName;
$indexName = 'Qubit'.$camelizedTypeName;

// If excluding types then index as a whole hasn't been flushed: delete
// type's documents if not updating
if (!$update) {
$this->recreateIndex($indexName, $indexProperties);
}
if (in_array(strtolower($indexName), $excludeTypes)) {
continue;
}

$class = new $className();
$class->setTimer($timer);
$camelizedTypeName = sfInflector::camelize($indexName);
$className = 'arElasticSearch'.$camelizedTypeName;
$indexName = 'Qubit'.$camelizedTypeName;

$typeErrors = $class->populate();
if (count($typeErrors) > 0) {
$showErrors = true;
$errors = array_merge($errors, $typeErrors);
}
// If not updating, recreate index.
if (!$update) {
$this->recreateIndex($indexName, $indexProperties);
}

$total += $class->getCount();
$class = new $className();
$class->setTimer($timer);

$typeErrors = $class->populate();
if (count($typeErrors) > 0) {
$showErrors = true;
$errors = array_merge($errors, $typeErrors);
}

$total += $class->getCount();
}

$this->log(
Expand Down Expand Up @@ -580,7 +532,6 @@ protected function initialize()
{
// Iterate over types (actor, informationobject, ...)
$indices = ['aip', 'term', 'actor', 'accession', 'repository', 'functionObject', 'informationObject'];
//$this->loadAndNormalizeMappings();
foreach ($indices as $indexName) {
$this->log(sprintf('index names %s...', $indexName));
$indexName = 'Qubit'.sfInflector::camelize($indexName);
Expand All @@ -590,6 +541,52 @@ protected function initialize()
}
}

private function recreateIndex($indexName, $indexProperties)
{
$index = $this->index->getIndex($indexName);
$prefixedIndexName = $this->config['index']['name'].'_'.strtolower($indexName);

try {
$index->delete();
} catch (\Elastica\Exception\ResponseException $e) {
// TODO?: make sure it's a non exist error.
}

// If the index has not been initialized, create it
$this->configureFilters();

// In ES 7.x if the mapping type is updated to a dummy type,
// this may need to include a param for include_type_name
// set to false in order to avoid automatically creating a
// type for the index that was just created
$index->create(
$this->config['index']['configuration'],
['recreate' => true]
);

// Define mapping in elasticsearch
$mapping = new \Elastica\Type\Mapping();

// Setting a dummy type since it is required in ES 6.x
// but it can be removed in 7.x when it becomes optional
$mapping->setType($index->getType(self::ES_TYPE));
$mapping->setProperties($indexProperties['properties']);

// Parse other parameters
unset($indexProperties['properties']);
foreach ($indexProperties as $key => $value) {
$mapping->setParam($key, $value);
}

$this->log(sprintf('Defining mapping for index %s...', $prefixedIndexName));

// In ES 7.x this should be changed to:
// $mapping->send($index, [ 'include_type_name' => false ])
// which can be removed in 8.x since that is the default behaviour
// and will have be removed by 9.x when it is discontinued
$mapping->send();
}

/**
* Set filter configuration params based on markdown settings.
*/
Expand Down

0 comments on commit 1525e40

Please sign in to comment.