You are here

protected function SearchApiSolrBackend::createFilterQuery in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::createFilterQuery()
  2. 8 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::createFilterQuery()
  3. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::createFilterQuery()

Create a single search query string.

Return value

string|null A filter query.

2 calls to SearchApiSolrBackend::createFilterQuery()
SearchApiSolrBackend::createFilterQueries in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Recursively transforms conditions into a flat array of Solr filter queries.
SearchApiSolrBackend::search in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Options on $query prefixed by 'solr_param_' will be passed natively to Solr as query parameter without the prefix. For example you can set the "Minimum Should Match" parameter 'mm' to '75%' like this:

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 3159

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function createFilterQuery($field, $value, $operator, FieldInterface $index_field, array &$options) {
  if (!is_array($value)) {
    $value = [
      $value,
    ];
  }
  foreach ($value as &$v) {
    if (NULL !== $v || !in_array($operator, [
      '=',
      '<>',
      'IN',
      'NOT IN',
    ])) {
      $v = $this
        ->formatFilterValue($v, $index_field
        ->getType());

      // Remaining NULL values are now converted to empty strings.
    }
  }
  unset($v);
  if (1 == count($value)) {
    $value = array_shift($value);
    switch ($operator) {
      case 'IN':
        $operator = '=';
        break;
      case 'NOT IN':
        $operator = '<>';
        break;
    }
  }
  if (NULL !== $value && isset($options['search_api_location'])) {
    foreach ($options['search_api_location'] as &$spatial) {
      if (!empty($spatial['field']) && $index_field
        ->getFieldIdentifier() == $spatial['field']) {

        // Spatial filter queries need modifications to the query itself.
        // Therefore we just store the parameters an let them be handled
        // later.
        // @see setSpatial()
        // @see createLocationFilterQuery()
        $spatial['filter_query_conditions'] = [
          'field' => $field,
          'value' => $value,
          'operator' => $operator,
        ];
        return NULL;
      }
    }
    unset($spatial);
  }
  switch ($operator) {
    case '<>':
      if (NULL === $value) {
        if ('location' === $index_field
          ->getType()) {
          return $field . ':[-90,-180 TO 90,180]';
        }
        return $this->queryHelper
          ->rangeQuery($field, NULL, NULL);
      }
      return '(*:* -' . $field . ':' . $this->queryHelper
        ->escapePhrase($value) . ')';
    case '<':
      return $this->queryHelper
        ->rangeQuery($field, NULL, $value, FALSE);
    case '<=':
      return $this->queryHelper
        ->rangeQuery($field, NULL, $value);
    case '>=':
      return $this->queryHelper
        ->rangeQuery($field, $value, NULL);
    case '>':
      return $this->queryHelper
        ->rangeQuery($field, $value, NULL, FALSE);
    case 'BETWEEN':
      if ('location' === $index_field
        ->getType()) {
        return $this->queryHelper
          ->rangeQuery($field, array_shift($value), array_shift($value), TRUE, FALSE);
      }
      return $this->queryHelper
        ->rangeQuery($field, array_shift($value), array_shift($value));
    case 'NOT BETWEEN':
      if ('location' === $index_field
        ->getType()) {
        return '(+' . $field . ':[-90,-180 TO 90,180] -' . $this->queryHelper
          ->rangeQuery($field, array_shift($value), array_shift($value), TRUE, FALSE) . ')';
      }
      return '(*:* -' . $this->queryHelper
        ->rangeQuery($field, array_shift($value), array_shift($value)) . ')';
    case 'IN':
      $parts = [];
      $null = FALSE;
      foreach ($value as $v) {
        if (NULL === $v) {
          $null = TRUE;
          break;
        }
        $parts[] = $this->queryHelper
          ->escapePhrase($v);
      }
      if ($null) {

        // @see https://stackoverflow.com/questions/4238609/how-to-query-solr-for-empty-fields/28859224#28859224
        return '(*:* -' . $this->queryHelper
          ->rangeQuery($field, NULL, NULL) . ')';
      }
      return $field . ':(' . implode(' ', $parts) . ')';
    case 'NOT IN':
      $parts = [];
      $null = FALSE;
      foreach ($value as $v) {
        if (NULL === $v) {
          $null = TRUE;
        }
        else {
          $parts[] = $this->queryHelper
            ->escapePhrase($v);
        }
      }
      return '(' . ($null ? $this->queryHelper
        ->rangeQuery($field, NULL, NULL) : '*:*') . ($parts ? ' -' . $field . ':(' . implode(' ', $parts) . ')' : '') . ')';
    case '=':
    default:
      if (NULL === $value) {

        // @see https://stackoverflow.com/questions/4238609/how-to-query-solr-for-empty-fields/28859224#28859224
        return '(*:* -' . $this->queryHelper
          ->rangeQuery($field, NULL, NULL) . ')';
      }
      return $field . ':' . $this->queryHelper
        ->escapePhrase($value);
  }
}