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
110
111
112
113
114
115
116
|
# File 'lib/mongoid/elasticsearch/response.rb', line 110
def count
@total ||= @client.search(@query.merge(search_type: 'count'))['hits']['total']
end
|
#each(&block) ⇒ Object
98
99
100
|
# File 'lib/mongoid/elasticsearch/response.rb', line 98
def each(&block)
results.each(&block)
end
|
#error ⇒ Object
86
87
88
|
# File 'lib/mongoid/elasticsearch/response.rb', line 86
def error
raw_response['error']
end
|
#failure? ⇒ Boolean
94
95
96
|
# File 'lib/mongoid/elasticsearch/response.rb', line 94
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
106
107
108
|
# File 'lib/mongoid/elasticsearch/response.rb', line 106
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
|
# File 'lib/mongoid/elasticsearch/response.rb', line 50
def results
return [] if failure?
@results ||= begin
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
118
119
120
|
# File 'lib/mongoid/elasticsearch/response.rb', line 118
def size
results.size
end
|
#success? ⇒ Boolean
90
91
92
|
# File 'lib/mongoid/elasticsearch/response.rb', line 90
def success?
error.to_s.empty?
end
|
#to_ary ⇒ Object
102
103
104
|
# File 'lib/mongoid/elasticsearch/response.rb', line 102
def to_ary
results
end
|