diff --git a/modules/feature-search.php b/modules/feature-search.php index d038801..eb8eb4b 100644 --- a/modules/feature-search.php +++ b/modules/feature-search.php @@ -316,6 +316,8 @@ register_module([ $result->time_format = "ms"; $result->decode_time = $env->perfdata->searchindex_decode_time; $result->query_time = $env->perfdata->searchindex_query_time; + if(isset($env->perfdata->didyoumean_correction)) + $result->didyoumean_correction_time = $env->perfdata->didyoumean_correction; $result->total_time = $result->decode_time + $result->query_time; $result->stas = search::stas_parse(search::stas_split($_GET["query"])); $result->search_results = $searchResults; @@ -342,12 +344,6 @@ register_module([ exit(page_renderer::render_main("Error - STAS Query Analysis - $settings->sitename", "

No query was present in the query GET parameter.

")); } - // The indexes are only needed if didyoumean is enabled - if(module_exists("feature-search-didyoumean") && $settings->search_didyoumean_enabled) { - search::invindex_load(); - search::didyoumean_load(); - } - $tokens = search::stas_split($_GET["query"]); $stas_query = search::stas_parse($tokens); diff --git a/modules/lib-search-engine.php b/modules/lib-search-engine.php index fccf2ea..c28c08d 100644 --- a/modules/lib-search-engine.php +++ b/modules/lib-search-engine.php @@ -170,7 +170,18 @@ class search * @return string|null The closest correction found, or null if none could be located. */ public static function didyoumean_correct(string $term) : ?string { - global $settings; + global $settings, $paths, $env; + + $start_time = microtime(true); + + // Load the didyoumean index, but only if it's enabled etc + if(!module_exists("feature-search-didyoumean") || !$settings->search_didyoumean_enabled) + return null; + + // If it's not loaded already, load the didyoumean index on-demand + if(self::$didyoumeanindex == null) + search::didyoumean_load($paths->searchindex); + $results = self::$didyoumeanindex->lookup( $term, $settings->search_didyoumean_editdistance @@ -179,6 +190,10 @@ class search usort($results, function($a, $b) : int { return self::compare($a, $b); }); + + if(!isset($env->perfdata->didyoumean_correction)) + $env->perfdata->didyoumean_correction = 0; + $env->perfdata->didyoumean_correction += (microtime(true) - $start_time) * 1000; return $results[0]; } @@ -664,7 +679,8 @@ class search self::invindex_term_exists($term_data["term"])) continue; // It's not a stop word or in the index, try and correct it - $correction = self::didyoumean_correct($termdata["term"]); + // self::didyoumean_correct auto-loads the didyoumean index on-demand + $correction = self::didyoumean_correct($term_data["term"]); // Make a note if we fail to correct a term if(!is_string($correction)) { $term_data["corrected"] = false;