lib/mongoid/elasticsearch/es.rb in mongoid-elasticsearch-0.8.3 vs lib/mongoid/elasticsearch/es.rb in mongoid-elasticsearch-0.9.0

- old
+ new

@@ -18,14 +18,20 @@ @index ||= Index.new(self) end def index_all(step_size = INDEX_STEP) index.reset - q = klass.order_by(_id: 1) + q = klass.asc(:id) steps = (q.count / step_size) + 1 + last_id = nil steps.times do |step| - docs = q.skip(step * step_size).limit(step_size) + if last_id + docs = q.gt(id: last_id).limit(step_size).to_a + else + docs = q.limit(step_size).to_a + end + last_id = docs.last.try(:id) docs = docs.map do |obj| if obj.es_index? { index: {data: obj.as_indexed_json}.merge(_id: obj.id.to_s) } else nil @@ -34,10 +40,10 @@ next if docs.empty? client.bulk({body: docs}.merge(type_options)) if block_given? yield steps, step end - end + end end def search(query, options = {}) if query.is_a?(String) query = {q: Utils.clean(query)}