class Elastic::Nodes::FunctionScore

Constants

BOOST_MODES
SCORE_MODES

Attributes

boost_mode[R]
query[RW]
score_mode[R]

Public Class Methods

build(_query) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 8
def self.build(_query)
  new.tap { |node| node.query = _query }
end
new() click to toggle source
# File lib/elastic/nodes/function_score.rb, line 15
def initialize
  @functions = []
end

Public Instance Methods

add_decay_function(_field, _options = {}) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 48
def add_decay_function(_field, _options = {})
  raise NotImplementedError, 'decay function not implemented'
end
add_field_function(_field, factor: 1, modifier: :none, missing: 1, weight: nil, filter: nil) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 37
def add_field_function(_field, factor: 1, modifier: :none, missing: 1, weight: nil, filter: nil)
  params = {
    'field' => _field,
    'factor' => factor,
    'modifier' => modifier,
    'missing' => missing
  }

  add_function('field_value_factor', params, filter, weight)
end
add_weight_function(_weight, filter: nil) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 33
def add_weight_function(_weight, filter: nil)
  add_function(nil, nil, filter, _weight)
end
boost_mode=(_value) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 28
def boost_mode=(_value)
  raise ArgumentError, "invalid boost mode #{_value}" if _value && !BOOST_MODES.include?(_value)
  @boost_mode = _value
end
clone() click to toggle source
# File lib/elastic/nodes/function_score.rb, line 75
def clone
  prepare_clone super, @query.clone
end
Also aliased as: super_clone
clone_with_query(_query) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 79
def clone_with_query(_query)
  prepare_clone super_clone, _query
end
functions=(_values) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 19
def functions=(_values)
  @functions = _values.dup.to_a
end
render(_options = {}) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 57
def render(_options = {})
  hash = { 'query' => @query.render(_options) }
  hash['boost_mode'] = @boost_mode.to_s if @boost_mode && @boost_mode != :multiply

  # TODO: add support for the query_path option
  if @functions.length > 1
    hash['score_mode'] = @score_mode.to_s if @score_mode && @score_mode != :multiply
    hash['functions'] = @functions
  elsif @functions.length == 1
    hash.merge! @functions.first
  end

  { 'function_score' => render_boost(hash) }
end
score_mode=(_value) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 23
def score_mode=(_value)
  raise ArgumentError, "invalid score mode #{_value}" if _value && !SCORE_MODES.include?(_value)
  @score_mode = _value
end
simplify() click to toggle source
# File lib/elastic/nodes/function_score.rb, line 83
def simplify
  new_query = query.simplify

  if @functions.empty?
    return new_query if boost.nil?

    if new_query.class.include?(Concerns::Boostable) && new_query.boost.nil?
      new_query.boost = boost
      return new_query
    end
  end

  prepare_clone(super, new_query)
end
super_clone()
Alias for: clone
traverse(&_block) click to toggle source
Calls superclass method Elastic::Nodes::Base#traverse
# File lib/elastic/nodes/function_score.rb, line 52
def traverse(&_block)
  super
  @query.traverse(&_block)
end

Private Instance Methods

add_function(_function, _params, _filter, _weight) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 100
def add_function(_function, _params, _filter, _weight)
  @functions << {}.tap do |hash|
    hash[_function] = _params if _function
    hash['weight'] = _weight unless _weight.nil?
    hash['filter'] = _filter.render unless _filter.nil?
  end
  self
end
prepare_clone(_clone, _query) click to toggle source
# File lib/elastic/nodes/function_score.rb, line 109
def prepare_clone(_clone, _query)
  _clone.query = _query
  _clone.functions = @functions
  _clone.boost_mode = @boost_mode
  _clone.score_mode = @score_mode
  _clone
end