Skip to content

Commit

Permalink
fix: update fields by ids when full reindex and ids present
Browse files Browse the repository at this point in the history
  • Loading branch information
TamarinEA committed Apr 26, 2017
1 parent 0a53cd6 commit fed8962
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/sphinx/integration/transmitter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ def update_fields(fields, where)
partitions { |partition| mysql_client.update(index.rt_name(partition), fields, where, matching) }

# и зареплейсим всё что осталось в core
# TODO: implement sphinx transactions
batch_options = {where: where, matching: matching}
mysql_client.find_in_batches(index.core_name, batch_options) do |ids|
klass.where(id: ids).each { |record| transmit(index, record) }
sleep 1 # empirical number
if where[:sphinx_internal_id].present?
transmit_all(index, where[:sphinx_internal_id])
else
# TODO: implement sphinx transactions
batch_options = {where: where, matching: matching}
mysql_client.find_in_batches(index.core_name, batch_options) do |ids|
transmit_all(index, ids)
sleep 1 # empirical number
end
end
else
mysql_client.update(index.name, fields, where, matching)
Expand All @@ -106,6 +110,10 @@ def transmit(index, record)
Sphinx::Integration::WasteRecords.for(index).add(record.sphinx_document_id) if full_reindex?
end

def transmit_all(index, ids)
klass.where(id: ids).each { |record| transmit(index, record) }
end

# Данные, необходимые для записи в индекс сфинкса
#
# index - ThinkingSphinx::Index
Expand Down
13 changes: 13 additions & 0 deletions spec/sphinx/integration/transmitter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@

transmitter.update_fields({field: 123}, id: 1, matching: "@id_idx 1")
end

context 'when with sphinx_internal_id condition' do
it do
expect(mysql_client).to receive(:update).
with("model_with_rt_rt0", {field: 123}, {sphinx_internal_id: 1}, "@id_idx 1")
expect(mysql_client).to receive(:update).
with("model_with_rt_rt1", {field: 123}, {sphinx_internal_id: 1}, "@id_idx 1")
expect(ModelWithRt).to receive(:where).with(id: 1).and_return([record])
expect(transmitter).to receive(:transmit).with(ModelWithRt.sphinx_indexes.first, record)

transmitter.update_fields({field: 123}, sphinx_internal_id: 1, matching: "@id_idx 1")
end
end
end

context 'when no full reindex' do
Expand Down

0 comments on commit fed8962

Please sign in to comment.