Module: Sunspot::DSL::Functional

Included in:
Fulltext
Defined in:
lib/sunspot/dsl/functional.rb

Overview

Mixin DSL to accept functions.

Instance Method Summary collapse

Instance Method Details

#create_function_query(expression) ⇒ Object

Creates an AbstractFunctionQuery from an expression, also called by Sunspot::DSL::Function



32
33
34
35
36
37
38
39
40
# File 'lib/sunspot/dsl/functional.rb', line 32

def create_function_query(expression) #:nodoc:
  if expression.is_a?(Sunspot::Query::FunctionQuery)
    expression
  elsif expression.is_a?(Symbol)
    Sunspot::Query::FieldFunctionQuery.new(@setup.field(expression))
  else
    Sunspot::Query::ConstantFunctionQuery.new(expression)
  end
end

#function(&block) ⇒ Object

Specify a function query with a block that returns an expression.

Examples

function { 10 }
function { :average_rating }
function { sum(:average_rating, 10) }

See wiki.apache.org/solr/FunctionQuery for a list of all applicable functions



20
21
22
23
24
25
26
# File 'lib/sunspot/dsl/functional.rb', line 20

def function(&block)
  expression = Sunspot::Util.instance_eval_or_call(
    Function.new(self),
    &block
  )
  create_function_query(expression)
end