Skip to content

Commit

Permalink
Remove unnecessary loop in arElasticSearchPlugin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
anvit committed Nov 22, 2024
1 parent 9859544 commit ef14555
Showing 1 changed file with 43 additions and 47 deletions.
90 changes: 43 additions & 47 deletions plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,36 +547,15 @@ protected function initialize()
foreach ($this->mappings as $indexName => $indexProperties) {
$indexName = 'Qubit'.sfInflector::camelize($indexName);
$prefixedIndexName = $this->config['index']['name'].'_'.strtolower($indexName);
$this->index->addIndex($indexName,
$this->client->getIndex($prefixedIndexName)
);
}
$index = $this->client->getIndex($prefixedIndexName);
$this->index->addIndex($indexName, $index);

foreach ($this->index->getIndices() as $indexType => $index) {
try {
$index->open();
} catch (Exception $e) {
// If the index has not been initialized, create it
if ($e instanceof \Elastica\Exception\ResponseException) {
// Based on the markdown_enabled setting, add a new filter to strip Markdown tags
if (
sfConfig::get('app_markdown_enabled', true)
&& isset($this->config['index']['configuration']['analysis']['char_filter']['strip_md'])
) {
foreach ($this->config['index']['configuration']['analysis']['analyzer'] as $key => $analyzer) {
$filters = ['strip_md'];

if ($this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']) {
$filters = array_merge($filters, $this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']);
}

if (sfConfig::get('app_diacritics')) {
$filters = array_merge($filters, ['diacritics_lowercase']);
}

$this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter'] = $filters;
}
}
$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
Expand All @@ -588,36 +567,53 @@ protected function initialize()
);
}

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

if ($indexType != $indexName) {
continue;
}
// 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']);

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

// 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']);
$this->log(sprintf('Defining mapping for index %s...', $prefixedIndexName));

// Parse other parameters
unset($this->mapping[$indexName]->indexProperties['properties']);
foreach ($this->mapping[$indexName]->indexProperties as $key => $value) {
$mapping->setParam($key, $value);
}
// 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();
}
}
}

$this->log(sprintf('Defining mapping %s...', $indexName));
/**
* Set filter configuration params based on markdown settings.
*/
private function configureFilters()
{
// Based on markdown_enabled setting, add a new filter to strip Markdown tags
if (
sfConfig::get('app_markdown_enabled', true)
&& isset($this->config['index']['configuration']['analysis']['char_filter']['strip_md'])
) {
foreach ($this->config['index']['configuration']['analysis']['analyzer'] as $key => $analyzer) {
$filters = ['strip_md'];

if ($this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']) {
$filters = array_merge($filters, $this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter']);
}

// 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();
if (sfConfig::get('app_diacritics')) {
$filters = array_merge($filters, ['diacritics_lowercase']);
}

$this->config['index']['configuration']['analysis']['analyzer'][$key]['char_filter'] = $filters;
}
}
}
Expand Down

0 comments on commit ef14555

Please sign in to comment.