You are here

function facetapi_luceneapi_range_matches_get in Facet API 6

Rewrites a range query into primitive terms.

Parameters

$index: A Zend_Search_Lucene_Interface object, such as the one returned by the luceneapi_index_open() function.

$lower: A string containing the lower boundary.

$upper: A string containing the upper boundary.

$inclusive: A boolean flagging whether to include the upper term in the result set.

$fields: An array of fields the terms are matched against.

Return value

An array of Zend_Search_Lucene_Index_Term objects.

1 call to facetapi_luceneapi_range_matches_get()
FacetapiLuceneapiAdapter::fetchDate in contrib/facetapi_luceneapi/facetapi_luceneapi.adapter.inc
Fetches data from facets that filter results by date ranges.

File

contrib/facetapi_luceneapi/facetapi_luceneapi.cache.inc, line 194
Term frequency cache functions.

Code

function facetapi_luceneapi_range_matches_get(Zend_Search_Lucene_Interface $index, $lower, $upper, $inclusive = FALSE, array $fields = array(
  'contents',
)) {

  // Prevents too many terms from being extracted.
  $max_terms = Zend_Search_Lucene::getTermsPerQueryLimit();
  $matches = array();
  foreach ($fields as $field) {
    $index
      ->resetTermsStream();
    if ($lower !== NULL) {
      $lower_term = new Zend_Search_Lucene_Index_Term($lower, $field);
      $index
        ->skipTo($lower_term);
      if (!$inclusive && $index
        ->currentTerm() == $lower_term) {
        $index
          ->nextTerm();
      }
    }
    else {
      $index
        ->skipTo(new Zend_Search_Lucene_Index_Term('', $field));
    }
    if ($upper !== NULL) {

      // Walk up to the upper term
      $upper_term = new Zend_Search_Lucene_Index_Term($upper, $field);
      while ($index
        ->currentTerm() !== NULL && $index
        ->currentTerm()->field == $field && $index
        ->currentTerm()->text < $upper_term->text) {
        $matches[] = $index
          ->currentTerm();
        if ($max_terms != 0 && count($matches) > $max_terms) {
          throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
        }
        $index
          ->nextTerm();
      }
      if ($inclusive && $index
        ->currentTerm() == $upper_term) {

        // Include upper term into result
        $matches[] = $upper_term;
      }
    }
    else {

      // Walk up to the end of field data
      while ($index
        ->currentTerm() !== NULL && $index
        ->currentTerm()->field == $field) {
        $matches[] = $index
          ->currentTerm();
        if ($max_terms != 0 && count($matches) > $max_terms) {
          throw new Zend_Search_Lucene_Exception('Terms per query limit is reached.');
        }
        $index
          ->nextTerm();
      }
    }
    $index
      ->closeTermsStream();
  }
  return $matches;
}