From bf2235bb11ca040558478e6fc5d6717dbcb13693 Mon Sep 17 00:00:00 2001 From: Eric Merrill Date: Thu, 3 Mar 2016 10:56:00 -0500 Subject: [PATCH] MDL-53325 search: Optimize Solr less often --- lang/en/admin.php | 3 +- ...{search_task.php => search_index_task.php} | 7 +-- lib/classes/task/search_optimize_task.php | 61 +++++++++++++++++++ lib/db/tasks.php | 11 +++- search/classes/engine.php | 13 ++++ search/classes/manager.php | 10 +-- search/engine/solr/lang/en/search_solr.php | 1 + version.php | 2 +- 8 files changed, 96 insertions(+), 12 deletions(-) rename lib/classes/task/{search_task.php => search_index_task.php} (90%) create mode 100644 lib/classes/task/search_optimize_task.php diff --git a/lang/en/admin.php b/lang/en/admin.php index a38048bf2f5..1ed26d1dd24 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -1028,7 +1028,8 @@ $string['taskdeleteincompleteusers'] = 'Delete incomplete users'; $string['taskdeleteunconfirmedusers'] = 'Delete unconfirmed users'; $string['taskeventscron'] = 'Background processing for events'; $string['taskfiletrashcleanup'] = 'Cleanup files in trash'; -$string['taskglobalsearch'] = 'Global search indexing'; +$string['taskglobalsearchindex'] = 'Global search indexing'; +$string['taskglobalsearchoptimize'] = 'Global search index optimization'; $string['taskgradecron'] = 'Background processing for gradebook'; $string['tasklegacycron'] = 'Legacy cron processing for plugins'; $string['taskmessagingcleanup'] = 'Background processing for messaging'; diff --git a/lib/classes/task/search_task.php b/lib/classes/task/search_index_task.php similarity index 90% rename from lib/classes/task/search_task.php rename to lib/classes/task/search_index_task.php index f22224a84ff..bdeedf1363a 100644 --- a/lib/classes/task/search_task.php +++ b/lib/classes/task/search_index_task.php @@ -30,7 +30,7 @@ namespace core\task; * @copyright 2015 David Monllao {@link http://www.davidmonllao.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class search_task extends scheduled_task { +class search_index_task extends scheduled_task { /** * Get a descriptive name for this task (shown to admins). @@ -38,7 +38,7 @@ class search_task extends scheduled_task { * @return string */ public function get_name() { - return get_string('taskglobalsearch', 'admin'); + return get_string('taskglobalsearchindex', 'admin'); } /** @@ -53,8 +53,5 @@ class search_task extends scheduled_task { // Indexing database records for modules + rich documents of forum. $globalsearch->index(); - - // Optimize index at last. - $globalsearch->optimize_index(); } } diff --git a/lib/classes/task/search_optimize_task.php b/lib/classes/task/search_optimize_task.php new file mode 100644 index 00000000000..9f4b8feb11b --- /dev/null +++ b/lib/classes/task/search_optimize_task.php @@ -0,0 +1,61 @@ +. + +/** + * A scheduled task for global search. + * + * @package core + * @copyright 2016 Eric Merrill {@link https://www.merrilldigital.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\task; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Runs search index optimization. + * + * @package core + * @copyright 2016 Eric Merrill {@link https://www.merrilldigital.com} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class search_optimize_task extends scheduled_task { + + /** + * Get a descriptive name for this task (shown to admins). + * + * @return string + */ + public function get_name() { + return get_string('taskglobalsearchoptimize', 'admin'); + } + + /** + * Do the job. + * Throw exceptions on errors (the job will be retried). + */ + public function execute() { + if (!\core_search\manager::is_global_search_enabled()) { + return; + } + + $globalsearch = \core_search\manager::instance(); + + // Optimize index at last. + $globalsearch->optimize_index(); + } +} diff --git a/lib/db/tasks.php b/lib/db/tasks.php index 8abc3f6d13b..1a564930462 100644 --- a/lib/db/tasks.php +++ b/lib/db/tasks.php @@ -285,7 +285,7 @@ $tasks = array( 'month' => '*' ), array( - 'classname' => 'core\task\search_task', + 'classname' => 'core\task\search_index_task', 'blocking' => 0, 'minute' => '*/30', 'hour' => '*', @@ -293,6 +293,15 @@ $tasks = array( 'dayofweek' => '*', 'month' => '*' ), + array( + 'classname' => 'core\task\search_optimize_task', + 'blocking' => 0, + 'minute' => '15', + 'hour' => '*/12', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*' + ), array( 'classname' => 'core\task\stats_cron_task', 'blocking' => 0, diff --git a/search/classes/engine.php b/search/classes/engine.php index bd77769f59f..d2097feb62f 100644 --- a/search/classes/engine.php +++ b/search/classes/engine.php @@ -229,6 +229,19 @@ abstract class engine { return $classname; } + /** + * Run any post indexing operations. + * + * Should be overwritten if the search engine needs to do any post index cleanup. + * + * @param int $numdocs The number of documents that were added to the index + * @param bool $fullindex True if a full index was performed + * @return void + */ + public function index_complete($numdocs = 0, $fullindex = false) { + // Nothing by default. + } + /** * Optimizes the search engine. * diff --git a/search/classes/manager.php b/search/classes/manager.php index 3f8f7da5786..0437e24df85 100644 --- a/search/classes/manager.php +++ b/search/classes/manager.php @@ -474,7 +474,7 @@ class manager { // Unlimited time. \core_php_time_limit::raise(); - $anyupdate = false; + $sumdocs = 0; $searchareas = $this->get_search_areas_list(true); foreach ($searchareas as $areaid => $searcharea) { @@ -527,7 +527,7 @@ class manager { } if ($numdocs > 0) { - $anyupdate = true; + $sumdocs += $numdocs; // Commit all remaining documents. $this->engine->commit(); @@ -551,13 +551,15 @@ class manager { } } - if ($anyupdate) { + if ($sumdocs > 0) { $event = \core\event\search_indexed::create( array('context' => \context_system::instance())); $event->trigger(); } - return $anyupdate; + $this->engine->index_complete($sumdocs, $fullindex); + + return (bool)$sumdocs; } /** diff --git a/search/engine/solr/lang/en/search_solr.php b/search/engine/solr/lang/en/search_solr.php index 9869aa5d72f..e82b7558e30 100644 --- a/search/engine/solr/lang/en/search_solr.php +++ b/search/engine/solr/lang/en/search_solr.php @@ -29,6 +29,7 @@ $string['extensionerror'] = 'The Apache Solr PHP extension is not installed. Ple $string['missingconfig'] = 'Your Apache Solr server is not yet configured in Moodle.'; $string['multivaluedfield'] = 'Field "{$a}" returned an array instead of a scalar, the field is probably defined in Solr with "Multivalued" to true, this means that Solr autocreated the field for you when you indexed data because you forgot to run search/engine/solr/cli/setup_schema.php. Please delete the current index, create a new one and run setup_schema.php before indexing data in Solr.'; $string['nodatafromserver'] = 'No data from server'; +$string['optimizetask'] = 'Optimize Solr index'; $string['pluginname'] = 'Solr'; $string['schemafieldautocreated'] = 'Field "{$a}" already exists in Solr schema. You probably forgot to run this script before indexing data and fields were autocreated by Solr. Please delete the current index, create a new one and run setup_schema.php again before indexing data in Solr.'; $string['searchinfo'] = 'Search queries'; diff --git a/version.php b/version.php index 725a10533b1..90fc4b020fa 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2016030400.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2016030400.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes.