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:) ⇒ DictionaryBoostFunction

Returns a new instance of DictionaryBoostFunction.



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

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

Instance Attribute Details

#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



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

def to_solr_s
  sf = solarize_field(field)
  dictionary.to_a.reverse.reduce(1) 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