Commit 1852f9abf74aefe6b818520d9b15237d41b435e5
1 parent
c8b626a5
Exists in
master
and in
1 other branch
Added update option to example [skip ci]
Showing
1 changed file
with
24 additions
and
19 deletions
Show diff stats
README.md
... | ... | @@ -720,7 +720,9 @@ Then, create a job to update the conversions column and reindex records with new |
720 | 720 | |
721 | 721 | ```ruby |
722 | 722 | class UpdateConversionsJob < ApplicationJob |
723 | - def perform(class_name, since: nil, reindex: true) | |
723 | + def perform(class_name, since: nil, update: true, reindex: true) | |
724 | + model = Searchkick.load_model(class_name) | |
725 | + | |
724 | 726 | # get records that have a recent conversion |
725 | 727 | recently_converted_ids = |
726 | 728 | Searchjoy::Conversion.where(convertable_type: class_name).where(created_at: since..) |
... | ... | @@ -728,28 +730,31 @@ class UpdateConversionsJob < ApplicationJob |
728 | 730 | |
729 | 731 | # split into batches |
730 | 732 | recently_converted_ids.in_groups_of(1000, false) do |ids| |
731 | - # fetch conversions | |
732 | - conversions = | |
733 | - Searchjoy::Conversion.where(convertable_id: ids, convertable_type: class_name) | |
734 | - .joins(:search).where.not(searchjoy_searches: {user_id: nil}) | |
735 | - .group(:convertable_id, :query).distinct.count(:user_id) | |
736 | - | |
737 | - # group by record | |
738 | - conversions_by_record = {} | |
739 | - conversions.each do |(id, query), count| | |
740 | - (conversions_by_record[id] ||= {})[query] = count | |
741 | - end | |
733 | + if update | |
734 | + # fetch conversions | |
735 | + conversions = | |
736 | + Searchjoy::Conversion.where(convertable_id: ids, convertable_type: class_name) | |
737 | + .joins(:search).where.not(searchjoy_searches: {user_id: nil}) | |
738 | + .group(:convertable_id, :query).distinct.count(:user_id) | |
739 | + | |
740 | + # group by record | |
741 | + conversions_by_record = {} | |
742 | + conversions.each do |(id, query), count| | |
743 | + (conversions_by_record[id] ||= {})[query] = count | |
744 | + end | |
742 | 745 | |
743 | - # update conversions column | |
744 | - model = Searchkick.load_model(class_name) | |
745 | - model.transaction do | |
746 | - conversions_by_record.each do |id, conversions| | |
747 | - model.where(id: id).update_all(search_conversions: conversions) | |
746 | + # update conversions column | |
747 | + model.transaction do | |
748 | + conversions_by_record.each do |id, conversions| | |
749 | + model.where(id: id).update_all(search_conversions: conversions) | |
750 | + end | |
748 | 751 | end |
749 | 752 | end |
750 | 753 | |
751 | - # reindex conversions data | |
752 | - model.where(id: ids).reindex(:conversions_data) if reindex | |
754 | + if reindex | |
755 | + # reindex conversions data | |
756 | + model.where(id: ids).reindex(:conversions_data) | |
757 | + end | |
753 | 758 | end |
754 | 759 | end |
755 | 760 | end | ... | ... |