Technology

Apache Solr: Conditionally Display Facet Blocks

In my last post, I showed a method for conditionally displaying facet blocks with Search Lucene, depending on the type of query being performed.  This can also be adapted to work for Apache Solr, albeit with a different mechanism. The easiest way to do this with Apache Solr is to use the Context module to control which facet blocks are displayed, along with when and where they are displayed.  In this example, I set up a context based on path, where that path was search/apachesolr/*.  I then added all of my Apache Solr facet blocks in the context. With the context configured, I now have the ability to alter that context on the fly, based on whatever conditions I choose. 

<?php 

/**
 * Implementation of hook_context_active_contexts_alter().
 *
 * @param mixed $contexts
 *   Associative array of context objects
 */
function MYMODULE_context_active_contexts_alter(&$contexts) {
  
// If one of the active contexts is the apachesolr_search context, remove 
  // some unneccessary blocks
  
if(in_array('search-apachesolr-search'array_keys($contexts))) {
    
/**
     * Look at the type of query that has been performed, and set a flag regarding whether or 
     * not to show the biblio facets if the type:biblio filter is in effect
     */
    
$show_biblio_facets false;
    
$query apachesolr_current_query();
    if(
$query) {
      if(
$query->has_filter('type''biblio')) {
        
$show_biblio_facets true;
      }
    }
    
// Loop through the apachesolr-search blocks and remove the Biblio facet blocks if needed
    
foreach($contexts['search-apachesolr-search']->block as $bid => $block) {
      if(
preg_match('/^apachesolr_biblio_/'$bid)) {
        if(!
$show_biblio_facets) {
          unset(
$contexts['search-apachesolr-search']->block[$bid]);
        }
      }
    }
  }
}

?>
 In this example, I get all of the active contexts, and if my apachesolr-search context is in effect, I look at the current Apache  Solr query to see what filters have been applied.  In the example, a flag is set to show the biblio facets if the current search is filtering by the biblio content type. There may be other methods for displaying facets conditionally in Apache Solr; however, I find this method to be fairly straightforward, and if you're not currently using Context for controlling block placement in Drupal, you should definitely reconsider that decision.

Crayon Physics

New Drupal Module Released

Back in October, I released my first module for Drupal, the open-source content management system. These days, I seem to be developing exclusively for Drupal, and with a robust API and thriving community, I can only say how much fun it is to work with.

However, like any platforms, there are pieces missing. Fortunately, Drupal is one of those platforms that is very easily extended through modules. I came across one of those missing pieces while working on a project.

Drupal and Taxonomy Weights

I recently worked on a project in Drupal that called for a large number of taxonomy terms. I needed to put the terms in a specific order, but unfortunately, I had more terms than Drupal's weight field supports, which is a range from -10 to +10.

I did a quick search on Drupal, and was horrified to see how many people are hacking core to add a greater range. This is pretty easy to do without hacking core.

Get Firefox Ad

This is a pretty cool ad for Firefox that just makes me laugh...

Firefox, IE, Opera, Netscape, browsers, funny, ad

Flex and Drupal Paths

At CommonPlaces, each developer has his or her own sandbox to code in. Each sandbox can run n instances of a Drupal application, which all run out of subdirectories from the developer's web root.

Hack-proof Your Drupal App - the Video

I had the pleasure of presenting at DrupalCon in Szeged Hungary, and the topic of my presentation was Drupal security from the perspective of the application. I am pleased to be able to share the video of my presentation. Drupal, DrupalCon, CommonPlaces, Szeged, security, hacking, filters, output

DrupalCon Experiences in Szeged, Hungary

I have been attending DrupalCon this week, hosted in the beautiful Hungarian town of Szeged.

I was fortunate in that my company, CommonPlaces, was generous enough to become a silver sponsor for the conference.

Drupal and Sane Flash Remoting

On my latest project, I was faced with a challenge: build Flash widgets that displayed dynamic data and could be embedded on any web page. Phase two of the widgets called for user interaction with the widget, as opposed to simply displaying content.

Drupal: Cross-domain Widgets

Drupal is incredibly flexible, but in current versions, lacks the ability to export content easily in the form of widgets. However, the Services module gives you that flexibility in a very easy to use manner.

Services allows you to expose pieces of your Drupal site, such as user, node, and views methods.

PHP Debugging Goodness

I have found PHP nirvana in a box.

I was trying to debug the lastest dev version of the userpoints module for Drupal, and was getting nowhere. The process of debugging PHP is tedious to begin with, but the practice of putting print statements into your code in places you think are likely the problem is a nightmare, and a huge black hole for productivity.

DrupalCon Boston 2008: Day1

I have the distinct pleasure of attending DrupalCon 2008, which is being held in Boston, MA this year.

Cell Phone Aggravation

Much to my dismay, the manager did not know the product he sold me, or else he would have known that the software does not support my phone. After searching the internet for a few hours, it became clear that a lot of people did not know how to connect their computer to the SCP-3200.

Drupal: Incorrect Pager Results

I've been working on a Drupal module that generates a search form and presents the results below the form.

Drupal: Releasing Custom Modules

I recently built my first module for Drupal, which exposes data from the Userpoints module to Views. There was some talk with the CEO of my company about releasing the module to the community as a contributed module, and some hedging about whether to release it or not.

Releasing modules shouldn't even be a topic of discussion within a company. The work I did on this module was built off the many, many, many hours others have spent on Userpoints and Views. In addition, thousands of developers contributed to, and continue to contribute to Drupal.

Syndicate content

About Erich

Erich is a web developer and a native New Englander who is passionate about life, the universe, and everything.

He is a Drupal consultant, previously employed as a senior developer at Harvard University, working on the IQSS OpenScholar project.  Prior to joining the team at Harvard, he was the engineering manager at CommonPlaces e-Solutions, in Hampstead, NH, contributing as the lead engineer on the Greenopolis.com and Twolia.com.

Erich is active in the Drupal community, having contributed modules and patches to the community. He presented at DrupalCon in Szeged Hungary, and co-presented at DrupalCon 2009 in Washington, DC.

Erich lives in New Hampshire with his wife, two sons, and three weimaraners.  When not writing code, Erich enjoys landscaping and woodworking.

Faceted search

Categories

Content type

Project types

Artwork Type

Artwork Tags

Recent comments

Activity Stream

August 29, 2011

August 25, 2011

August 24, 2011

August 23, 2011

August 15, 2011

August 11, 2011

August 10, 2011

August 9, 2011

August 4, 2011

August 3, 2011