Class: Sunspot::Query::Scope
- Inherits:
-
Object
- Object
- Sunspot::Query::Scope
- Defined in:
- lib/sunspot/query/scope.rb
Overview
The Scope class encapsulates a set of restrictions that scope search results (as well as query facets rows). This class’s API is exposed by Query::Query and Query::QueryFacetRow.
Direct Known Subclasses
Instance Method Summary collapse
-
#add_component(component) ⇒ Object
Add a component to the connective.
-
#add_conjunction ⇒ Object
Add a conjunction to the scope.
-
#add_disjunction ⇒ Object
Add a disjunction to the scope.
-
#add_negated_restriction(field_name, restriction_type, value) ⇒ Object
Add a negated restriction to the query.
-
#add_negated_shorthand_restriction(field_name, value) ⇒ Object
Add a negated shorthand restriction.
-
#add_restriction(field_name, restriction_type, value, negated = false) ⇒ Object
Add a restriction to the query.
-
#add_shorthand_restriction(field_name, value, negated = false) ⇒ Object
Determine which restriction type to add based on the type of the value.
-
#dynamic_query(base_name) ⇒ Object
Generate a DynamicQuery instance for the given base name.
-
#exclude_instance(instance) ⇒ Object
Exclude a particular instance from the search results.
-
#initialize(setup) ⇒ Scope
constructor
:nodoc:.
-
#to_params ⇒ Object
Representation of this query as solr parameters.
Constructor Details
#initialize(setup) ⇒ Scope
:nodoc:
9 10 11 12 |
# File 'lib/sunspot/query/scope.rb', line 9 def initialize(setup) @setup = setup @components = [] end |
Instance Method Details
#add_component(component) ⇒ Object
Add a component to the connective. All components must implement the #to_boolean_phrase method.
155 156 157 |
# File 'lib/sunspot/query/scope.rb', line 155 def add_component(component) #:nodoc: @components << component end |
#add_conjunction ⇒ Object
Add a conjunction to the scope. In most cases, this will simply return the Scope object itself, since scopes by default combine their restrictions with OR semantics. The Connective::Disjunction class overrides this method to return a Connective::Conjunction.
Returns
- Scope
-
Self or another scope with conjunctive semantics.
82 83 84 |
# File 'lib/sunspot/query/scope.rb', line 82 def add_conjunction self end |
#add_disjunction ⇒ Object
Add a disjunction to the scope. The disjunction can then take a set of restrictions, which are combined with OR semantics.
Returns
- Connective::Disjunction
-
New disjunction
67 68 69 70 |
# File 'lib/sunspot/query/scope.rb', line 67 def add_disjunction add_component(disjunction = Connective::Disjunction.new(@setup)) disjunction end |
#add_negated_restriction(field_name, restriction_type, value) ⇒ Object
Add a negated restriction to the query. The restriction will be taken as the opposite of its usual meaning (e.g., an :equal_to restriction will be “not equal to”.
Parameters
- field_name<Symbol>
-
Name of the field to which the restriction applies
- restriction_type<Class>
-
Subclass of Sunspot::Query::Restriction::Base to instantiate
- value<Object>
-
Value against which the restriction applies (e.g. less_than(2) has a value of 2)
55 56 57 |
# File 'lib/sunspot/query/scope.rb', line 55 def add_negated_restriction(field_name, restriction_type, value) add_restriction(field_name, restriction_type, value, true) end |
#add_negated_shorthand_restriction(field_name, value) ⇒ Object
Add a negated shorthand restriction. See #add_shorthand_restriction
147 148 149 |
# File 'lib/sunspot/query/scope.rb', line 147 def add_negated_shorthand_restriction(field_name, value) add_shorthand_restriction(field_name, value, true) end |
#add_restriction(field_name, restriction_type, value, negated = false) ⇒ Object
Add a restriction to the query.
Parameters
- field_name<Symbol>
-
Name of the field to which the restriction applies
- restriction_type<Class,Symbol>
-
Subclass of Sunspot::Query::Restriction::Base, or snake_cased name as symbol (e.g.,
:equal_to
) - value<Object>
-
Value against which the restriction applies (e.g. less_than(2) has a value of 2)
- negated
-
Whether this restriction should be negated (use add_negated_restriction)
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/sunspot/query/scope.rb', line 29 def add_restriction(field_name, restriction_type, value, negated = false) if restriction_type.is_a?(Symbol) restriction_type = Restriction[restriction_type] end add_component( restriction = restriction_type.new( build_field(field_name), value, negated ) ) restriction end |
#add_shorthand_restriction(field_name, value, negated = false) ⇒ Object
Determine which restriction type to add based on the type of the value. Used to interpret query conditions passed as a hash, as well as the short-form DSL::Scope#with method.
Parameters
- field_name<Symbol>
-
Name of the field on which to apply the restriction
- value<Object,Array,Range>
-
Value to which to apply to the restriction
–
- negated<Boolean>
-
Whether to negate the restriction.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/sunspot/query/scope.rb', line 131 def add_shorthand_restriction(field_name, value, negated = false) #:nodoc: restriction_type = case value when Range Restriction::Between when Array Restriction::AnyOf else Restriction::EqualTo end add_restriction(field_name, restriction_type, value, negated) end |
#dynamic_query(base_name) ⇒ Object
Generate a DynamicQuery instance for the given base name. This gives you access to a subset of the Query API but the operations apply to dynamic fields inside the dynamic field definition specified by base_name
.
Parameters
- base_name<Symbol>
-
Base name of the dynamic field definition to use in the dynamic query operations
Returns
- DynamicQuery
-
Instance providing dynamic query functionality for the given field definitions.
115 116 117 |
# File 'lib/sunspot/query/scope.rb', line 115 def dynamic_query(base_name) DynamicQuery.new(@setup.dynamic_field_factory(base_name), self) end |
#exclude_instance(instance) ⇒ Object
Exclude a particular instance from the search results
Parameters
- instance<Object>
-
instance to exclude from results
93 94 95 |
# File 'lib/sunspot/query/scope.rb', line 93 def exclude_instance(instance) add_component(Restriction::SameAs.new(instance, true)) end |
#to_params ⇒ Object
Representation of this query as solr parameters.
Returns
- Hash
-
Representation of query in Solr form
166 167 168 169 170 171 172 |
# File 'lib/sunspot/query/scope.rb', line 166 def to_params #:nodoc: params = {} for component in @components Util.deep_merge!(params, component.to_params) end params end |