From 9d6fa03503d3708bbf6275b500367abb8334e4fc Mon Sep 17 00:00:00 2001 From: Anvit Srivastav Date: Thu, 21 Nov 2024 15:55:19 -0800 Subject: [PATCH] Change references to index type for ElasticSearch Update any references to ElasticSearch index type to index name. Also add getType as an alias for getIndex for backwards compatibility with custom themes that still might be making references to ElasticSearch's getType. --- .../actions/autocompleteAction.class.php | 6 +- ...arElasticSearchMultiIndexWrapper.class.php | 36 ++++---- .../lib/arElasticSearchPlugin.class.php | 83 ++++++++++--------- 3 files changed, 67 insertions(+), 58 deletions(-) diff --git a/apps/qubit/modules/search/actions/autocompleteAction.class.php b/apps/qubit/modules/search/actions/autocompleteAction.class.php index 942afadfc4..fc1f4f22dd 100644 --- a/apps/qubit/modules/search/actions/autocompleteAction.class.php +++ b/apps/qubit/modules/search/actions/autocompleteAction.class.php @@ -39,7 +39,7 @@ public function execute($request) $culture = $this->context->user->getCulture(); $client = QubitSearch::getInstance()->client; - $indices = QubitSearch::getInstance()->index->getInstance(); + $indices = QubitSearch::getInstance()->index->getIndices(); // Multisearch object $mSearch = new \Elastica\Multi\Search($client); @@ -77,10 +77,10 @@ public function execute($request) foreach ($items as $item) { $search = new \Elastica\Search($client); foreach ($indices as $type => $index) { - $itemType = QubitSearch::getInstance()::ES_TYPE; + $elasticSearchTypeName = QubitSearch::getInstance()::ES_TYPE; // This can be updated in ES 7.x when type params are optional - $search->addIndex($index)->addType($index->getType($itemType)); + $search->addIndex($index)->addType($index->getType($elasticSearchTypeName)); } $query = new \Elastica\Query(); diff --git a/plugins/arElasticSearchPlugin/lib/arElasticSearchMultiIndexWrapper.class.php b/plugins/arElasticSearchPlugin/lib/arElasticSearchMultiIndexWrapper.class.php index bfda98c38f..6eaddbd057 100644 --- a/plugins/arElasticSearchPlugin/lib/arElasticSearchMultiIndexWrapper.class.php +++ b/plugins/arElasticSearchPlugin/lib/arElasticSearchMultiIndexWrapper.class.php @@ -24,32 +24,32 @@ */ class arElasticSearchMultiIndexWrapper { - protected $_instance; + protected $indices; - protected $_indexPrefix; + protected $indexPrefix; public function __construct($prefix) { - $this->_instance = []; + $this->indices = []; - $this->_indexPrefix = $prefix; + $this->indexPrefix = $prefix; } - public function createIndex($name, Elastica\Index $index) + public function addIndex($name, Elastica\Index $index) { $name = $this->getIndexName($name); - $this->_instance[$name] = $index; + $this->indices[$name] = $index; } // Converts camelized Qubit class names to lower case index name used for ElasticSearch public function getIndexName($name) { - return $this->_indexPrefix.'_'.strtolower($name); + return $this->indexPrefix.'_'.strtolower($name); } public function delete() { - foreach ($this->_instance as $index) { + foreach ($this->indices as $index) { $index->delete(); } } @@ -57,18 +57,18 @@ public function delete() public function addDocuments($name, $documents) { $name = $this->getIndexName($name); - $this->_instance[$name]->addDocuments($documents); + $this->indices[$name]->addDocuments($documents); } public function deleteDocuments($name, $documents) { $name = $this->getIndexName($name); - $this->_instance[$name]->deleteDocuments($documents); + $this->indices[$name]->deleteDocuments($documents); } public function refresh() { - foreach ($this->_instance as $index) { + foreach ($this->indices as $index) { $index->refresh(); } } @@ -79,11 +79,19 @@ public function getIndex($name) { $name = $this->getIndexName($name); - return $this->_instance[$name]; + return $this->indices[$name]; } - public function getInstance() + // Alias for getIndex. Can be safely removed once + // calls to getIndex that are external to the plugin have + // been removed or refactored. + public function getType($name) { - return $this->_instance; + return $this->getIndex($name); + } + + public function getIndices() + { + return $this->indices; } } diff --git a/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php b/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php index 814f24787f..c78e9f74fb 100644 --- a/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php +++ b/plugins/arElasticSearchPlugin/lib/arElasticSearchPlugin.class.php @@ -28,7 +28,7 @@ class arElasticSearchPlugin extends QubitSearchEngine /** * Minimum version of Elasticsearch supported. */ - public const MIN_VERSION = '1.3.0'; + public const MIN_VERSION = '6.0.0'; /** * Dummy type for the ElasticSearch index. @@ -53,11 +53,11 @@ class arElasticSearchPlugin extends QubitSearchEngine public $index; /** - * Current batch type, used for batch flush. + * Current batch index name, used for batch flush. * * @var mixed defaults to null */ - protected $currentBatchType; + protected $currentBatchIndexName; /** * Mappings configuration, mapping.yml. @@ -199,7 +199,7 @@ public function flushBatch() // Batch add documents, if any if (count($this->batchAddDocs) > 0) { try { - $this->index->addDocuments($this->currentBatchType, $this->batchAddDocs); + $this->index->addDocuments($this->currentBatchIndexName, $this->batchAddDocs); } catch (Exception $e) { // Clear batchAddDocs if something went wrong too $this->batchAddDocs = []; @@ -213,7 +213,7 @@ public function flushBatch() // Batch delete documents, if any if (count($this->batchDeleteDocs) > 0) { try { - $this->index->deleteDocuments($this->currentBatchType, $this->batchDeleteDocs); + $this->index->deleteDocuments($this->currentBatchIndexName, $this->batchDeleteDocs); } catch (Exception $e) { // Clear batchDeleteDocs if something went wrong too $this->batchDeleteDocs = []; @@ -280,9 +280,9 @@ public function populate($options = []) $errors = []; $showErrors = false; - foreach ($this->mappings as $typeName => $typeProperties) { - if (!in_array(strtolower($typeName), $excludeTypes)) { - $camelizedTypeName = sfInflector::camelize($typeName); + foreach ($this->mappings as $indexName => $indexProperties) { + if (!in_array(strtolower($indexName), $excludeTypes)) { + $camelizedTypeName = sfInflector::camelize($indexName); $className = 'arElasticSearch'.$camelizedTypeName; // If excluding types then index as a whole hasn't been flushed: delete @@ -342,10 +342,14 @@ public function disable() * Centralize document addition to keep control of the batch queue. * * @param mixed $data - * @param mixed $type + * @param mixed $indexName */ - public function addDocument($data, $type) + public function addDocument($data, $indexName) { + if (!$this->enabled) { + return; + } + if (!isset($data['id'])) { throw new sfException('Failed to parse id field.'); } @@ -361,15 +365,15 @@ public function addDocument($data, $type) // but it can be removed in 7.x when it becomes optional $document->setType(self::ES_TYPE); - if (!$this->currentBatchType) { - $this->currentBatchType = $type; + if (!$this->currentBatchIndexName) { + $this->currentBatchIndexName = $indexName; } if ($this->batchMode) { // Add this document to the batch add queue - if ($this->currentBatchType != $type) { + if ($this->currentBatchIndexName != $indexName) { $this->flushBatch(); - $this->currentBatchType = $type; + $this->currentBatchIndexName = $indexName; $this->index->refresh(); } @@ -381,7 +385,7 @@ public function addDocument($data, $type) $this->index->refresh(); } } else { - $this->index->getIndex($type)->addDocuments([$document]); + $this->index->getIndex($indexName)->addDocuments([$document]); } } @@ -405,7 +409,7 @@ public function partialUpdate($object, $data) return; } - $type = get_class($object); + $indexName = get_class($object); $document = new \Elastica\Document($object->id, $data); @@ -414,7 +418,7 @@ public function partialUpdate($object, $data) $document->setType(self::ES_TYPE); try { - $this->index->getIndex($type)->updateDocuments([$document]); + $this->index->getIndex($indexName)->updateDocuments([$document]); } catch (\Elastica\Exception\NotFoundException $e) { // Create document if it's not found $this->update($object); @@ -459,10 +463,10 @@ public function delete($object) } if ($this->batchMode) { - $type = get_class($object); + $indexName = get_class($object); - if (!$this->currentBatchType) { - $this->currentBatchType = $type; + if (!$this->currentBatchIndexName) { + $this->currentBatchIndexName = $indexName; } // The document being deleted may not have been added to the index yet (if it's @@ -472,9 +476,9 @@ public function delete($object) $document = new \Elastica\Document($object->id); $document->setType(self::ES_TYPE); - if ($this->currentBatchType != $type) { + if ($this->currentBatchIndexName != $indexName) { $this->flushBatch(); - $this->currentBatchType = $type; + $this->currentBatchIndexName = $indexName; $this->index->refresh(); } @@ -487,7 +491,7 @@ public function delete($object) } } else { try { - $this->index->getIndex($type)->deleteById($object->id); + $this->index->getIndex($indexName)->deleteById($object->id); } catch (\Elastica\Exception\NotFoundException $e) { // Ignore } @@ -541,14 +545,14 @@ protected function initialize() $this->loadAndNormalizeMappings(); // Iterate over types (actor, informationobject, ...) - foreach ($this->mappings as $typeName => $typeProperties) { - $typeName = 'Qubit'.sfInflector::camelize($typeName); - $this->index->createIndex($typeName, - $this->client->getIndex($this->index->getIndexName($typeName)) + foreach ($this->mappings as $indexName => $indexProperties) { + $indexName = 'Qubit'.sfInflector::camelize($indexName); + $this->index->addIndex($indexName, + $this->client->getIndex($this->index->getIndexName($indexName)) ); } - foreach ($this->index->getInstance() as $indexType => $index) { + foreach ($this->index->getIndices() as $indexType => $index) { try { $index->open(); } catch (Exception $e) { @@ -584,14 +588,11 @@ protected function initialize() ); } - // Load and normalize mappings - $this->loadAndNormalizeMappings(); - // Iterate over types (actor, informationobject, ...) - foreach ($this->mappings as $typeName => $typeProperties) { - $typeName = 'Qubit'.sfInflector::camelize($typeName); + foreach ($this->mappings as $indexName => $indexProperties) { + $indexName = 'Qubit'.sfInflector::camelize($indexName); - if ($indexType != $this->index->getIndexName($typeName)) { + if ($indexType != $this->index->getIndexName($indexName)) { continue; } @@ -601,15 +602,15 @@ protected function initialize() // 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($typeProperties['properties']); + $mapping->setProperties($indexProperties['properties']); // Parse other parameters - unset($this->mapping[$typeName]->typeProperties['properties']); - foreach ($this->mapping[$typeName]->typeProperties as $key => $value) { + unset($this->mapping[$indexName]->indexProperties['properties']); + foreach ($this->mapping[$indexName]->indexProperties as $key => $value) { $mapping->setParam($key, $value); } - $this->log(sprintf('Defining mapping %s...', $typeName)); + $this->log(sprintf('Defining mapping %s...', $indexName)); // In ES 7.x this should be changed to: // $mapping->send($index, [ 'include_type_name' => false ]) @@ -678,9 +679,9 @@ private function displayTypesToIndex($excludeTypes) $this->log('Types that will be indexed:'); - foreach ($this->mappings as $typeName => $typeProperties) { - if (!in_array(strtolower($typeName), $excludeTypes)) { - $this->log(' - '.$typeName); + foreach ($this->mappings as $indexName => $indexProperties) { + if (!in_array(strtolower($indexName), $excludeTypes)) { + $this->log(' - '.$indexName); ++$typeCount; } }