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.
81 82 83 84 85 |
# File 'lib/elasticity/search.rb', line 81 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.
77 78 79 |
# File 'lib/elasticity/search.rb', line 77 def search_definition @search_definition end |
Instance Method Details
#aggregations ⇒ Object
99 100 101 |
# File 'lib/elasticity/search.rb', line 99 def aggregations response["aggregations"] ||= {} end |
#blank? ⇒ Boolean
91 92 93 |
# File 'lib/elasticity/search.rb', line 91 def blank? empty? end |
#count(args = {}) ⇒ Object
107 108 109 |
# File 'lib/elasticity/search.rb', line 107 def count(args = {}) @client.count(@search_definition.to_search_args.reverse_merge(args))["count"] end |
#current_page ⇒ Object
for pagination
134 135 136 137 |
# File 'lib/elasticity/search.rb', line 134 def current_page return 1 if @search_definition.body[:from].nil? @search_definition.body[:from] / per_page + 1 end |
#empty? ⇒ Boolean
87 88 89 |
# File 'lib/elasticity/search.rb', line 87 def empty? total == 0 end |
#per_page ⇒ Object
for pagination
124 125 126 |
# File 'lib/elasticity/search.rb', line 124 def per_page @search_definition.body[:size] || DEFAULT_SIZE end |
#search_results ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/elasticity/search.rb', line 111 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
103 104 105 |
# File 'lib/elasticity/search.rb', line 103 def suggestions response["hits"]["suggest"] ||= {} end |
#total ⇒ Object
95 96 97 |
# File 'lib/elasticity/search.rb', line 95 def total response["hits"]["total"] end |
#total_pages ⇒ Object
for pagination
129 130 131 |
# File 'lib/elasticity/search.rb', line 129 def total_pages (total.to_f / per_page.to_f).ceil end |