#504012: index nodes on creation.

From:  <>


---

 modules/node/node.module        |    4 ++--
 modules/search/search.admin.inc |    6 ++++++
 modules/search/search.module    |   16 ++++++++++++++++
 3 files changed, 24 insertions(+), 2 deletions(-)


diff --git modules/node/node.module modules/node/node.module
index fd6c2ff..26d19fd 100644
--- modules/node/node.module
+++ modules/node/node.module
@@ -1969,7 +1969,7 @@ function node_update_index() {
   $result = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, n.nid ASC", 0, $limit);
 
   foreach ($result as $node) {
-    _node_index_node($node);
+    node_index_node($node);
   }
 }
 
@@ -1979,7 +1979,7 @@ function node_update_index() {
  * @param $node
  *   The node to index.
  */
-function _node_index_node($node) {
+function node_index_node($node) {
   $node = node_load($node->nid);
 
   // Save the changed time of the most recent indexed node, for the search
diff --git modules/search/search.admin.inc modules/search/search.admin.inc
index 45be2bf..3877202 100644
--- modules/search/search.admin.inc
+++ modules/search/search.admin.inc
@@ -92,6 +92,12 @@ function search_admin_settings($form) {
     '#type' => 'fieldset',
     '#title' => t('Indexing settings')
   );
+  $form['indexing_settings']['search_index_content_insert'] = array(
+    '#title' => t('Index content when created'),
+    '#type' => 'checkbox',
+    '#default_value' => variable_get('search_index_content_insert', TRUE),
+    '#description' => t('Search module will index content as soon as it is created instead of waiting for a cron run. Not recommended for high traffic sites.'),
+  );
   $form['indexing_settings']['info'] = array(
     '#markup' => t('<p><em>Changing the settings below will cause the site index to be rebuilt. The search index is not cleared but systematically updated to reflect the new settings. Searching will continue to work but new content won\'t be indexed until all existing content has been re-indexed.</em></p><p><em>The default settings should be appropriate for the majority of sites.</em></p>')
   );
diff --git modules/search/search.module modules/search/search.module
index 2344635..efaea24 100644
--- modules/search/search.module
+++ modules/search/search.module
@@ -736,6 +736,22 @@ function search_node_update_index($node) {
 }
 
 /**
+ * Implement hook_node_insert().
+ */
+function search_node_insert($node) {
+  // Index nodes when first created so that the immediately show up in search
+  // results.
+  if (variable_get('search_index_content_insert', TRUE)) {
+    // Register a shutdown function for the search index update to ensure
+    // the node is added to the index, after it has been fully saved (after
+    // all the hook_node_insert() implementation have run).
+    // @see search_cron().
+    register_shutdown_function('node_index_node', $node);
+    register_shutdown_function('search_update_totals');
+  }
+}
+
+/**
  * Implement hook_node_update().
  */
 function search_node_update($node) {
