Permalink
Browse files

Add batch record indexing for search.

  • Loading branch information...
jimsafley committed Jul 27, 2012
1 parent 4cdfc44 commit 429ec28c52b66e89a17367192d2035533b1c73a6
@@ -10,6 +10,7 @@
if (has_permission('Security', 'edit')) {
$navArray[__('Security Settings')] = uri('security');
}
+ $navArray[__('Search Settings')] = uri('search/settings');
echo nav(apply_filters('admin_navigation_settings', $navArray));
?>
@@ -0,0 +1,12 @@
+<?php
+$pageTitle = __('Search Settings');
+head(array('title' => $pageTitle, 'content_class' => 'vertical-nav', 'bodyclass'=>'settings primary'));
+?>
+<div class="seven columns alpha">
+<?php common('settings-nav'); ?>
+<?php echo flash(); ?>
+<form method="post">
+ <input type="submit" name="index_search_text" value="Index Search Text"/>
+</form>
+</div>
+<?php foot(); ?>
@@ -6,4 +6,13 @@ public function indexAction()
$results = $this->_helper->db->getTable('SearchText')->search($this->_getParam('query'));
$this->view->results = $results;
}
+
+ public function settingsAction()
+ {
+ if (isset($_POST['index_search_text'])) {
+ Zend_Registry::get('bootstrap')->getResource('jobs')
+ ->sendLongRunning('SearchText_IndexJob');
+ $this->_helper->flashMessenger(__('Indexing search text. This may take a while.'), 'success');
+ }
+ }
}
@@ -0,0 +1,45 @@
+<?php
+class SearchText_IndexJob extends Omeka_JobAbstract
+{
+ /**
+ * List of all class names in the application/models directory that extend
+ * Omeka_Record and implement the search mixin.
+ * @var array
+ */
+ protected $_searchRecords = array('Item', 'File', 'Collection');
+
+ public function perform()
+ {
+ // Get the registry of records that implement the search mixin.
+ $pluginBroker = Zend_Registry::get('pluginbroker');
+ $searchRecords = $pluginBroker->applyFilters('search_records', $this->_searchRecords);
+
+ foreach ($searchRecords as $searchRecord) {
+
+ if (!class_exists($searchRecord)) {
+ // The class does not exist or cannot be found.
+ continue;
+ }
+ $record = new $searchRecord;
+ if (!($record instanceof Omeka_Record)) {
+ // The class is not a valid record.
+ continue;
+ }
+ if (!is_callable(array($record, 'addSearchText'))) {
+ // The record does not implement the search mixin.
+ continue;
+ }
+
+ $pageNumber = 1;
+ $recordTable = $record->getTable();
+ // Query a limited number of rows at a time to prevent memory issues.
+ while ($recordObjects = $recordTable->fetchObjects($recordTable->getSelect()->limitPage($pageNumber, 100))) {
+ foreach ($recordObjects as $recordObject) {
+ // Save the record object, which indexes its search text.
+ $recordObject->save();
+ }
+ $pageNumber++;
+ }
+ }
+ }
+}

0 comments on commit 429ec28

Please sign in to comment.