class Elasticity::Search::ActiveRecordProxy

Public Class Methods

map_response(relation, body, response) click to toggle source
# 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
new(client, search_definition, relation) click to toggle source
# 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

Public Instance Methods

metadata() click to toggle source
# File lib/elasticity/search.rb, line 240
def metadata
  @metadata ||= { total: response["hits"]["total"], suggestions: response["suggest"] || {} }
end
method_missing(name, *args, &block) click to toggle source
# File lib/elasticity/search.rb, line 257
def method_missing(name, *args, &block)
  filtered_relation.public_send(name, *args, &block)
end
suggestions() click to toggle source
# File lib/elasticity/search.rb, line 253
def suggestions
  metadata[:suggestions]
end
total() click to toggle source
# File lib/elasticity/search.rb, line 244
def total
  res = metadata[:total]
  if res.is_a?(::Hash)
    res["value"]
  else
    res
  end
end

Private Instance Methods

filtered_relation() click to toggle source
# File lib/elasticity/search.rb, line 267
def filtered_relation
  return @filtered_relation if defined?(@filtered_relation)
  @filtered_relation = ActiveRecordProxy.map_response(@relation, @search_definition.body, response)
end
response() click to toggle source
# File lib/elasticity/search.rb, line 263
def response
  @response ||= @client.search(@search_definition.to_search_args)
end