RSS

Tag Archives: Taxonomy

“OR” search with Apache Solr

Apache Solr is most popular and powerful open source enterprise search platform from the Apache Lucene project. I am using Apache Solr search platform mostly with my Drupal projects. Recently I got one requirement from one of my client to do search with keyword and taxonomy term for a specific content type. Where user can select multiple taxonomy term i.e. we need to do a OR search between the taxonomy terms based on user selection.

After doing some investigation we found that Drupal Apache Solr module doesn’t provide any custom search facility that will meet the client requirement. So I have planned to write a custom module for this. While working on custom module I found a real problem, it seems Faceted Search doesn’t allow to do OR search. So, I have started search on web again. After doing some searching I found one solution how to do “OR” search with Apache Solr Faceted search.

I have modified the Apache Solr query from my custom module by implementing “hook_apachesolr_modify_query“.

  • Collect the current subquery with apache_drupal_query() function.
  • Add taxonomy term ids into the collected sub query with add_filter() function.
  • Finally add all subquery into main query with “OR” operator.

function my_module_apachesolr_modify_query(&$query, &$params, $caller) {
if(isset($_GET[‘tids’])){
$term_ids = explode(‘,’, $_GET[‘tids’]);
$subquery = apachesolr_drupal_query();
foreach ($term_ids as $key => $value){
$subquery->add_filter(‘tid’, $value);
}
$query->add_subquery($subquery, ‘OR’);
watchdog(“my_module”, “Add subquery search for selected term id from my module search page.”);
}
}

Advertisements
 
Leave a comment

Posted by on February 8, 2012 in Drupal

 

Tags: , ,