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.



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_definitionObject

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

#aggregationsObject



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

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

#blank?Boolean

Returns:

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

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

Returns:

  • (Boolean)


87
88
89
# File 'lib/elasticity/search.rb', line 87

def empty?
  total == 0
end

#per_pageObject

for pagination



124
125
126
# File 'lib/elasticity/search.rb', line 124

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

#search_resultsObject



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

#suggestionsObject



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

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

#totalObject



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

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

#total_pagesObject

for pagination



129
130
131
# File 'lib/elasticity/search.rb', line 129

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