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.



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_definitionObject

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

#aggregationsObject



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

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

#blank?Boolean

Returns:

  • (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_pageObject

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

Returns:

  • (Boolean)


96
97
98
# File 'lib/elasticity/search.rb', line 96

def empty?
  total == 0
end

#per_pageObject

for pagination



133
134
135
# File 'lib/elasticity/search.rb', line 133

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

#search_resultsObject



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

#suggestionsObject



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

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

#totalObject



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

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

#total_pagesObject

for pagination



138
139
140
# File 'lib/elasticity/search.rb', line 138

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