Class: Mongoid::Elasticsearch::Response
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
          - Object
- Mongoid::Elasticsearch::Response
 show all
      - Includes:
- Enumerable, Pagination
    - Defined in:
- lib/mongoid/elasticsearch/response.rb
 
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  Methods included from Pagination
  #current_page, #first_page?, #last_page?, #next_page, #offset, #out_of_bounds?, #per_page, #previous_page, #total_entries, #total_pages
  
  Constructor Details
  
    
  
  
    #initialize(client, query, multi, model, options)  ⇒ Response 
  
  
  
  
    
Returns a new instance of Response.
   
 
  
  
    | 
14
15
16
17
18
19
20
21 | # File 'lib/mongoid/elasticsearch/response.rb', line 14
def initialize(client, query, multi, model, options)
  @client  = client
  @query   = query
  @multi   = multi
  @model   = model
  @wrapper = options[:wrapper]
  @options = options
end
 | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #facets  ⇒ Object  
  
  
  
  
    
Returns the value of attribute facets.
   
 
  
  
    | 
11
12
13 | # File 'lib/mongoid/elasticsearch/response.rb', line 11
def facets
  @facets
end
 | 
 
    
      
      
      
  
  
    #max  ⇒ Object  
  
  
  
  
    
Returns the value of attribute max.
   
 
  
  
    | 
11
12
13 | # File 'lib/mongoid/elasticsearch/response.rb', line 11
def max
  @max
end
 | 
 
    
      
      
      
  
  
    #options  ⇒ Object  
  
  
  
  
    
Returns the value of attribute options.
   
 
  
  
    | 
11
12
13 | # File 'lib/mongoid/elasticsearch/response.rb', line 11
def options
  @options
end
 | 
 
    
      
      
      
  
  
    #response  ⇒ Object  
  
  
  
  
    
Returns the value of attribute response.
   
 
  
  
    | 
12
13
14 | # File 'lib/mongoid/elasticsearch/response.rb', line 12
def response
  @response
end
 | 
 
    
      
      
      
  
  
    #time  ⇒ Object  
  
  
  
  
    
Returns the value of attribute time.
   
 
  
  
    | 
11
12
13 | # File 'lib/mongoid/elasticsearch/response.rb', line 11
def time
  @time
end
 | 
 
    
      
      
      
  
  
    #total  ⇒ Object  
  
  
  
  
    
Returns the value of attribute total.
   
 
  
  
    | 
11
12
13 | # File 'lib/mongoid/elasticsearch/response.rb', line 11
def total
  @total
end
 | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #count  ⇒ Object 
  
  
  
  
    | 
120
121
122
123
124
125
126 | # File 'lib/mongoid/elasticsearch/response.rb', line 120
def count
  
  
  
  @total ||= @client.search(@query.merge(search_type: 'count'))['hits']['total']
end
 | 
 
    
      
  
  
    #each(&block)  ⇒ Object 
  
  
  
  
    | 
108
109
110 | # File 'lib/mongoid/elasticsearch/response.rb', line 108
def each(&block)
  results.each(&block)
end
 | 
 
    
      
  
  
    #error  ⇒ Object 
  
  
  
  
    | 
96
97
98 | # File 'lib/mongoid/elasticsearch/response.rb', line 96
def error
  raw_response['error']
end
 | 
 
    
      
  
  
    #failure?  ⇒ Boolean 
  
  
  
  
    | 
104
105
106 | # File 'lib/mongoid/elasticsearch/response.rb', line 104
def failure?
  !success?
end
 | 
 
    
      
  
  
    #hits  ⇒ Object 
  
  
  
  
    | 
46
47
48 | # File 'lib/mongoid/elasticsearch/response.rb', line 46
def hits
  @hits ||= raw_response['hits']['hits']
end
 | 
 
    
      
  
  
    #inspect  ⇒ Object 
  
  
  
  
    | 
116
117
118 | # File 'lib/mongoid/elasticsearch/response.rb', line 116
def inspect
  "#<Mongoid::Elasticsearch::Response @size:#{@results.nil? ? 'not run yet' : size} @results:#{@results.inspect} @raw_response=#{@raw_response}>"
end
 | 
 
    
      
  
  
    | 
23
24
25
26
27
28
29
30
31 | # File 'lib/mongoid/elasticsearch/response.rb', line 23
def perform!
  response = @client.search(@query)
  @options = options
  @time = response['took'].to_i
  @total = response['hits']['total'].to_i rescue nil
  @facets = response['facets']
  @max_score = response['hits']['max_score'].to_f rescue nil
  response
end
 | 
 
    
      
  
  
    #raw_response  ⇒ Object 
  
  
  
  
    | 
42
43
44 | # File 'lib/mongoid/elasticsearch/response.rb', line 42
def raw_response
  @raw_response ||= perform!
end
 | 
 
    
      
  
  
    #results  ⇒ Object 
  
  
  
  
    | 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 | # File 'lib/mongoid/elasticsearch/response.rb', line 50
def results
  return [] if failure?
  @results ||= begin
    puts "here are the hits."
    puts JSON.pretty_generate(hits)
    case @wrapper
    when :load
      if @multi
        multi_with_load
      else
        records = @model.find(hits.map { |h| h['_id'] })
        hits.map do |item|
          records.detect do |record|
            record.id.to_s == item['_id'].to_s
          end
        end
      end
    when :mash
    
      hits.map do |h|
        s = h.delete('_source')
        m = Hashie::Mash.new(h.merge(s))
        if defined?(Moped::BSON)
          m.id = Moped::BSON::ObjectId.from_string(h['_id'])
        else
    
          m.id = BSON::ObjectId.from_string(h['_id'])
        end
        m._id = m.id
        m
      end
    when :model
    
      multi_without_load
    else
      hits
    end
  end
end
 | 
 
    
      
  
  
    #size  ⇒ Object 
  
  
    Also known as:
    length
    
  
  
  
    | 
128
129
130 | # File 'lib/mongoid/elasticsearch/response.rb', line 128
def size
  results.size
end
 | 
 
    
      
  
  
    #success?  ⇒ Boolean 
  
  
  
  
    | 
100
101
102 | # File 'lib/mongoid/elasticsearch/response.rb', line 100
def success?
  error.to_s.empty?
end
 | 
 
    
      
  
  
    #to_ary  ⇒ Object 
  
  
  
  
    | 
112
113
114 | # File 'lib/mongoid/elasticsearch/response.rb', line 112
def to_ary
  results
end
 |