You are here

protected function SearchApiSolrService::createFilterQuery in Search API Solr 7

Create a single search query string according to the given field, value and operator.

1 call to SearchApiSolrService::createFilterQuery()
SearchApiSolrService::createFilterQueries in includes/service.inc
Transforms a query filter into a flat array of Solr filter queries, using the field names in $fields.

File

includes/service.inc, line 1888

Class

SearchApiSolrService
Search service class using Solr server.

Code

protected function createFilterQuery($field, $value, $operator, $field_info) {
  $field = call_user_func(array(
    $this
      ->getConnectionClass(),
    'escapeFieldName',
  ), $field);

  // Special handling for location fields.
  if (isset($field_info['real_type']) && $field_info['real_type'] == 'location') {

    // Empty / non-empty comparison has to take place in one of the subfields
    // of the location field type. These subfields are usually generated with
    // the index and the field type as name suffix.
    // @TODO Do we need to handle other operators / values too?
    if ($value === NULL) {
      $field .= '_0___tdouble';
    }
  }
  if ($value === NULL) {
    return ($operator == '=' ? '*:* AND -' : '') . "{$field}:[* TO *]";
  }
  $type = search_api_extract_inner_type($field_info['type']);
  if (!is_array($value)) {
    $value = $this
      ->formatFilterValue($value, $type);
  }
  else {
    foreach ($value as &$val) {
      $val = $this
        ->formatFilterValue($val, $type);
    }
    unset($val);
  }
  switch (strtoupper($operator)) {
    case '<>':
      return "*:* AND -({$field}:{$value})";
    case '<':
      return "{$field}:{* TO {$value}}";
    case '<=':
      return "{$field}:[* TO {$value}]";
    case '>=':
      return "{$field}:[{$value} TO *]";
    case '>':
      return "{$field}:{{$value} TO *}";
    case 'BETWEEN':
      return "{$field}:[{$value[0]} TO {$value[1]}]";
    case 'NOT BETWEEN':
      return "*:* AND -{$field}:[{$value[0]} TO {$value[1]}]";
    default:
      return "{$field}:{$value}";
  }
}