You are here

public function ApacheSolrFacetapiNumericRange::build in Apache Solr Search 7

Same name and namespace in other branches
  1. 8 plugins/facetapi/query_type_numeric_range.inc \ApacheSolrFacetapiNumericRange::build()
  2. 6.3 plugins/facetapi/query_type_numeric_range.inc \ApacheSolrFacetapiNumericRange::build()

Initializes the facet's build array.

Any calls to this method need to be wrapped in a try-catch block.

Return value

array The initialized render array.

File

plugins/facetapi/query_type_numeric_range.inc, line 58
Numeric range query type plugin for the Apache Solr adapter.

Class

ApacheSolrFacetapiNumericRange
Plugin for "numeric_range" query types.

Code

public function build() {
  $build = array();
  if (!isset($this->single_key)) {
    return $build;
  }

  // Per key we save our statistics result
  $cache = cache_get('stats_' . $this->single_key, 'cache_apachesolr');
  $stats_minmax = array();
  if (!isset($cache->data)) {

    // we need an additional query for the statistics of the field
    // We can optionally specify a Solr object.
    $solr = apachesolr_get_solr();

    // We possibly need some caching for this query
    $query_stats = apachesolr_drupal_query('apachesolr_stats', array(), '', '', $solr);
    $query_stats
      ->addParam('stats', 'true');
    $query_stats
      ->addParam('stats.field', $this->single_key);
    $query_stats
      ->addParam('stats.facet', $this->single_key);
    $response_stats = $query_stats
      ->search();
    if ($response_stats->response) {
      $stats_minmax = $response_stats->stats->stats_fields->{$this->single_key};
      cache_set('stats_' . $this->single_key, $stats_minmax, 'cache_apachesolr');
    }
  }
  else {

    // Set our statistics from the cache
    $stats_minmax = $cache->data;
  }
  if ($response = apachesolr_static_response_cache($this->adapter
    ->getSearcher())) {
    if (isset($response->stats->stats_fields->{$this->single_key})) {
      $stats = (array) $response->stats->stats_fields->{$this->single_key};
      foreach ($stats as $key => $val) {
        $build[$this->facet['field']]['#range_' . $key] = $val;
        $build[$this->facet['field']]['#global_range_' . $key] = $stats_minmax->{$key};
      }
    }
  }
  return $build;
}