Date Published: February 23, 2022
Completely wipe a Solr index for reindexing
In cases where you find content in your Acquia Search index that doesn't belong to the site it is showing up on, you may be experiencing something known as "index pollution". Reindexing via the Drupal module UI usually isn't enough to resolve this. Acquia Support recommends running a PHP script via Drush to completely clear the data from the Solr backend.
PLEASE NOTE
This process will delete all the data from your Solr backend, and is irreversible. You will subsequently need to run a reindex operation in order for search results to populate again.
For Drupal 8, the snippet will look like this. Please note the sections that require replacements. Depending on the number of environments you need to reindex you may need to run this multiple times with different uri options.
# D8 Search API Solr: Wipe a Solr core completely. NOTE: NEEDS EDITING
drush --uri=[domain] ev '
$search_api_server_machine_name = "acquia_search_server"; ## EDIT THIS
$backend = Drupal\search_api\Entity\Server::load($search_api_server_machine_name)->getBackend();
$connector = $backend->getSolrConnector();
# Code based on \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::deleteAllIndexItems()
$query = "solrfieldname:solrfieldvalue"; ## REPLACE "solrfieldname:solrfieldvalue" WITH "*:*" TO DELETE EVERYTHING
$update_query = $connector->getUpdateQuery();
$update_query->addDeleteQuery($query);
$update_query->addOptimize();
$connector->update($update_query);
'
And the Drupal 7 version is as follows
# D7 Search API Solr or apachesolr.module : Wipe a Solr core completely. NOTE: NEEDS EDITING
drush --uri=[domain] ev '
// Get Solr.
function XX_get_solr() {
$solr = false;
if (function_exists("apachesolr_get_solr")) {
// apachesolr.module
$solr = apachesolr_get_solr();
}
elseif (function_exists("search_api_server_load")) {
// search_api_solr.module
$servers = search_api_server_load_multiple(FALSE, array("class" => "acquia_search_service"));
foreach ($servers as $server_id => $server_data) { break; }
$solr = search_api_server_load($server_id)->getSolrConnection();
}
return $solr;
}
$solr = XX_get_solr();
# EDIT THIS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# REALLY! :)
## REPLACE "solrfieldname:solrfieldvalue" WITH "*:*" TO DELETE EVERYTHING
$solr->deleteByQuery("solrfieldname:solrfieldvalue");
'
These commands essentially clear everything in the hosted Solr index, ensuring a fresh start for reindexing. You can then proceed to reindex by whichever method you are most comfortable with, while also clearing any caches such as Varnish that contain search pages.
Did not find what you were looking for?
If this content did not answer your questions, try searching or contacting our support team for further assistance.