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.



210
211
212
213
214
# File 'lib/elasticity/search.rb', line 210

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



228
229
230
# File 'lib/elasticity/search.rb', line 228

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

Class Method Details

.from_hits(relation, hits) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/elasticity/search.rb', line 178

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



216
217
218
# File 'lib/elasticity/search.rb', line 216

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

#suggestionsObject



224
225
226
# File 'lib/elasticity/search.rb', line 224

def suggestions
  [:suggestions]
end

#totalObject



220
221
222
# File 'lib/elasticity/search.rb', line 220

def total
  [:total]
end