You are here

function facetapi_luceneapi_wildcard_matches_get in Facet API 6

Rewrites a wildcard query into primitive terms.

Parameters

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

$text: A string containing the text being parsed.

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

Return value

An array of Zend_Search_Lucene_Index_Term objects.

File

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

Code

function facetapi_luceneapi_wildcard_matches_get(Zend_Search_Lucene_Interface $index, $text, array $fields = array(
  'contents',
)) {

  // Prevents too many terms from being extracted.
  $max_terms = Zend_Search_Lucene::getTermsPerQueryLimit();

  // Gets the word prefix.
  $prefix = facetapi_luceneapi_prefix_get($text);
  $prefix_len = strlen($prefix);
  $pattern = '/^' . str_replace(array(
    '\\?',
    '\\*',
  ), array(
    '.',
    '.*',
  ), preg_quote($text, '/')) . '$/';

  // Caclulates matches.
  $matches = array();
  foreach ($fields as $field) {
    $index
      ->resetTermsStream();
    $index
      ->skipTo(new Zend_Search_Lucene_Index_Term($prefix, $field));
    while ($index
      ->currentTerm() !== NULL && $index
      ->currentTerm()->field == $field) {
      if ($prefix != '' && substr($index
        ->currentTerm()->text, 0, $prefix_len) != $prefix) {
        break;
      }
      if (preg_match($pattern, $index
        ->currentTerm()->text) === 1) {
        $matches[] = $index
          ->currentTerm();

        // Terms per query limit reached.
        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;
}