You are here

protected function SearchApiSolrService::createFilterQueries in Search API Solr 7

Transforms a query filter into a flat array of Solr filter queries, using the field names in $fields.

3 calls to SearchApiSolrService::createFilterQueries()
SearchApiSolrService::getAutocompleteSuggestions in includes/service.inc
SearchApiSolrService::search in includes/service.inc
Executes a search on the server represented by this object.
SearchApiSolrService::searchMultiple in includes/service.inc
Implements SearchApiMultiServiceInterface::searchMultiple().

File

includes/service.inc, line 1843

Class

SearchApiSolrService
Search service class using Solr server.

Code

protected function createFilterQueries(SearchApiQueryFilterInterface $filter, array $solr_fields, array $fields) {
  $or = $filter
    ->getConjunction() == 'OR';
  $fq = array();
  $prefix = '';
  foreach ($filter
    ->getFilters() as $f) {
    if (is_array($f)) {
      if (!isset($fields[$f[0]])) {
        throw new SearchApiException(t('Filter term on unknown or unindexed field @field.', array(
          '@field' => $f[0],
        )));
      }
      if ($f[1] !== '') {
        $fq[] = $this
          ->createFilterQuery($solr_fields[$f[0]], $f[1], $f[2], $fields[$f[0]]);
      }
    }
    elseif ($f instanceof SearchApiQueryFilterInterface) {
      $q = $this
        ->createFilterQueries($f, $solr_fields, $fields);
      if ($filter
        ->getConjunction() != $f
        ->getConjunction() && count($q) > 1) {
        $fq[] = '((' . implode(') ' . $f
          ->getConjunction() . ' (', $q) . '))';
      }
      else {
        $fq = array_merge($fq, $q);
      }
    }
  }
  if (method_exists($filter, 'getTags')) {
    foreach ($filter
      ->getTags() as $tag) {
      $prefix = "{!tag={$tag}}";

      // We can only apply one tag per filter.
      break;
    }
  }
  if ($or && count($fq) > 1) {
    $fq = array(
      '((' . implode(') OR (', $fq) . '))',
    );
  }
  if ($prefix) {
    foreach ($fq as $i => $filter) {
      $fq[$i] = $prefix . $filter;
    }
  }
  return $fq;
}