You are here

public function apachesolr_views_query::build in Apache Solr Views 6

Same name and namespace in other branches
  1. 7 apachesolr_views_query.inc \apachesolr_views_query::build()

Build the query object. Load up all avaivable facets so the blocks work.

Overrides views_plugin_query::build

File

./apachesolr_views_query.inc, line 131

Class

apachesolr_views_query
Class for handling a view that gets its data not from the database, but from a Solr server.

Code

public function build(&$view) {
  $view
    ->init_pager();
  if ($this->pager
    ->use_pager()) {
    $this->pager
      ->set_current_page($view->current_page);
  }

  // Let the pager modify the query to add limits.
  $this->pager
    ->query();

  // Build in the default qf, bf and bq if they haven't been set
  // TODO: remove this when we expose and interface via Views UI
  if (empty($this->_query_template) || $this->_query_template == 'dismax') {

    // if no query fields are set, use default
    // TODO: remove when we have this exposed to the Views UI
    if (empty($this->_query_fields)) {
      $qf = variable_get('apachesolr_search_query_fields', array());
      foreach ($qf as $field_name => $boost) {
        if (!empty($boost)) {

          // if its a normed field
          if ($field_name == 'body') {
            $boost *= 40.0;
          }
          $this
            ->add_query_field($field_name, $boost);
        }
      }
    }

    // now we do boost functions
    if (empty($this->_boost_functions)) {
      $this->_params['bf'] = array();
      $solr = $this->_solr_service;
      $total = 0;
      if (isset($solr)) {
        try {
          $data = $solr
            ->getLuke();
        } catch (Exception $e) {
          watchdog('apachesolr_views', $e
            ->getMessage());
        }
        if (isset($data) && isset($data->index->numDocs)) {
          $total = $data->index->numDocs;
        }
      }
      if (empty($total)) {
        $total = db_result(db_query("SELECT COUNT(nid) FROM {node}"));
      }

      // date_settings
      $date_settings = variable_get('apachesolr_search_date_boost', '4:200.0');
      list($date_steepness, $date_boost) = explode(':', $date_settings);
      if (!empty($date_boost)) {
        $values = array(
          $date_steepness,
          $total,
          $total,
          $date_boost,
        );
        $this
          ->add_boost_function("recip(rord(created),%f,%d,%d)^%f", $values);
      }

      // comment_settings
      $comment_settings = variable_get('apachesolr_search_comment_boost', '0:0');
      list($comment_steepness, $comment_boost) = explode(':', $comment_settings);
      if ($comment_boost) {
        $values = array(
          $comment_steepness,
          $total,
          $total,
          $comment_boost,
        );
        $this
          ->add_boost_function("recip(rord(comment_count),%f,%d,%d)^%f", $values);
      }

      // changed_settings
      $changed_settings = variable_get('apachesolr_search_changed_boost', '0:0');
      list($changed_steepness, $changed_boost) = explode(':', $changed_settings);
      if ($changed_boost) {
        $values = array(
          $changed_steepness,
          $total,
          $total,
          $changed_boost,
        );
        $this
          ->add_boost_function("recip(rord(last_comment_or_change),%f,%d,%d)^%f", $values);
      }
    }

    // now we do boost queries
    if (empty($this->_boost_queries)) {

      // Boost for nodes with sticky bit set.
      $sticky_boost = variable_get('apachesolr_search_sticky_boost', 0);
      if ($sticky_boost) {
        $this
          ->add_boost_query('sticky', 'true', $sticky_boost);
      }

      // Boost for nodes with promoted bit set.
      $promote_boost = variable_get('apachesolr_search_promote_boost', 0);
      if ($promote_boost) {
        $this
          ->add_boost_query('promote', 'true', $promote_boost);
      }

      // Modify the weight of results according to the node types.
      $type_boosts = variable_get('apachesolr_search_type_boosts', array());
      if (!empty($type_boosts)) {
        foreach ($type_boosts as $type => $boost) {

          // Only add a param if the boost is != 0 (i.e. > "Normal").
          if (!empty($boost)) {
            $this
              ->add_boost_query('type', $type, $boost);
          }
        }
      }
    }
  }
  try {

    // TODO: add in config screen on the View instead of using apachesolr settings
    // here we add in all the facet stuff for things that are turned on
    // we rely on the apachesolr_search module to provide them for us
    apachesolr_search_add_facet_params($this->_params, $this);
    if ($this->_query_template == 'dismax' || empty($this->_query_template)) {

      // add in the qf to the params array here
      $this->_params['qf'] = array();
      foreach ($this->_query_fields as $field_name => $boost) {
        $this->_params['qf'][] = "{$field_name}^{$boost}";
      }

      // add in the bf
      $this->_params['bf'] = $this->_boost_functions;

      // build the bq
      foreach ($this->_boost_queries as $field_name => $fields) {
        foreach ($fields as $field_value => $boost) {
          $this->_params['bq'][] = "{$field_name}:{$field_value}^{$boost}";
        }
      }
    }

    // Add in facets now
    $this->_params['fq'] = $this
      ->rebuild_fq();

    // process subqueries
    foreach ($this->_subqueries as $query_data) {

      // process the query string first
      $sub_query_string = $query_data['query']
        ->get_query_basic();
      if ($sub_query_string) {
        $this->_query .= " {$query_data['q_op']} ({$sub_query_string})";
      }

      // now handle the filter query
      $subfq = $query_data['query']
        ->get_fq();
      if (!empty($subfq)) {
        $operator = $query_data['fq_op'];
        $this->_params['fq'][] = "(" . implode(" {$operator} ", $subfq) . ")";
      }
    }

    // add in the fields. These fields include the necessary fields.
    $this->_params['fl'] = implode(',', $this->_used_fields);

    // build up the sorts here
    $sort_strings = array();
    foreach ($this->_sorts as $name => $direction) {
      $sort_strings[] = "{$name} {$direction}";
    }
    $this->_params['sort'] = implode(',', $sort_strings);

    // query template handling
    if (!empty($this->_query_template)) {
      $this->_params['qt'] = $this->_query_template;
    }
  } catch (Exception $e) {
    watchdog('apachesolr_views', $e
      ->getMessage());
  }
}