3

I have a pretty basic Drupal 7 web site with Solr search (works great!).

I would now like to perform some "work" after each node is indexed with Solr.

First I tried creating a workflow rule but there is no event for "node has been indexed". Next I looked for Solr API hooks but came up empty handed.

Should I look at adding a hook somewhere in the PHP code? If so where? Or am I overlooking a more obvious solution?

Thanks

  • Which SOLR module are you using? You've tagged the question with both ;) – Clive Jan 23 '14 at 15:11
  • I have a solr stand-alone instance running and then Drupal connects to that using the Apache Solr Search module I believe. I search-api-solr adds API calls that developers can use to interface with Solr from within drupal code? – Brad Hein Jan 23 '14 at 15:50
0

Both Solr modules provide APIs:

But neither provide an after-node-update hook. If I had to guess I'd say it's both modules send data to Solr in batches, not one node at a time.

You might get some joy with the core hook_node_update_index():

Act on a node being indexed for searching.

This hook is invoked during search indexing, after node_load(), and after the result of node_view() is added as $node->rendered to the node object.

|improve this answer|||||
  • Learning as I go I'm building a drupal module to test your suggestion. – Brad Hein Jan 23 '14 at 18:44
  • 1
    I think this hook is going to cause you the same problem you mentioned in the comments on the other answer. Patching the Solr module to provide your own hook might be the simplest solution. If you go down that route, this question on SO should give you a good start – Clive Jan 23 '14 at 19:07
0

What you probably want is the ApacheSolr hook hook_apachesolr_index_document_build. This gets fired when a node (or any entity type) is being processed before sending to solr. Note the 'before', but this is called for every node whenever it is being indexed.

You could exit early from your hook by doing if ($entity_type != 'node') { return $document; } and you can also add or modify fields being sent to solr. The ApacheSolr module processes created/updated nodes in batches (default 200) during each cron run.

|improve this answer|||||
  • This might work but I would have to kick off some sort of asynchronous event that ran my code which requires that the given node be indexed already. Race condition comes to mind... and the time to index a node might increase over time... – Brad Hein Jan 23 '14 at 18:24
  • Sadly, I do not see a direct hook either. If you are comfortable changing the ApacheSolr module code, you could place a call to your own function there, or add your own hook. Also, if you are so inclined you could do it from the Solr server end - writing a plugin that could request a drupal callback page (or just do what you want) when a node document is being inserted/updated. – Wayne Weibel Jan 23 '14 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.