Apache Solr Spielerei

If you haven't heard of Apache Solr and the integration with Drupal, than you're probably still struggling with the default search shipped with Drupal core. Pity you. Now, this won't be an introduction on the excellent search engine, no, this is a tale about the combination between Apache Solr, node caching and Node displays. Take a look at following snippet:

<?php
/**
* Creme de la creme:
* Put the full node object in the index, so no node_loads are needed for results.
*/
function nd_search_apachesolr_update_index(&$document, $node) {
 
$node->body = $node->content['body']['#value'];
  unset(
$node->content);
 
$document->tm_node = serialize($node);
}
?>

This code lives in nd_search, a small contrib which you can download from the Node Displays Contributions project. This code indexes the complete node object into the Apache Solr engine which you can you use later on either in custom code or on the search results page. Drupal core gives you the freedom to define a custom search function to render the results instead of the default page, which is - IMHO - pretty hard to customize. ND search implements hook_search_page and in combination with the power of ND, we have full control how to render a node per content type, and this without getting any extra data from the database. The code underneath explains it all.

<?php
/**
* Get the serialized version from the node, and unserialize it.
* @param $doc The apache solr document to be converted.
*
* @return Node version from the document.
*/
function _solr_document_to_node($doc) {
 
$node_serialized = $doc['node']->getField('tm_node');
 
$node = unserialize($node_serialized['value']);
  return
$node;
}

/**
* Implementation of hook_search_page().
*/
function apachesolr_search_search_page($results) {
$output = '';

  foreach (

$results as $key => $result) {
   
$node = _solr_document_to_node($result);
   
$node->build_mode = NODE_BUILD_SEARCH_RESULT;
   
$output .= node_view($node);
  }

 

$output .= theme('pager', NULL, 10, 0);

  return

$output;
}
?>

Pretty cool, right? The module also indexes all CCK fiels for you which you can use in custom code if you want to fire custom queries on one of those fields. Following snippet comes from a block where we want to search on a CCK field called 'name'. The result we get back uses the same function to unserialize the node object and after that we call node_view which is altered through the ND module with a custom build mode. Score again!

<?php
  $filter
= 'ss_cck_field_name:swentel';
 
$search_results = apachesolr_search_execute($filter, '', '');
 
$output = '';
  foreach (
$search_results as $key => $result) {
   
$nid = $result['node']->getField('nid');
    if (
$nid['value'] == arg(1)) { // Don't list the same node we're looking at right now.
     
continue;
    }
   
$node = _solr_document_to_node($result);
   
$node->build_mode = 'nd_blocks';
   
$output .= node_view($node, FALSE, FALSE);
  }
  return
$output;
?>

With this power, imagine a search results page with 2 or 3 blocks which doesn't fire any extra queries at the database for extra data. Our ultimate - and probably improbably - dream is to cache all data in apache solr so we don't need to access MySQL anymore. Of course, that's bullocks, but with the project we're currently building (more than 300K nodes to start with) we're pretty sure we'll be able to deliver a nice search experience for our end users.

Note: I'm pretty biased when it comes to the ND project since I'm one of the co-developers , but hey, we're so excited about it and we're planning a lot of new features pretty soon, but more news on that later!

Resolutions

When looking at my resolutions for 2009, I noticed nothing happened yet. But it has been a very interesting year so far and we still have 3 months to go. I can at least predict the concert of the Pixies within 2 weeks will be on the list of highlights for 2009.

Travel: I visited Washington, New York, Paris, London, Paris, Bristol and Norwhich and I probably forget some smallish places. 2 places involved Drupal conferences which might not sound so exciting for most people, but for me it's the right time to get my batteries reloaded. However, I'm getting used to leave my laptop behind now too, equally refreshing.

Coding: there are times I can really profoundly hate what I'm doing, but after a few hours of hacking, mostly during the weekend, my passion is cracking me up. We also had a few good ideas at work which resulted in the release of ND, not fully done yet, but we're getting there. And last but not least, I got to work on the new site of StuBru - but all I got was an unsigned 'lijveken'.

Personal: with all the stuff going on, I can only say I became a few years older in my mind, say 23 ;)

So for 2010, I wouldn't mind if I can't fulfill one of my resolutions.

Topics 

personal

Node displays beta 2 out

We're proud to anounce beta 2 of Node displays, born out of a long code sprint which was called DrupalconParis :)
Main features in this release are:

  • Custom fields via de UI
  • Default node region css
  • More usability changes
  • Tests!

A full list of changes is available on the release page. We also created another screencast with a much nicer theme so you have a better visual of the module. In the meantime, we'll start working on beta 3!

Topics 

drupal, node displays

September is here, I'm on holiday

This is going to be a short post, because, well, I'm on holiday :) I'm currently in Paris for my fifth DrupalCon, pretty amazing, mostly hacking the time away during the day. I'll be back on sunday for another two lazy weeks with some small trips now and then .. aah life can be great! Till then, santé!

And for my buddies at work, here's a great picture, I'm sure you'll understand :)

Topics 

holiday, personal

Introducing the Node displays module

In a previous post, I talked about pimping the CCK ui. In that post a first patch was made available for CCK, however after a long debate, we decided to commit our code as a separate module on d.o, available at http://drupal.org/project/nd. After a very short development cycle, we're proud to release the first public beta version. Features included are:

  • Separated the UI for fields and displays: the order of fields isn't done anymore on the fields overview screen, they are now available on the display screen for every build mode. If you want control over the node edit form, I recommend the excellent nodeform columns module.
  • Node regions: every field can be dropped in its own region.
  • Custom build modes: that's right, create as many build modes you like via the interface or via hook_nd_build_modes().
  • Views row plugin: create lists and use one of the standard or custom build modes.
  • hook_nd_extra_display_fields(): define custom fields. The module itself defines title, author, links, body and taxonomy so far. You can implement your own hook for more fields.
  • Exclude build modes from rendering and use the node template instead.
  • Override core search results page to use node display rendering.

As you can see, lots of fine goodies, but a screencast will explain more than a thousands words. Look at this video to discover the power of this module. We also would like to invite you all to test the beta version and submit bugs, features and/or patches. Happy Node Build Mode building!

Pages

Subscribe to realize.be RSS