Skip to content

Commit ef14555

Browse files
committed
Remove unnecessary loop in arElasticSearchPlugin
Remove unnecessary foreach loop in arElasticSearchPlugin. Also move code for adding filter for stripping md tags into a seperate method to make the initialization code more readable.
1 parent 9859544 commit ef14555

File tree

1 file changed

+43
-47
lines changed

1 file changed

+43
-47
lines changed

plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -547,36 +547,15 @@ protected function initialize()
547547
foreach ($this->mappings as $indexName => $indexProperties) {
548548
$indexName = 'Qubit'.sfInflector::camelize($indexName);
549549
$prefixedIndexName = $this->config['index']['name'].'_'.strtolower($indexName);
550-
$this->index->addIndex($indexName,
551-
$this->client->getIndex($prefixedIndexName)
552-
);
553-
}
550+
$index = $this->client->getIndex($prefixedIndexName);
551+
$this->index->addIndex($indexName, $index);
554552

555-
foreach ($this->index->getIndices() as $indexType => $index) {
556553
try {
557554
$index->open();
558555
} catch (Exception $e) {
559556
// If the index has not been initialized, create it
560557
if ($e instanceof \Elastica\Exception\ResponseException) {
561-
// Based on the markdown_enabled setting, add a new filter to strip Markdown tags
562-
if (
563-
sfConfig::get('app_markdown_enabled', true)
564-
&& isset($this->config['index']['configuration']['analysis']['char_filter']['strip_md'])
565-
) {
566-
foreach ($this->config['index']['configuration']['analysis']['analyzer'] as $key => $analyzer) {
567-
$filters = ['strip_md'];
568-
569-
if ($this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']) {
570-
$filters = array_merge($filters, $this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']);
571-
}
572-
573-
if (sfConfig::get('app_diacritics')) {
574-
$filters = array_merge($filters, ['diacritics_lowercase']);
575-
}
576-
577-
$this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter'] = $filters;
578-
}
579-
}
558+
$this->configureFilters();
580559

581560
// In ES 7.x if the mapping type is updated to a dummy type,
582561
// this may need to include a param for include_type_name
@@ -588,36 +567,53 @@ protected function initialize()
588567
);
589568
}
590569

591-
// Iterate over types (actor, informationobject, ...)
592-
foreach ($this->mappings as $indexName => $indexProperties) {
593-
$indexName = 'Qubit'.sfInflector::camelize($indexName);
570+
// Define mapping in elasticsearch
571+
$mapping = new \Elastica\Type\Mapping();
594572

595-
if ($indexType != $indexName) {
596-
continue;
597-
}
573+
// Setting a dummy type since it is required in ES 6.x
574+
// but it can be removed in 7.x when it becomes optional
575+
$mapping->setType($index->getType(self::ES_TYPE));
576+
$mapping->setProperties($indexProperties['properties']);
598577

599-
// Define mapping in elasticsearch
600-
$mapping = new \Elastica\Type\Mapping();
578+
// Parse other parameters
579+
unset($this->mapping[$indexName]->indexProperties['properties']);
580+
foreach ($this->mapping[$indexName]->indexProperties as $key => $value) {
581+
$mapping->setParam($key, $value);
582+
}
601583

602-
// Setting a dummy type since it is required in ES 6.x
603-
// but it can be removed in 7.x when it becomes optional
604-
$mapping->setType($index->getType(self::ES_TYPE));
605-
$mapping->setProperties($indexProperties['properties']);
584+
$this->log(sprintf('Defining mapping for index %s...', $prefixedIndexName));
606585

607-
// Parse other parameters
608-
unset($this->mapping[$indexName]->indexProperties['properties']);
609-
foreach ($this->mapping[$indexName]->indexProperties as $key => $value) {
610-
$mapping->setParam($key, $value);
611-
}
586+
// In ES 7.x this should be changed to:
587+
// $mapping->send($index, [ 'include_type_name' => false ])
588+
// which can be removed in 8.x since that is the default behaviour
589+
// and will have be removed by 9.x when it is discontinued
590+
$mapping->send();
591+
}
592+
}
593+
}
612594

613-
$this->log(sprintf('Defining mapping %s...', $indexName));
595+
/**
596+
* Set filter configuration params based on markdown settings.
597+
*/
598+
private function configureFilters()
599+
{
600+
// Based on markdown_enabled setting, add a new filter to strip Markdown tags
601+
if (
602+
sfConfig::get('app_markdown_enabled', true)
603+
&& isset($this->config['index']['configuration']['analysis']['char_filter']['strip_md'])
604+
) {
605+
foreach ($this->config['index']['configuration']['analysis']['analyzer'] as $key => $analyzer) {
606+
$filters = ['strip_md'];
607+
608+
if ($this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']) {
609+
$filters = array_merge($filters, $this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']);
610+
}
614611

615-
// In ES 7.x this should be changed to:
616-
// $mapping->send($index, [ 'include_type_name' => false ])
617-
// which can be removed in 8.x since that is the default behaviour
618-
// and will have be removed by 9.x when it is discontinued
619-
$mapping->send();
612+
if (sfConfig::get('app_diacritics')) {
613+
$filters = array_merge($filters, ['diacritics_lowercase']);
620614
}
615+
616+
$this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter'] = $filters;
621617
}
622618
}
623619
}

0 commit comments

Comments
 (0)