Module: Elasticsearch::Model::Adapter::Multiple::Records

Defined in:
lib/elasticsearch/model/adapters/multiple.rb

Instance Method Summary collapse

Instance Method Details

#__adapter_name_for_klass(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the adapter registered for a particular ‘klass` or `nil` if not available



103
104
105
# File 'lib/elasticsearch/model/adapters/multiple.rb', line 103

def __adapter_name_for_klass(klass)
  Adapter.adapters.select { |name, checker| checker.call(klass) }.keys.first
end

#__ids_by_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the record IDs grouped by class based on type ‘_type`

Example:

{ Foo => ["1"], Bar => ["1", "5"] }


72
73
74
75
76
77
78
79
80
81
# File 'lib/elasticsearch/model/adapters/multiple.rb', line 72

def __ids_by_type
  ids_by_type = {}

  response.response["hits"]["hits"].each do |hit|
    type = __type_for_hit(hit)
    ids_by_type[type] ||= []
    ids_by_type[type] << hit[:_id]
  end
  ids_by_type
end

#__records_by_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

 Returns the collection of records grouped by class based on ‘_type`

Example:

Foo  => {"1"=> #<Foo id: 1, title: "ABC", ...},
Bar  => #<Bar id: 1, name: "XYZ", ...}

}



37
38
39
40
41
42
43
44
45
# File 'lib/elasticsearch/model/adapters/multiple.rb', line 37

def __records_by_type
  result = __ids_by_type.map do |klass, ids|
    records = __records_for_klass(klass, ids)
    ids     = records.map(&:id).map(&:to_s)
    [ klass, Hash[ids.zip(records)] ]
  end

  Hash[result]
end

#__records_for_klass(klass, ids) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the collection of records for a specific type based on passed ‘klass`



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/elasticsearch/model/adapters/multiple.rb', line 51

def __records_for_klass(klass, ids)
  adapter = __adapter_name_for_klass(klass)

  case adapter
    when Elasticsearch::Model::Adapter::ActiveRecord
      klass.where(klass.primary_key => ids)
    when Elasticsearch::Model::Adapter::Mongoid
      klass.where(:id.in => ids)
    else
      klass.find(ids)
  end
end

#__type_for_hit(hit) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the class of the model corresponding to a specific ‘hit` in Elasticsearch results

See Also:



89
90
91
92
93
94
95
96
97
# File 'lib/elasticsearch/model/adapters/multiple.rb', line 89

def __type_for_hit(hit)
  @@__types ||= {}

  @@__types[ "#{hit[:_index]}::#{hit[:_type]}" ] ||= begin
    Registry.all.detect do |model|
      model.index_name == hit[:_index] && model.document_type == hit[:_type]
    end
  end
end

#recordsObject

Note:

The order of results in the Elasticsearch response is preserved

Returns a collection of model instances, possibly of different classes (ActiveRecord, Mongoid, …)



18
19
20
21
22
23
24
# File 'lib/elasticsearch/model/adapters/multiple.rb', line 18

def records
  records_by_type = __records_by_type

  response.response["hits"]["hits"].map do |hit|
    records_by_type[ __type_for_hit(hit) ][ hit[:_id] ]
  end
end