From 0df38c2699e2dd271a00de76cb9c244910fffe26 Mon Sep 17 00:00:00 2001 From: Lucas D Hedding Date: Fri, 29 Mar 2024 10:44:50 -0600 Subject: [PATCH] #177: Port search_api_solr_devel solr index clean-up command --- drush.services.yml | 9 ++++ src/Commands/IndexManagement.php | 83 ++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/Commands/IndexManagement.php diff --git a/drush.services.yml b/drush.services.yml index 57fe20ed..d327f9ba 100644 --- a/drush.services.yml +++ b/drush.services.yml @@ -19,3 +19,12 @@ services: arguments: [ "@logger.factory", "@search_api_pantheon.pantheon_guzzle", "@search_api_pantheon.endpoint", "@search_api_pantheon.solarium_client" ] tags: - { name: drush.command } + search_api_pantheon.index_management: + class: Drupal\search_api_pantheon\Commands\IndexManagement + arguments: + - '@entity_type.manager' + - '@module_handler' + - '@event_dispatcher' + - '@search_api_solr.configset_controller' + tags: + - { name: drush.command } diff --git a/src/Commands/IndexManagement.php b/src/Commands/IndexManagement.php new file mode 100644 index 00000000..1c48289b --- /dev/null +++ b/src/Commands/IndexManagement.php @@ -0,0 +1,83 @@ +commandHelper = new SolrCommandHelper($entityTypeManager, $moduleHandler, $eventDispatcher, $solrConfigSetController); + } + + /** + * {@inheritdoc} + */ + public function setLogger(LoggerInterface $logger): void { + parent::setLogger($logger); + $this->commandHelper->setLogger($logger); + } + + /** + * Deletes *all* documents on a Solr search server (including all indexes). + * + * @param string $server_id + * The ID of the server. + * + * @command search-api-pantheon:sapi-delete-all + * + * @usage search-api-pantheon:sapi-delete-all server_id + * Deletes *all* documents on server_id. + * + * @throws \Drupal\Component\Plugin\Exception\PluginException + * @throws \Drupal\search_api_solr\SearchApiSolrException + * @throws \Drupal\search_api\SearchApiException + */ + public function deleteAll(string $server_id): void { + $servers = $this->commandHelper->loadServers([$server_id]); + if ($server = \reset($servers)) { + $backend = $server->getBackend(); + if ($backend instanceof SolrBackendInterface) { + $connector = $backend->getSolrConnector(); + $update_query = $connector->getUpdateQuery(); + $update_query->addDeleteQuery('*:*'); + $connector->update($update_query); + + foreach ($server->getIndexes() as $index) { + if ($index->status() && !$index->isReadOnly()) { + if ($connector->isCloud()) { + $connector->update($update_query, $backend->getCollectionEndpoint($index)); + } + $index->reindex(); + } + } + } + else { + throw new SearchApiSolrException("The given server ID doesn't use the Solr backend."); + } + } + else { + throw new SearchApiException("The given server ID doesn't exist."); + } + } + +}