You are here

function facetapi_luceneapi_termfreqs_populate in Facet API 6

Populates the termfreqs() database for the specified fields.

Parameters

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

$fields: An array containing the fields the termFreqs cache is being populated for.

&$context: An optional array containing the batch context when using this function as a batch operation.

Return value

NULL

1 call to facetapi_luceneapi_termfreqs_populate()
facetapi_luceneapi_termfreqs_get in contrib/facetapi_luceneapi/facetapi_luceneapi.cache.inc
Reads termfrequencies from the lookup table to avoid having to calculate it on the fly, which is very performance intensive.
1 string reference to 'facetapi_luceneapi_termfreqs_populate'
facetapi_luceneapi_form_facetapi_admin_settings_form in contrib/facetapi_luceneapi/facetapi_luceneapi.module
Processes facetapi_admin_settings_form form submissions.

File

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

Code

function facetapi_luceneapi_termfreqs_populate($searcher, $fields = NULL, &$context = NULL) {
  if (!($index = luceneapi_index_open($searcher))) {
    return;
  }

  // Gets fields, defaults to all enabled facets.
  if (NULL === $fields) {
    $fields = array();
    foreach (facetapi_enabled_facets_get('luceneapi_node') as $facet) {
      $fields[$facet['field']] = $facet['field'];
    }
  }
  else {
    $fields = drupal_map_assoc((array) $fields);
  }

  // Sets message if this is a batch process.
  if (NULL !== $context) {
    $context['message'] = format_plural(count($fields), 'Populating termFreqs cache for the %fields field', 'Populating termFreqs cache for the %fields fields', array(
      '%fields' => join(', ', $fields),
    ));
  }

  // Gets terms from index.
  $terms = array();
  foreach ($index
    ->terms() as $term) {
    if (isset($fields[$term->field])) {
      $terms[$term->field][$term->text] = $index
        ->termFreqs($term);
    }
  }

  // Populates termfreqs cache for all passed fields.
  $table = '{' . $searcher . '_termfreqs}';
  foreach ($terms as $field => $field_terms) {

    // Builds placeholders and values.
    $values = $placeholders = array();
    foreach ($field_terms as $term => $termfreqs) {
      $placeholders[] = "('%s', '%s', '%s')";
      $values[] = $term;
      $values[] = $field;
      $values[] = serialize($termfreqs);
    }

    // If there are values, inserts data.
    if (!empty($values)) {
      $sql = "DELETE FROM {$table} WHERE field = '%s'";
      db_query($sql, array(
        $field,
      ));

      // @todo Batch in groups of 100? 1000?  OK as is?
      $sql = "INSERT INTO {$table} (term, field, termfreqs) VALUES " . join(',', $placeholders);
      db_query($sql, $values);
    }
  }
}