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.



234
235
236
237
238
# File 'lib/elasticity/search.rb', line 234

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Class Method Details

.map_response(relation, body, response) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
# File 'lib/elasticity/search.rb', line 195

def self.map_response(relation, body, response)
  ids = response["hits"]["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.new(relation.where("#{id_col} IN (?)", ids).order(Arel.sql("FIELD(#{id_col}, #{id_vals.join(',')})")), body, response)
  else
    Relation.new(relation.none, body, response)
  end
end

Instance Method Details

#metadataObject



240
241
242
# File 'lib/elasticity/search.rb', line 240

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

#suggestionsObject



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

def suggestions
  [:suggestions]
end

#totalObject



244
245
246
247
248
249
250
251
# File 'lib/elasticity/search.rb', line 244

def total
  res = [:total]
  if res.is_a?(::Hash)
    res["value"]
  else
    res
  end
end