Class: Lunar::ResultSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lunar/result_set.rb

Overview

This wraps a Lunar search result set. You can do all the expected operations with an Enumerable.

results = Lunar.search(Gadget, :q => "Apple Macbook Pro")
results.class == Lunar::ResultSet
# => true

results.kind_of?(Enumerable)
# => true

#sort and #sort_by commands are available and directly calls the Redis SORT command.

results = Lunar.search(Gadget, :q => "Apple Macbook Pro")
results.sort(:by => :name, :order => "ALPHA")
results.sort(:by => :name, :order => "ALPHA ASC")
results.sort(:by => :name, :order => "ALPHA DESC")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(distkey, nest, finder) ⇒ ResultSet

Returns a new instance of ResultSet.



28
29
30
31
32
33
# File 'lib/lunar/result_set.rb', line 28

def initialize(distkey, nest, finder)
  @distkey   = distkey
  @nest      = nest
  @finder    = finder
  @sortables = @nest[:Sortables]['*']
end

Instance Attribute Details

#distkeyObject (readonly)

Returns the value of attribute distkey.



24
25
26
# File 'lib/lunar/result_set.rb', line 24

def distkey
  @distkey
end

#nestObject (readonly)

Returns the value of attribute nest.



25
26
27
# File 'lib/lunar/result_set.rb', line 25

def nest
  @nest
end

#sortablesObject (readonly)

Returns the value of attribute sortables.



26
27
28
# File 'lib/lunar/result_set.rb', line 26

def sortables
  @sortables
end

Instance Method Details

#eachObject



35
36
37
38
39
# File 'lib/lunar/result_set.rb', line 35

def each
  return if not distkey

  objects(distkey.zrange(0, -1)).each { |e| yield e }
end

#sizeObject



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

def size
  return 0 if not distkey

  distkey.zcard
end

#sort(opts = {}) ⇒ Array

Gives the ability to sort the search results via a ‘sortable` field in your index.

Examples:


Lunar.index Gadget do |i|
  i.id 1001
  i.text :title, "Apple Macbook Pro"
  i.sortable :votes, 10
end

Lunar.index Gadget do |i|
  i.id 1002
  i.text :title, "Apple iPad"
  i.sortable :votes, 50
end

results = Lunar.search(Gadget, :q => "apple")
sorted  = results.sort(:by => :votes, :order => "DESC")

sorted == [Gadget[1002], Gadget[1001]]
# => true

Parameters:

  • opts (Hash) (defaults to: {})

    the various opts to pass to Redis SORT command.

Options Hash (opts):

  • :by (#to_s)

    the field in the namespace you want to sort by.

  • :order (#to_s)

    the direction you want to sort i.e. ASC DESC ALPHA

  • :limit (Array)

    offset and max results to return.

Returns:

  • (Array)

    Array of objects as defined by the ‘finder`.

See Also:



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lunar/result_set.rb', line 90

def sort(opts = {})
  return [] if not distkey
  
  opts[:by] = sortables[opts[:by]]  if opts[:by]

  if opts[:start] && opts[:limit]
    opts[:limit] = [opts[:start], opts[:limit]]
  end

  objects(distkey.sort(opts))
end

#sort_by(att, opts = {}) ⇒ Array

Provides syntatic sugar for ‘sort`.

Examples:


results = Lunar.search(Gadget, :q => "apple")
results.sort(:by => :votes)
results.sort_by(:votes)

Parameters:

  • att (#to_s)

    the field in the namespace you want to sort by.

  • opts (Hash) (defaults to: {})

    the various opts to pass to Redis SORT command.

Options Hash (opts):

  • :order (#to_s)

    the direction you want to sort i.e. ASC DESC ALPHA

  • :limit (Array)

    offset and max results to return.

Returns:

  • (Array)

    Array of objects as defined by the ‘finder`.

See Also:



56
57
58
# File 'lib/lunar/result_set.rb', line 56

def sort_by(att, opts = {})
  sort(opts.merge(:by => att))
end