Class: Elasticity::Search::LazySearch

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elasticity/search.rb

Constant Summary collapse

DEFAULT_SIZE =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, search_definition, &mapper) ⇒ LazySearch

Returns a new instance of LazySearch.



93
94
95
96
97
# File 'lib/elasticity/search.rb', line 93

def initialize(client, search_definition, &mapper)
  @client            = client
  @search_definition = search_definition
  @mapper            = mapper
end

Instance Attribute Details

#search_definitionObject

Returns the value of attribute search_definition.



89
90
91
# File 'lib/elasticity/search.rb', line 89

def search_definition
  @search_definition
end

Instance Method Details

#aggregationsObject



111
112
113
# File 'lib/elasticity/search.rb', line 111

def aggregations
  response["aggregations"] ||= {}
end

#blank?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/elasticity/search.rb', line 103

def blank?
  empty?
end

#count(args = {}) ⇒ Object



119
120
121
# File 'lib/elasticity/search.rb', line 119

def count(args = {})
  @client.count(@search_definition.to_count_args.reverse_merge(args))["count"]
end

#current_pageObject

for pagination



146
147
148
149
# File 'lib/elasticity/search.rb', line 146

def current_page
  return 1 if @search_definition.body[:from].nil?
  @search_definition.body[:from] / per_page + 1
end

#empty?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/elasticity/search.rb', line 99

def empty?
  total == 0
end

#per_pageObject

for pagination



136
137
138
# File 'lib/elasticity/search.rb', line 136

def per_page
  @search_definition.body[:size] || DEFAULT_SIZE
end

#search_resultsObject



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/elasticity/search.rb', line 123

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

#suggestionsObject



115
116
117
# File 'lib/elasticity/search.rb', line 115

def suggestions
  response["hits"]["suggest"] ||= {}
end

#totalObject



107
108
109
# File 'lib/elasticity/search.rb', line 107

def total
  response["hits"]["total"]
end

#total_pagesObject

for pagination



141
142
143
# File 'lib/elasticity/search.rb', line 141

def total_pages
  (total.to_f / per_page.to_f).ceil
end