Class: Searcher::Boostificator

Inherits:
Object
  • Object
show all
Defined in:
app/models/searcher/boostificator.rb

Constant Summary collapse

MILLIS_IN_YEAR =
1000 * 60 * 60 * 24 * 365
PRECISION =
6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boost_field, options = {}) ⇒ Boostificator

Returns a new instance of Boostificator.



13
14
15
16
17
18
19
# File 'app/models/searcher/boostificator.rb', line 13

def initialize(boost_field, options={})
  self.boost_field = boost_field
  options.reverse_merge!(:a => 1.0, :b => 1.0, :m => 1.0 / MILLIS_IN_YEAR)
  options.each do |attribute, value|
    self.send("#{attribute}=", value)
  end
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



7
8
9
# File 'app/models/searcher/boostificator.rb', line 7

def a
  @a
end

#bObject

Returns the value of attribute b.



7
8
9
# File 'app/models/searcher/boostificator.rb', line 7

def b
  @b
end

#boost_fieldObject

Returns the value of attribute boost_field.



5
6
7
# File 'app/models/searcher/boostificator.rb', line 5

def boost_field
  @boost_field
end

#boostsObject

Returns the value of attribute boosts.



11
12
13
# File 'app/models/searcher/boostificator.rb', line 11

def boosts
  @boosts
end

#mObject

Returns the value of attribute m.



7
8
9
# File 'app/models/searcher/boostificator.rb', line 7

def m
  @m
end

#nowObject

Returns the value of attribute now.



9
10
11
# File 'app/models/searcher/boostificator.rb', line 9

def now
  @now
end

#xObject

Returns the value of attribute x.



7
8
9
# File 'app/models/searcher/boostificator.rb', line 7

def x
  @x
end

Instance Method Details

#adjust_solr_params(searcher) ⇒ Object



21
22
23
24
25
26
27
28
# File 'app/models/searcher/boostificator.rb', line 21

def adjust_solr_params(searcher)
  searcher.configuration.scope do |sunspot|
    sunspot.adjust_solr_params do |params|
      params[:boost] = boosts + [recip_s]
      params[:defType] = 'edismax'
    end
  end
end

#now_msObject



46
47
48
# File 'app/models/searcher/boostificator.rb', line 46

def now_ms
  @now_ms ||= now.to_i * 1000
end

#now_sObject



50
51
52
# File 'app/models/searcher/boostificator.rb', line 50

def now_s
  @now_s ||= now.utc.to_datetime.to_time.iso8601
end

#recipObject



42
43
44
# File 'app/models/searcher/boostificator.rb', line 42

def recip
  @recip ||= a / (m * now_ms + b)
end

#recip_maxObject



58
59
60
# File 'app/models/searcher/boostificator.rb', line 58

def recip_max
  @recip_max ||= sprintf("%f", (recip * 10**PRECISION).ceil/10.0**PRECISION)
end

#recip_minObject



54
55
56
# File 'app/models/searcher/boostificator.rb', line 54

def recip_min
  @recip_min ||= sprintf("%f", (recip * 10**PRECISION).floor/10.0**PRECISION)
end

#recip_sObject



34
35
36
# File 'app/models/searcher/boostificator.rb', line 34

def recip_s
  @recip_s ||= "map(recip(abs(ms(#{now_s},#{boost_field})),#{m},#{a},#{b}),#{recip_min},#{recip_max},1)"
end