Module: Lunar

Extended by:
Connection
Defined in:
lib/lunar.rb,
lib/lunar/index.rb,
lib/lunar/words.rb,
lib/lunar/scoring.rb,
lib/lunar/stopwords.rb,
lib/lunar/connection.rb,
lib/lunar/fuzzy_word.rb,
lib/lunar/result_set.rb,
lib/lunar/fuzzy_matches.rb,
lib/lunar/range_matches.rb,
lib/lunar/number_matches.rb,
lib/lunar/keyword_matches.rb

Defined Under Namespace

Modules: Connection, Stopwords Classes: FuzzyMatches, FuzzyWord, Index, KeywordMatches, NumberMatches, RangeMatches, ResultSet, Scoring, Words

Constant Summary collapse

VERSION =
'0.6.0'

Class Method Summary collapse

Methods included from Connection

connect, redis, redis=

Class Method Details

.delete(namespace, id) ⇒ nil

Delete a document identified by its namespace and id.

Parameters:

  • namespace (#to_s)

    the namespace of the document to delete.

  • id (#to_s)

    the id of the document to delete.

Returns:

  • (nil)


51
52
53
# File 'lib/lunar.rb', line 51

def self.delete(namespace, id)
  Index.new(namespace).delete(id)
end

.encode(word) ⇒ Object



97
98
99
# File 'lib/lunar.rb', line 97

def self.encode(word)
  Base64.encode64(word).gsub("\n", "")
end

.index(namespace) {|Lunar::Index| ... } ⇒ Lunar::Index

Index any document using a namespace. The namespace can be a class, or a plain Symbol/String.

Examples:


Lunar.index Gadget do |i|
  i.text  :name, 'iphone 3gs'
  i.text  :tags, 'mobile apple smartphone'

  i.number :price, 200
  i.number :rating, 25.5

  i.sortable :votes, 50
end

Parameters:

  • namespace (String, Symbol, Class)

    the namespace of this document.

Yields:

Returns:

See Also:



42
43
44
# File 'lib/lunar.rb', line 42

def self.index(namespace)
  Index.new(namespace).tap { |i| yield i }
end

.metaphone(word) ⇒ Object



92
93
94
# File 'lib/lunar.rb', line 92

def self.metaphone(word)
  Text::Metaphone.metaphone(word)
end

.nestObject



102
103
104
# File 'lib/lunar.rb', line 102

def self.nest
  Nest.new(:Lunar, redis)
end

.search(namespace, options, finder = lambda { |id| namespace[id] }) ⇒ Object

Search for a document, scoped under a namespace.

Examples:


Lunar.search Gadget, :q => "apple"
# returns all gadgets with `text` apple.

Lunar.search Gadget, :q => "apple", :description => "cool"
# returns all gadgets with `text` apple and description:cool

Lunar.search Gadget, :q => "phone", :price => 200..250
# returns all gadgets with `text` phone priced between 200 to 250

Lunar.search Customer, :fuzzy => { :name => "ad" }
# returns all customers with their first / last name beginning with 'ad'

Lunar.search Customer, :fuzzy => { :name => "ad" }, :age => 20..25
# returns all customers with name starting with 'ad' aged 20 to 25.

Parameters:

  • namespace (#to_s)

    search under which scope e.g. Gadget

  • options (Hash)

    i.e. :q, :field1, :field2, :fuzzy

  • finder (#to_proc) (defaults to: lambda { |id| namespace[id] })

    (optional) for cases where ‘Gadget` isn’t the method of finding. You can for example use an ActiveRecord model and pass in lambda { |id| Gadget.find(id) }.

Options Hash (options):

  • :q (Symbol)

    keywords e.g. ‘apple iphone 3g`

  • :field1 (Symbol)

    any field you indexed and a value

  • :fuzzy (Symbol)

    hash of :key => :value pairs

Returns:

  • Lunar::ResultSet an Enumerable object.



84
85
86
87
88
89
# File 'lib/lunar.rb', line 84

def self.search(namespace, options, finder = lambda { |id| namespace[id] })
  sets = find_and_combine_sorted_sets_for(namespace, options)
  key  = try_intersection_of_sorted_sets(namespace, sets, options)

  ResultSet.new(key, nest[namespace], finder)
end