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,7 +720,9 @@ Then, create a job to update the conversions column and reindex records with new | ||
720 | 720 | ||
721 | ```ruby | 721 | ```ruby |
722 | class UpdateConversionsJob < ApplicationJob | 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 | # get records that have a recent conversion | 726 | # get records that have a recent conversion |
725 | recently_converted_ids = | 727 | recently_converted_ids = |
726 | Searchjoy::Conversion.where(convertable_type: class_name).where(created_at: since..) | 728 | Searchjoy::Conversion.where(convertable_type: class_name).where(created_at: since..) |
@@ -728,28 +730,31 @@ class UpdateConversionsJob < ApplicationJob | @@ -728,28 +730,31 @@ class UpdateConversionsJob < ApplicationJob | ||
728 | 730 | ||
729 | # split into batches | 731 | # split into batches |
730 | recently_converted_ids.in_groups_of(1000, false) do |ids| | 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 | end | 751 | end |
749 | end | 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 | end | 758 | end |
754 | end | 759 | end |
755 | end | 760 | end |