You are here

function facetapi_luceneapi_termfreqs_get in Facet API 6

Reads termfrequencies from the lookup table to avoid having to calculate it on the fly, which is very performance intensive.

Parameters

$searcher: A string containing the machine readable name of the searcher module.

$term: A Zend_Search_Lucene_Index_Term object.

Return value

An array keyed by Lucene document ID to term frequency.

2 calls to facetapi_luceneapi_termfreqs_get()
FacetapiLuceneapiAdapter::fetch in contrib/facetapi_luceneapi/facetapi_luceneapi.adapter.inc
Overrides the fetch() function, prepopulates the termFreqs cache by calling the facetapi_luceneapi_termfreqs_get() function.
facetapi_luceneapi_terms_count in contrib/facetapi_luceneapi/facetapi_luceneapi.cache.inc
Gets the filter counts for an array of terms.

File

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

Code

function facetapi_luceneapi_termfreqs_get($searcher, Zend_Search_Lucene_Index_Term $term) {
  static $cache = array();

  // If cache is not populated for the field, reads the field's cached data.
  if (!isset($cache[$searcher][$term->field])) {
    $variable = 'facetapi:termfreqs_cached:' . $searcher . ':::' . $term->field;
    if (!variable_get($variable, FALSE)) {
      facetapi_luceneapi_termfreqs_populate($searcher, $term->field);
      variable_set($variable, TRUE);
    }

    // Reads term frequencies from the database.
    $cache[$searcher][$term->field] = array();
    $table = '{' . $searcher . '_termfreqs}';
    $sql = "SELECT term, termfreqs FROM {$table} WHERE field = '%s'";
    if ($result = db_query($sql, array(
      $term->field,
    ))) {
      while ($record = db_fetch_object($result)) {
        $cache[$searcher][$term->field][$record->term] = array(
          'termfreqs' => $record->termfreqs,
          'unserialized' => FALSE,
        );
      }
    }
  }

  // Returns array of termfreqs, unserializes if necessary.
  if (isset($cache[$searcher][$term->field][$term->text])) {
    $data =& $cache[$searcher][$term->field][$term->text];
    if (empty($data['unserialized'])) {
      $data['termfreqs'] = unserialize($data['termfreqs']);
      $data['unserialized'] = TRUE;
    }
    return $data['termfreqs'];
  }
  else {
    return array();
  }
}