Skip to content

Commit 1525e40

Browse files
committed
Tweak index recreation
1 parent bf282c8 commit 1525e40

File tree

1 file changed

+65
-68
lines changed

1 file changed

+65
-68
lines changed

plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php

Lines changed: 65 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -216,52 +216,6 @@ public function flushBatch()
216216
}
217217
}
218218

219-
private function recreateIndex($indexName, $indexProperties) {
220-
$index = $this->index->getIndex($indexName);
221-
$prefixedIndexName = $this->config['index']['name'].'_'.strtolower($indexName);
222-
try {
223-
$index->open();
224-
$index->delete();
225-
} catch (Exception $e) {
226-
// If the index has not been initialized, create it
227-
if ($e instanceof \Elastica\Exception\ResponseException) {
228-
$this->configureFilters();
229-
230-
// In ES 7.x if the mapping type is updated to a dummy type,
231-
// this may need to include a param for include_type_name
232-
// set to false in order to avoid automatically creating a
233-
// type for the index that was just created
234-
$index->create(
235-
$this->config['index']['configuration'],
236-
['recreate' => true]
237-
);
238-
}
239-
240-
// Define mapping in elasticsearch
241-
$mapping = new \Elastica\Type\Mapping();
242-
243-
// Setting a dummy type since it is required in ES 6.x
244-
// but it can be removed in 7.x when it becomes optional
245-
$index = $this->index->getIndex($indexName);
246-
$mapping->setType($index->getType(self::ES_TYPE));
247-
$mapping->setProperties($indexProperties['properties']);
248-
249-
// Parse other parameters
250-
unset($indexProperties['properties']);
251-
foreach ($indexProperties as $key => $value) {
252-
$mapping->setParam($key, $value);
253-
}
254-
255-
$this->log(sprintf('Defining mapping for index %s...', $prefixedIndexName));
256-
257-
// In ES 7.x this should be changed to:
258-
// $mapping->send($index, [ 'include_type_name' => false ])
259-
// which can be removed in 8.x since that is the default behaviour
260-
// and will have be removed by 9.x when it is discontinued
261-
$mapping->send();
262-
}
263-
}
264-
265219
/**
266220
* Populate index.
267221
*
@@ -272,9 +226,6 @@ public function populate($options = [])
272226
$excludeTypes = (!empty($options['excludeTypes'])) ? $options['excludeTypes'] : [];
273227
$update = (!empty($options['update'])) ? $options['update'] : false;
274228

275-
// Initialize index if necessary
276-
//$this->initialize();
277-
278229
if (sfConfig::get('app_diacritics')) {
279230
$this->config['index']['configuration']['analysis']['char_filter']['diacritics_lowercase'] = $this->loadDiacriticsMappings();
280231
}
@@ -313,28 +264,29 @@ public function populate($options = [])
313264
$showErrors = false;
314265

315266
foreach ($this->mappings as $indexName => $indexProperties) {
316-
if (!in_array(strtolower($indexName), $excludeTypes)) {
317-
$camelizedTypeName = sfInflector::camelize($indexName);
318-
$className = 'arElasticSearch'.$camelizedTypeName;
319-
$indexName = 'Qubit'.$camelizedTypeName;
320-
321-
// If excluding types then index as a whole hasn't been flushed: delete
322-
// type's documents if not updating
323-
if (!$update) {
324-
$this->recreateIndex($indexName, $indexProperties);
325-
}
267+
if (in_array(strtolower($indexName), $excludeTypes)) {
268+
continue;
269+
}
326270

327-
$class = new $className();
328-
$class->setTimer($timer);
271+
$camelizedTypeName = sfInflector::camelize($indexName);
272+
$className = 'arElasticSearch'.$camelizedTypeName;
273+
$indexName = 'Qubit'.$camelizedTypeName;
329274

330-
$typeErrors = $class->populate();
331-
if (count($typeErrors) > 0) {
332-
$showErrors = true;
333-
$errors = array_merge($errors, $typeErrors);
334-
}
275+
// If not updating, recreate index.
276+
if (!$update) {
277+
$this->recreateIndex($indexName, $indexProperties);
278+
}
335279

336-
$total += $class->getCount();
280+
$class = new $className();
281+
$class->setTimer($timer);
282+
283+
$typeErrors = $class->populate();
284+
if (count($typeErrors) > 0) {
285+
$showErrors = true;
286+
$errors = array_merge($errors, $typeErrors);
337287
}
288+
289+
$total += $class->getCount();
338290
}
339291

340292
$this->log(
@@ -580,7 +532,6 @@ protected function initialize()
580532
{
581533
// Iterate over types (actor, informationobject, ...)
582534
$indices = ['aip', 'term', 'actor', 'accession', 'repository', 'functionObject', 'informationObject'];
583-
//$this->loadAndNormalizeMappings();
584535
foreach ($indices as $indexName) {
585536
$this->log(sprintf('index names %s...', $indexName));
586537
$indexName = 'Qubit'.sfInflector::camelize($indexName);
@@ -590,6 +541,52 @@ protected function initialize()
590541
}
591542
}
592543

544+
private function recreateIndex($indexName, $indexProperties)
545+
{
546+
$index = $this->index->getIndex($indexName);
547+
$prefixedIndexName = $this->config['index']['name'].'_'.strtolower($indexName);
548+
549+
try {
550+
$index->delete();
551+
} catch (\Elastica\Exception\ResponseException $e) {
552+
// TODO?: make sure it's a non exist error.
553+
}
554+
555+
// If the index has not been initialized, create it
556+
$this->configureFilters();
557+
558+
// In ES 7.x if the mapping type is updated to a dummy type,
559+
// this may need to include a param for include_type_name
560+
// set to false in order to avoid automatically creating a
561+
// type for the index that was just created
562+
$index->create(
563+
$this->config['index']['configuration'],
564+
['recreate' => true]
565+
);
566+
567+
// Define mapping in elasticsearch
568+
$mapping = new \Elastica\Type\Mapping();
569+
570+
// Setting a dummy type since it is required in ES 6.x
571+
// but it can be removed in 7.x when it becomes optional
572+
$mapping->setType($index->getType(self::ES_TYPE));
573+
$mapping->setProperties($indexProperties['properties']);
574+
575+
// Parse other parameters
576+
unset($indexProperties['properties']);
577+
foreach ($indexProperties as $key => $value) {
578+
$mapping->setParam($key, $value);
579+
}
580+
581+
$this->log(sprintf('Defining mapping for index %s...', $prefixedIndexName));
582+
583+
// In ES 7.x this should be changed to:
584+
// $mapping->send($index, [ 'include_type_name' => false ])
585+
// which can be removed in 8.x since that is the default behaviour
586+
// and will have be removed by 9.x when it is discontinued
587+
$mapping->send();
588+
}
589+
593590
/**
594591
* Set filter configuration params based on markdown settings.
595592
*/

0 commit comments

Comments
 (0)