You are here

public function ApacheSolrFacetapiTerm::execute in Apache Solr Search 8

Same name and namespace in other branches
  1. 6.3 plugins/facetapi/query_type_term.inc \ApacheSolrFacetapiTerm::execute()
  2. 7 plugins/facetapi/query_type_term.inc \ApacheSolrFacetapiTerm::execute()

Adds the filter to the query object.

Parameters

DrupalSolrQueryInterface $query: An object containing the query in the backend's native API.

File

plugins/facetapi/query_type_term.inc, line 29
Term query type plugin for the Apache Solr adapter.

Class

ApacheSolrFacetapiTerm
Plugin for "term" query types.

Code

public function execute($query) {
  $settings = $this->adapter
    ->getFacet($this->facet)
    ->getSettings();

  // Adds the operator parameter.
  $operator = $settings->settings['operator'];
  $ex = FACETAPI_OPERATOR_OR != $operator ? '' : "{!ex={$this->facet['field']}}";
  $query
    ->addParam('facet.field', $ex . $this->facet['field']);
  if (!empty($settings->settings['facet_missing'])) {
    $query
      ->addParam('f.' . $this->facet['field'] . '.facet.missing', 'true');
  }

  // Adds "hard limit" parameter to prevent too many return values.
  $limit = empty($settings->settings['hard_limit']) ? 20 : (int) $settings->settings['hard_limit'];
  $query
    ->addParam('f.' . $this->facet['field'] . '.facet.limit', $limit);

  // Adds "facet mincount" parameter to limit the number of facets.
  if (isset($settings->settings['facet_mincount'])) {
    $count = $settings->settings['facet_mincount'];
    $query
      ->addParam('f.' . $this->facet['field'] . '.facet.mincount', $count);
  }
  $active = $this->adapter
    ->getActiveItems($this->facet);

  // Adds filter based on the operator.
  if (FACETAPI_OPERATOR_OR != $operator) {
    foreach ($active as $value => $item) {

      // Handle facet missing:
      if ($value === '_empty_' && !empty($settings->settings['facet_missing'])) {
        $query
          ->addFilter($this->facet['field'], '[* TO *]', TRUE);
      }
      elseif (strlen($value)) {
        $query
          ->addFilter($this->facet['field'], $value);
      }
    }
  }
  else {

    // OR facet.
    $local = "tag={$this->facet['field']}";
    $values = array_keys($active);
    if ($values) {

      // Quote any values that have white space or colons.
      foreach ($values as &$v) {
        if (preg_match('/[:\\s]/', $v) || strlen($v) == 0) {
          $v = '"' . $v . '"';
        }
      }
      $query
        ->addFilter($this->facet['field'], '(' . implode(' OR ', $values) . ')', FALSE, $local);
    }
  }
}