Class: Solr::Query::Request::Boosting::DictionaryBoostFunction

Inherits:
Object
  • Object
show all
Includes:
Support::SchemaHelper
Defined in:
lib/solr/query/request/boosting/dictionary_boost_function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::SchemaHelper

#solarize_field

Constructor Details

#initialize(field:, dictionary:, default_boost: 1) ⇒ DictionaryBoostFunction

Returns a new instance of DictionaryBoostFunction.



10
11
12
13
14
15
# File 'lib/solr/query/request/boosting/dictionary_boost_function.rb', line 10

def initialize(field:, dictionary:, default_boost: 1)
  raise 'dictionary must be a non-empty Hash' if Hash(dictionary).empty?
  @field = field
  @dictionary = dictionary
  @default_boost = default_boost
end

Instance Attribute Details

#default_boostObject (readonly)

Returns the value of attribute default_boost.



8
9
10
# File 'lib/solr/query/request/boosting/dictionary_boost_function.rb', line 8

def default_boost
  @default_boost
end

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



8
9
10
# File 'lib/solr/query/request/boosting/dictionary_boost_function.rb', line 8

def dictionary
  @dictionary
end

#fieldObject (readonly)

Returns the value of attribute field.



8
9
10
# File 'lib/solr/query/request/boosting/dictionary_boost_function.rb', line 8

def field
  @field
end

Instance Method Details

#to_solr_sObject

example: given a hash (dictionary) => 2.0, 3024 => 1.5, 3023 => 1.2 and a field of category_id the resulting boosting function will be: if(eq(category_id_it, 3025), 2.0, if(eq(category_id_it, 3024), 1.5, if(eq(category_id_it, 3023), 1.2, 1))) note that I added spaces for readability, real Solr query functions must always be w/out spaces



23
24
25
26
27
28
29
30
31
32
# File 'lib/solr/query/request/boosting/dictionary_boost_function.rb', line 23

def to_solr_s
  sf = solarize_field(field)
  dictionary.to_a.reverse.reduce(default_boost) do |acc, (field_value, boost)|
    if field_value.is_a?(String) || field_value.is_a?(Symbol)
      "if(termfreq(#{sf},\"#{field_value.to_s.solr_escape}\"),#{boost},#{acc})"
    else
      "if(eq(#{sf},#{field_value}),#{boost},#{acc})"
    end
  end
end