Class: Elasticity::Search::LazySearch
- Inherits:
-
Object
- Object
- Elasticity::Search::LazySearch
- Includes:
- Enumerable
- Defined in:
- lib/elasticity/search.rb
Constant Summary collapse
- DEFAULT_SIZE =
10
Instance Attribute Summary collapse
-
#search_definition ⇒ Object
Returns the value of attribute search_definition.
Instance Method Summary collapse
- #aggregations ⇒ Object
- #blank? ⇒ Boolean
- #count(args = {}) ⇒ Object
-
#current_page ⇒ Object
for pagination.
- #empty? ⇒ Boolean
-
#initialize(client, search_definition, &mapper) ⇒ LazySearch
constructor
A new instance of LazySearch.
-
#per_page ⇒ Object
for pagination.
- #search_results ⇒ Object
- #suggestions ⇒ Object
- #total ⇒ Object
-
#total_pages ⇒ Object
for pagination.
Constructor Details
#initialize(client, search_definition, &mapper) ⇒ LazySearch
Returns a new instance of LazySearch.
90 91 92 93 94 |
# File 'lib/elasticity/search.rb', line 90 def initialize(client, search_definition, &mapper) @client = client @search_definition = search_definition @mapper = mapper end |
Instance Attribute Details
#search_definition ⇒ Object
Returns the value of attribute search_definition.
86 87 88 |
# File 'lib/elasticity/search.rb', line 86 def search_definition @search_definition end |
Instance Method Details
#aggregations ⇒ Object
108 109 110 |
# File 'lib/elasticity/search.rb', line 108 def aggregations response["aggregations"] ||= {} end |
#blank? ⇒ Boolean
100 101 102 |
# File 'lib/elasticity/search.rb', line 100 def blank? empty? end |
#count(args = {}) ⇒ Object
116 117 118 |
# File 'lib/elasticity/search.rb', line 116 def count(args = {}) @client.count(@search_definition.to_count_args.reverse_merge(args))["count"] end |
#current_page ⇒ Object
for pagination
143 144 145 146 |
# File 'lib/elasticity/search.rb', line 143 def current_page return 1 if @search_definition.body[:from].nil? @search_definition.body[:from] / per_page + 1 end |
#empty? ⇒ Boolean
96 97 98 |
# File 'lib/elasticity/search.rb', line 96 def empty? total == 0 end |
#per_page ⇒ Object
for pagination
133 134 135 |
# File 'lib/elasticity/search.rb', line 133 def per_page @search_definition.body[:size] || DEFAULT_SIZE end |
#search_results ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/elasticity/search.rb', line 120 def search_results return @search_results if defined?(@search_results) hits = response["hits"]["hits"] @search_results = if @mapper.nil? hits else hits.map { |hit| @mapper.(hit) } end end |
#suggestions ⇒ Object
112 113 114 |
# File 'lib/elasticity/search.rb', line 112 def suggestions response["hits"]["suggest"] ||= {} end |
#total ⇒ Object
104 105 106 |
# File 'lib/elasticity/search.rb', line 104 def total response["hits"]["total"] end |
#total_pages ⇒ Object
for pagination
138 139 140 |
# File 'lib/elasticity/search.rb', line 138 def total_pages (total.to_f / per_page.to_f).ceil end |