Method: Sunspot::DSL::Scope#without

Defined in:
lib/sunspot/dsl/scope.rb

#without(*args) ⇒ Object

Build a negative restriction (exclusion). This method can take three forms: equality exclusion, exclusion by another restriction, or identity exclusion. The first two forms work the same way as the #with method; the third excludes a specific instance from the search results.

Parameters (exclusion by field value)

field_name<Symbol>

Name of the field on which to place the exclusion

value<Symbol>

If passed, creates an equality exclusion with this value

Parameters (exclusion by identity)

args<Object>…

One or more instances that should be excluded from the results

Examples

An equality exclusion:

Sunspot.search(Post) do
  without(:blog_id, 1)
end

Other restriction types:

Sunspot.search(Post) do
  without(:average_rating).greater_than(3.0)
end

Exclusion by identity:

Sunspot.search(Post) do
  without(some_post_instance)
end


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sunspot/dsl/scope.rb', line 108

def without(*args)
  case args.first
  when String, Symbol
    field_name = args[0]
    value = args.length > 1 ? args[1] : NONE
    if value == NONE
      DSL::Restriction.new(field_name.to_sym, @query, true)
    else
      @query.add_negated_shorthand_restriction(field_name, value)
    end
  else
    instances = args
    for instance in instances.flatten
      @query.exclude_instance(instance)
    end
  end
end