Share Article To

How to index a node at time of its creation/modification in Drupal7?

Drupal would not indexing nodes at the time of its creation/modification.
At the time of executing the cron script drupal indexing all the nodes those are yet not indexed.
That is why if we want to search a node in site(at user end) just after its creation/updation we can’t get this node as the node is not indexed.So our objective is indexing node at the time of its creation/updation(means just after saving it into the database).Drupal provides us a hook(hook_node_insert and hook_node_update) which is execute just after node saved into the database and also provide a function(_node_index_node) which can index a node.
So, we just need to add the following functions in any custom module.
/* Implementing hook_node_insert */
function {module_name}_node_insert($node) {
 //calling the the below function to index the current node.
 _node_index_node($node);
}
/* Implement hook_node_update */
function {module_name}_node_update($node) {
 //calling the the below function to index the current node.
 _node_index_node($node);
}
*NOTE: replace {module_name} by your module name.
After adding the above functions if we create/updation any node it will immediately indexed and you are able to get the created node in your search result at user end.

Leave a Reply

Your email address will not be published. Required fields are marked *