Class: Sunspot::Query::DynamicQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/query/dynamic_query.rb

Overview

A dynamic query is a proxy object that implements a subset of the API of the Query class, but wraps a dynamic field definition and thus applies the query components using dynamic field instances. – Dynamic queries do not hold their own state, but rather proxy to the query that generated them, adding components directly to the owning query’s internal state. ++ DynamicQuery instances are publicly generated by the Query#dynamic_query factory method.

Instance Method Summary collapse

Constructor Details

#initialize(dynamic_field, query) ⇒ DynamicQuery

:nodoc:



16
17
18
# File 'lib/sunspot/query/dynamic_query.rb', line 16

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

Instance Method Details

#add_field_facet(dynamic_name) ⇒ Object

Add a field facet based on the dynamic field definition and dynamic name given.

Parameters

dynamic_name<Symbol>

Dynamic name to facet on



69
70
71
# File 'lib/sunspot/query/dynamic_query.rb', line 69

def add_field_facet(dynamic_name)
  @query.add_component(FieldFacet.new(@dynamic_field.build(dynamic_name)))
end

#add_negated_restriction(dynamic_name, restriction_type, value) ⇒ Object

Add a negated restriction based on the dynamic field definition and dynamic name given.

Parameters

dynamic_name<Symbol>

Dynamic name to apply to the field in the restriction.

restriction_type<Symbol,Class>

Type of restriction to apply (e.g. Sunspot::Query::Restriction::EqualTo), or symbol shorthand (e.g. :equal_to)

value

Value to apply to the restriction.



57
58
59
# File 'lib/sunspot/query/dynamic_query.rb', line 57

def add_negated_restriction(dynamic_name, restriction_type, value)
  add_restriction(dynamic_name, restriction_type, value, true)
end

#add_restriction(dynamic_name, restriction_type, value, negated = false) ⇒ Object

Add a restriction based on the dynamic field definition and dynamic name given.

Parameters

dynamic_name<Symbol>

Dynamic name to apply to the field in the restriction.

restriction_type<Symbol,Class>

Type of restriction to apply (e.g. Sunspot::Query::Restriction::EqualTo), or symbol shorthand (e.g. :equal_to)

value

Value to apply to the restriction.

negated

Whether to negate the restriction (prefer #add_negated_restriction)



36
37
38
39
40
41
# File 'lib/sunspot/query/dynamic_query.rb', line 36

def add_restriction(dynamic_name, restriction_type, value, negated = false)
  if restriction_type.is_a?(Symbol)
    restriction_type = Restriction[restriction_type]
  end
  @query.add_component(restriction_type.new(@dynamic_field.build(dynamic_name), value, negated))
end

#order_by(dynamic_name, direction) ⇒ Object

Order by the given dynamic field.

Parameters

dynamic_name<Symbol>

Dynamic name of ordering field

direction<Symbol>

Direction in which to order (:asc, :desc)



81
82
83
# File 'lib/sunspot/query/dynamic_query.rb', line 81

def order_by(dynamic_name, direction)
  @query.add_component(Sort.new(@dynamic_field.build(dynamic_name), direction))
end