lib/searchkick/index.rb in searchkick-1.2.0 vs lib/searchkick/index.rb in searchkick-1.2.1

- old
+ new

@@ -51,18 +51,28 @@ def remove(record) bulk_delete([record]) end def bulk_delete(records) - Searchkick.queue_items(records.reject { |r| r.id.blank? }.map { |r| {delete: {_index: name, _type: document_type(r), _id: search_id(r)}} }) + Searchkick.queue_items(records.reject { |r| r.id.blank? }.map { |r| {delete: record_data(r)} }) end def bulk_index(records) - Searchkick.queue_items(records.map { |r| {index: {_index: name, _type: document_type(r), _id: search_id(r), data: search_data(r)}} }) + Searchkick.queue_items(records.map { |r| {index: record_data(r).merge(data: search_data(r))} }) end alias_method :import, :bulk_index + def record_data(r) + data = { + _index: name, + _id: search_id(r), + _type: document_type(r) + } + data[:_routing] = r.search_routing if r.respond_to?(:search_routing) + data + end + def retrieve(record) client.get( index: name, type: document_type(record), id: search_id(record) @@ -461,11 +471,14 @@ } end routing = {} if options[:routing] - routing = {required: true, path: options[:routing].to_s} + routing = {required: true} + unless options[:routing] == true + routing[:path] = options[:routing].to_s + end end dynamic_fields = { # analyzed field must be the default field for include_in_all # http://www.elasticsearch.org/guide/reference/mapping/multi-field-type/ @@ -531,14 +544,19 @@ def client Searchkick.client end def document_type(record) - klass_document_type(record.class) + if record.respond_to?(:search_document_type) + record.search_document_type + else + klass_document_type(record.class) + end end def search_id(record) - record.id.is_a?(Numeric) ? record.id : record.id.to_s + id = record.respond_to?(:search_document_id) ? record.search_document_id : record.id + id.is_a?(Numeric) ? id : id.to_s end def search_data(record) source = record.search_data options = record.class.searchkick_options