Skip to content
This repository has been archived by the owner on Jul 27, 2022. It is now read-only.

ISAICP-6530: Clean orphan Solr document entries #2491

Merged
merged 5 commits into from
Jun 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions web/modules/custom/joinup_core/joinup_core.deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

declare(strict_types = 1);

use Laminas\Diactoros\Uri;

/**
* Update fields in specific solutions.
*/
Expand Down Expand Up @@ -99,3 +101,25 @@ function joinup_core_deploy_0107200(array &$sandbox): string {
$sandbox['#finished'] = ($sandbox['progress'] >= $sandbox['max']) ? 1 : (float) $sandbox['progress'] / (float) $sandbox['max'];
return "Processed {$sandbox['progress']} out of {$sandbox['max']} items.";
}

/**
* Clean orphan Solr document entries.
*/
function joinup_core_deploy_0107201(?array &$sandbox = NULL): void {
$http_client = \Drupal::httpClient();
foreach (['published', 'unpublished'] as $index_id) {
$endpoint = \Drupal::config("search_api.server.solr_{$index_id}")
->get('backend_config.connector_config');
$uri = (string) (new Uri())
->withScheme($endpoint['scheme'])
->withHost($endpoint['host'])
->withPort($endpoint['port'])
->withPath(rtrim($endpoint['path'], '/') . "/{$endpoint['core']}/update")
->withQuery(http_build_query([
'stream.body' => '<delete><query>-hash:tb6juv</query></delete>',
'commit' => 'true',
'wt' => 'json',
]));
$http_client->get($uri);
}
}
65 changes: 65 additions & 0 deletions web/modules/custom/joinup_search/joinup_search.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* @file
* Install, update and uninstall functions for Joinup Search module.
*/

declare(strict_types = 1);

use Drupal\Component\Serialization\Json;
use Drupal\search_api_solr\Utility\Utility;
use Laminas\Diactoros\Uri;

/**
* Implements hook_requirements().
*/
function joinup_search_requirements(string $phase): array {
$requirements = [];
if ($phase !== 'runtime') {
return $requirements;
}

$http_client = \Drupal::httpClient();
$site_hash = Utility::getSiteHash();
foreach (['published', 'unpublished'] as $index_id) {
$endpoint = \Drupal::config("search_api.server.solr_{$index_id}")
->get('backend_config.connector_config');
$uri = (string) (new Uri())
->withScheme($endpoint['scheme'])
->withHost($endpoint['host'])
->withPort($endpoint['port'])
->withPath(rtrim($endpoint['path'], '/') . "/{$endpoint['core']}/select")
->withQuery(http_build_query([
'fq' => "-hash:{$site_hash}",
'q' => '*:*',
'wt' => 'json',
]));

try {
$response = $http_client->get($uri);
}
catch (\Throwable $exception) {
// Ignore error, it's already covered by search_api_solr module.
// @see search_api_solr_requirements()
continue;
}

$num_found = Json::decode($response->getBody()->getContents())['response']['numFound'];
if ($num_found) {
$requirements["solr_data:{$index_id}"] = [
'severity' => REQUIREMENT_ERROR,
'title' => t('Solr index: %index', ['%index' => $index_id]),
'value' => t('Found @count entries with invalid site hash', [
'@count' => $num_found,
]),
'description' => t('The valid site hash is %hash. Retrieve the invalid entries with this <a href=":uri">query</a>.', [
'%hash' => $site_hash,
':uri' => $uri,
]),
];
}
}

return $requirements;
}