Class: Ion::Indices::Number

Inherits:
Text show all
Defined in:
lib/ion/indices/number.rb

Constant Summary collapse

MARGIN =
0.0001

Instance Attribute Summary

Attributes inherited from Ion::Index

#name, #options

Instance Method Summary collapse

Methods inherited from Text

deindex, #index_words, #search_words

Methods inherited from Ion::Index

deindex, del, #initialize

Constructor Details

This class inherits a constructor from Ion::Index

Instance Method Details

#index(record) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/ion/indices/number.rb', line 5

def index(record)
  value = value_for(record).to_f
  refs  = references_key(record)

  index_key.zadd value, record.id
  refs.sadd index_key
end

#search(what, args = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ion/indices/number.rb', line 13

def search(what, args={})
  key = Ion.volatile_key
  key.zunionstore [index_key] #copy

  if what.is_a?(Hash)
    # Strip away the upper/lower limits.
    key.zremrangebyscore '-inf', what[:gt]  if what[:gt]
    key.zremrangebyscore what[:lt], '+inf'  if what[:lt]
    key.zremrangebyscore '-inf', (what[:min].to_f-MARGIN)  if what[:min]
    key.zremrangebyscore (what[:max].to_f+MARGIN), '+inf'  if what[:max]
  else
    key.zremrangebyscore '-inf', what.to_f-MARGIN
    key.zremrangebyscore what.to_f+MARGIN, '+inf'
  end

  key
end