Class: Sunspot::DSL::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/dsl/scope.rb

Overview

This DSL presents methods for constructing restrictions and other query elements that are specific to fields. As well as being a superclass of Sunspot::DSL::Query, which presents the main query block, this DSL class is also used directly inside the #dynamic() block, which only allows operations on specific fields.

Direct Known Subclasses

Query

Constant Summary collapse

NONE =
Object.new

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Scope

:nodoc:



13
14
15
# File 'lib/sunspot/dsl/scope.rb', line 13

def initialize(query) #:nodoc:
  @query = query
end

Instance Method Details

#facet(*field_names) ⇒ Object

Request facets on the given field names. See Sunspot::Search#facet and Sunspot::Facet for information on what is returned.

Parameters

field_names…<Symbol>

fields for which to return field facets



130
131
132
133
134
# File 'lib/sunspot/dsl/scope.rb', line 130

def facet(*field_names)
  for field_name in field_names
    @query.add_field_facet(field_name)
  end
end

#order_by(field_name, direction = nil) ⇒ Object

Specify the order that results should be returned in. This method can be called multiple times; precedence will be in the order given.

Parameters

field_name<Symbol>

the field to use for ordering

direction<Symbol>

:asc or :desc (default :asc)



119
120
121
# File 'lib/sunspot/dsl/scope.rb', line 119

def order_by(field_name, direction = nil)
  @query.order_by(field_name, direction)
end

#with(field_name, value = NONE) ⇒ Object

Build a positive restriction. With one argument, this method returns another DSL object which presents methods for attaching various restriction types. With two arguments, acts as a shorthand for creating an equality restriction.

Parameters

field_name<Symbol>

Name of the field on which to place the restriction

value<Symbol>

If passed, creates an equality restriction with this value

Returns

Sunspot::DSL::Query::Restriction

Restriction DSL object (if only one argument is passed)

Examples

An equality restriction:

Sunspot.search do
  with(:blog_id, 1)
end

Other restriction types:

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


48
49
50
51
52
53
54
# File 'lib/sunspot/dsl/scope.rb', line 48

def with(field_name, value = NONE)
  if value == NONE
    DSL::Restriction.new(field_name.to_sym, @query, false)
  else
    @query.add_restriction(field_name, Sunspot::Query::Restriction::EqualTo, value, false)
  end
end

#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


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sunspot/dsl/scope.rb', line 93

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_restriction(field_name, Sunspot::Query::Restriction::EqualTo, value)
    end
  else
    instances = args
    for instance in instances.flatten
      @query.exclude_instance(instance)
    end
  end
end