Class: Sunspot::DSL::FieldQuery

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

Overview

Provides an API for areas of the query DSL that operate on specific fields. This functionality is provided by the query DSL and the dynamic query DSL.

Direct Known Subclasses

Query

Constant Summary

Constants inherited from Scope

Scope::NONE

Instance Method Summary collapse

Methods inherited from Scope

#all_of, #any_of, #text_fields, #with, #without

Constructor Details

#initialize(search, query, setup) ⇒ FieldQuery

:nodoc:



9
10
11
12
# File 'lib/sunspot/dsl/field_query.rb', line 9

def initialize(search, query, setup) #:nodoc:
  @search, @query = search, query
  super(query.scope, setup)
end

Instance Method Details

#dynamic(base_name, &block) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/sunspot/dsl/field_query.rb', line 141

def dynamic(base_name, &block)
  dynamic_field_factory = @setup.dynamic_field_factory(base_name)
  Sunspot::Util.instance_eval_or_call(
    FieldQuery.new(@search, @query, dynamic_field_factory),
    &block
  )
end

#facet(*field_names, &block) ⇒ Object

Request facets on the given field names. If the last argument is a hash, the given options will be applied to all specified fields. See Sunspot::Search#facet and Sunspot::Facet for information on what is returned.

Parameters

field_names…<Symbol>

fields for which to return field facets

Options

:sort<Symbol>

Either :count (values matching the most terms first) or :index (lexical)

:limit<Integer>

The maximum number of facet rows to return

:minimum_count<Integer>

The minimum count a facet row must have to be returned

:zeros<Boolean>

Return facet rows for which there are no matches (equivalent to :minimum_count => 0). Default is false.

:extra<Symbol,Array>

One or more of :any and :none. :any returns a facet row with a count of all matching documents that have some value for this field. :none returns a facet row with a count of all matching documents that have no value for this field. The facet row(s) corresponding to the extras have a value of the symbol passed.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sunspot/dsl/field_query.rb', line 68

def facet(*field_names, &block)
  options = Sunspot::Util.extract_options_from(field_names)

  if block
    if field_names.length != 1
      raise(
        ArgumentError,
        "wrong number of arguments (#{field_names.length} for 1)"
      )
    end
    search_facet = @search.add_query_facet(field_names.first, options)
    Sunspot::Util.instance_eval_or_call(
      QueryFacet.new(@query, @setup, search_facet),
      &block
    )
  elsif options[:only]
    field_names.each do |field_name|
      field = @setup.field(field_name)
      search_facet = @search.add_field_facet(field, options)
      Util.Array(options[:only]).each do |value|
        facet = Sunspot::Query::QueryFacet.new
        facet.add_restriction(field, Sunspot::Query::Restriction::EqualTo, value)
        @query.add_query_facet(facet)
        search_facet.add_row(value, facet.to_boolean_phrase)
      end
    end
  else
    field_names.each do |field_name|
      search_facet = nil
      field = @setup.field(field_name)
      facet =
        if options[:time_range]
          unless field.type == Sunspot::Type::TimeType
            raise(
              ArgumentError,
              ':time_range can only be specified for Date or Time fields'
            )
          end
          search_facet = @search.add_date_facet(field, options)
          Sunspot::Query::DateFieldFacet.new(field, options)
        else
          search_facet = @search.add_field_facet(field)
          Sunspot::Query::FieldFacet.new(field, options)
        end
      @query.add_field_facet(facet)
      Util.Array(options[:extra]).each do |extra|
        extra_facet = Sunspot::Query::QueryFacet.new
        case extra
        when :any
          extra_facet.add_negated_restriction(
            field,
            Sunspot::Query::Restriction::EqualTo,
            nil
          )
        when :none
          extra_facet.add_restriction(
            field,
            Sunspot::Query::Restriction::EqualTo,
            nil
          )
        else
          raise(
            ArgumentError,
            "Allowed values for :extra are :any and :none"
          )
        end
        search_facet.add_row(extra, extra_facet.to_boolean_phrase)
        @query.add_query_facet(extra_facet)
      end
    end
  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)



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sunspot/dsl/field_query.rb', line 22

def order_by(field_name, direction = nil)
  sort =
    if special = Sunspot::Query::Sort.special(field_name)
      special.new(direction)
    else
      Sunspot::Query::Sort::FieldSort.new(
        @setup.field(field_name), direction
      )
    end
  @query.add_sort(sort)
end

#order_by_randomObject

DEPRECATED Use order_by(:random)



37
38
39
# File 'lib/sunspot/dsl/field_query.rb', line 37

def order_by_random
  order_by(:random)
end