Skip to content

Commit

Permalink
pantheon-systems#177: Port search_api_solr_devel solr index clean-up …
Browse files Browse the repository at this point in the history
…command
  • Loading branch information
Lucas D Hedding committed Mar 29, 2024
1 parent 269ff78 commit 0df38c2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
9 changes: 9 additions & 0 deletions drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
83 changes: 83 additions & 0 deletions src/Commands/IndexManagement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Drupal\search_api_pantheon\Commands;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\search_api\SearchApiException;
use Drupal\search_api_solr\Controller\SolrConfigSetController;
use Drupal\search_api_solr\SearchApiSolrException;
use Drupal\search_api_solr\SolrBackendInterface;
use Drupal\search_api_solr\Utility\SolrCommandHelper;
use Drush\Commands\DrushCommands;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Defines Drush commands for managing Pantheon's Solr index.
*
* @see \Drupal\search_api_solr_devel\Commands\SearchApiSolrDevelCommands
*/
final class IndexManagement extends DrushCommands {

private SolrCommandHelper $commandHelper;

public function __construct(EntityTypeManagerInterface $entityTypeManager, ModuleHandlerInterface $moduleHandler, EventDispatcherInterface $eventDispatcher, SolrConfigSetController $solrConfigSetController) {
parent::__construct();
$this->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.");
}
}

}

0 comments on commit 0df38c2

Please sign in to comment.