Class: Mebla::ResultSet

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

Overview

Represents a set of search results

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ResultSet

Creates a new result set from an elasticsearch response hash

Parameters:

  • response


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mebla/result_set.rb', line 14

def initialize(response)
  # Keep the query time
  @time = response['took'] 
  # Keep the facets      
  @facets = response['facets']
  # Keep the query total to check against the count 
  @total = response['hits']['total']       
  
  # Be efficient only query the database once
  model_ids = []
  
  # Collect results' ids
  response['hits']['hits'].each do |hit|
    model_class = hit['_type'].camelize.constantize        
    model_ids << [model_class]

    if model_class.embedded?
      model_class_collection = model_ids.assoc(model_class)
      # collect parent ids
      # [class, [parent_id, ids]]
      parent_id = hit['_source']['_parent']
      
      model_class_collection << [parent_id]
      
      model_class_collection.assoc(parent_id) << hit['_source']['id']
    else
      # collect ids
      # [class, ids]
      model_ids.assoc(model_class) << hit['_source']['id']
    end
  end
  
  # Cast the results into their appropriate classes
  @entries = []

  model_ids.each do |model_class_collection|          
    model_class = model_class_collection.first
    ids = model_class_collection.drop(1)

    unless model_class.embedded?
      # Retrieve the results from the database
      ids.each do |id|
        @entries << model_class.find(id)
      end
    else
      # Get the parent
      parent_class = model_class.embedded_parent
      access_method = model_class.embedded_as
      
      ids.each do |parent_id_collection|
        parent_id = parent_id_collection.first
        entries_ids = parent_id_collection.drop(1)
        
        parent = parent_class.find parent_id
        
        # Retrieve the results from the database
        entries_ids.each do |entry_id|
          @entries << parent.send(access_method.to_sym).find(entry_id)
        end
      end
    end
  end
        
  Mebla.log("WARNING: Index not synchronized with the database; index total hits: #{@total}, retrieved documents: #{self.count}", :warn) if @total != self.count
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



6
7
8
# File 'lib/mebla/result_set.rb', line 6

def entries
  @entries
end

#facetsObject (readonly)

Returns the value of attribute facets.



6
7
8
# File 'lib/mebla/result_set.rb', line 6

def facets
  @facets
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/mebla/result_set.rb', line 6

def time
  @time
end

#totalObject (readonly)

Returns the value of attribute total.



6
7
8
# File 'lib/mebla/result_set.rb', line 6

def total
  @total
end

Instance Method Details

#[](index) ⇒ Object

Returns the item with the given index

Parameters:

  • index (Integer)


87
88
89
# File 'lib/mebla/result_set.rb', line 87

def [](index)
  @entries[index]
end

#each(&block) ⇒ Object

Iterates over the collection



81
82
83
# File 'lib/mebla/result_set.rb', line 81

def each(&block)
  @entries.each(&block)
end