Hello!

Great work on this module -- thanks very much. I need to be able to pass information regarding taxonomy into my search query, but am not sure how to do this. In the standard Drupal search mode, I'd do this by adding " category:2,4,5" (for example) to the end of the query, but after installing Lucene I find I'm unable to do this. What am I doing wrong?

Thanks again!

Comments

cpliakas’s picture

Hi webslinger.

Thanks for your kind words. I understand what you are trying to do, and I wrestled with this problem for a little while. The Zend Framework has a built-in query parser that converts user generated queries into an object oriented format used for the search. Although the basic format is very similar to the core search, it does have some differences like the one you illustrated above. Instead of trying to pre-parse the query or extend the Zend query parser to convert core syntax to Lucene, I decided to go with the Zend query parser "as-is" for reliability and transparency.

In terms of a Lucene solution to your problem, there are a few ways to approach it. One would be to execute a query similar to the following:
category:2 AND category:4 AND category:5

A more terse solution would be to make use of the "field grouping" functionality of Lucene.
category:(2 AND 4 AND 5)

In this case, I will fully admit that the core search syntax it more human friendly than the Lucene syntax. To get an overview of Lucene syntax and it's capabilities, check out the Lucene syntax page in the Drupal handbook. I stole it from the Zend Framework documentation which stole it from the Apache Lucene documentation.

If you are looking to add the query programatically via the API, you can append a subquery via hook_luceneapi_query_alter(). The following assumes you are adding the hook to the "mymodule" module. The hook is invoked after the facet subqueries are appended to the main query and right before the search is executed.

<?php

/**
 * Implementation of hook_luceneapi_query_alter().
 */
function mymodule_luceneapi_query_alter($query, $module, $type = NULL) {
  if ('node' == $type) { // only should apply to nodes
    
    // builds the category subquery
    $categories = array('2', '4', '5');
    $cat_query = luceneapi_query_get('multiterm');
    foreach ($categories as $category) {
      luceneapi_add_term($cat_query, luceneapi_term_get($category, 'category'));
    }
    
    // adds the category subquery to the main query
    luceneapi_add_subquery($query, $cat_query, 'required');
  }
}

Note that the $query parameter does not require an ampersand. In PHP 5, objects are more or less passed by reference automatically.

webslinger’s picture

Exactly what the doctor ordered -- thanks a million!

cpliakas’s picture

Status: Active » Closed (fixed)

Closed manually after weeks of inactivity.