Class: Elasticity::Search::ActiveRecordProxy

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

Defined Under Namespace

Classes: Relation

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, search_definition, relation) ⇒ ActiveRecordProxy

Returns a new instance of ActiveRecordProxy.



251
252
253
254
255
# File 'lib/elasticity/search.rb', line 251

def initialize(client, search_definition, relation)
  @client            = client
  @search_definition = search_definition.update(_source: false)
  @relation          = Relation.new(relation)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



269
270
271
# File 'lib/elasticity/search.rb', line 269

def method_missing(name, *args, &block)
  filtered_relation.public_send(name, *args, &block)
end

Class Method Details

.from_hits(relation, hits) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/elasticity/search.rb', line 219

def self.from_hits(relation, hits)
  ids = hits.map { |hit| hit["_id"] }

  if ids.any?
    id_col  = "#{relation.connection.quote_column_name(relation.table_name)}.#{relation.connection.quote_column_name(relation.klass.primary_key)}"
    id_vals = ids.map { |id| relation.connection.quote(id) }
    relation.where("#{id_col} IN (?)", ids).order("FIELD(#{id_col}, #{id_vals.join(',')})")
  else
    relation.none
  end
end

Instance Method Details

#metadataObject



257
258
259
# File 'lib/elasticity/search.rb', line 257

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

#suggestionsObject



265
266
267
# File 'lib/elasticity/search.rb', line 265

def suggestions
  [:suggestions]
end

#totalObject



261
262
263
# File 'lib/elasticity/search.rb', line 261

def total
  [:total]
end