Class: Cloudsearchable::Query

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

Overview

Wraps a Cloudsearchable::QueryChain, provides methods to execute and reify a query into search result objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clazz, domain, identity_field) ⇒ Query

Returns a new instance of Query.

Parameters:

  • clazz (ActiveRecord::Model)

    The class of the Model object that is being searched. The result set will be objects of this type.

  • domain (Domain)

    Cloudsearchable Domain to search

  • identity_field (Symbol)

    name of the field that contains the id of the clazz (e.g. :collection_id)



171
172
173
174
175
176
# File 'lib/cloudsearchable.rb', line 171

def initialize(clazz, domain, identity_field)
  @query = Cloudsearchable::QueryChain.new(domain, fatal_warnings: Cloudsearchable.config.fatal_warnings)
  @class = clazz
  @query.returning(identity_field)
  @identity_field = identity_field
end

Instance Attribute Details

#classObject (readonly)

Returns the value of attribute class.



162
163
164
# File 'lib/cloudsearchable.rb', line 162

def class
  @class
end

#queryObject (readonly)

Returns the value of attribute query.



162
163
164
# File 'lib/cloudsearchable.rb', line 162

def query
  @query
end

Instance Method Details

#each(&block) ⇒ Object



195
196
197
198
199
200
# File 'lib/cloudsearchable.rb', line 195

def each &block
  # Returns an enumerator
  return enum_for(__method__) unless block_given?
  materialize!
  @results.respond_to?(:each) ? @results.each { |o| yield o } : [@results].send(:each, &block)
end

#found_countObject



202
203
204
# File 'lib/cloudsearchable.rb', line 202

def found_count
  query.found_count
end

#materialize!(*args) ⇒ Object

Pass through to Cloudsearchable::Domain#materialize!, then retrieve objects from database TODO: this does NOT preserve order!



188
189
190
191
192
193
# File 'lib/cloudsearchable.rb', line 188

def materialize!(*args)
  @results ||= begin
    record_ids = @query.map{|result_hit| result_hit['data'][@identity_field.to_s].first}.reject{|r| r.nil?}
    @class.send(@class.materialize_method, record_ids)
  end
end