lib/searchkick/index.rb in searchkick-0.7.4 vs lib/searchkick/index.rb in searchkick-0.7.5
- old
+ new
@@ -24,29 +24,29 @@
def store(record)
client.index(
index: name,
type: document_type(record),
- id: record.id,
+ id: search_id(record),
body: search_data(record)
)
end
def remove(record)
client.delete(
index: name,
type: document_type(record),
- id: record.id
+ id: search_id(record)
)
end
def import(records)
records.group_by{|r| document_type(r) }.each do |type, batch|
client.bulk(
index: name,
type: type,
- body: batch.map{|r| data = search_data(r); {index: {_id: data["_id"] || data["id"] || r.id, data: data}} }
+ body: batch.map{|r| data = search_data(r); {index: {_id: search_id(r), data: data}} }
)
end
end
def retrieve(record)
@@ -69,19 +69,18 @@
def document_type(record)
klass_document_type(record.class)
end
+ def search_id(record)
+ record.id.is_a?(Numeric) ? record.id : record.id.to_s
+ end
+
def search_data(record)
source = record.search_data
# stringify fields
- source = source.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
-
- # Mongoid 4 hack
- if defined?(BSON::ObjectId) and source["_id"].is_a?(BSON::ObjectId)
- source["_id"] = source["_id"].to_s
- end
+ source = source.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}.except("id", "_id")
options = record.class.searchkick_options
# conversions
conversions_field = options[:conversions]