From 65b2e145703c8e14fcfbce12036a652ae39c0acd Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Wed, 31 Jul 2024 19:10:10 -0300 Subject: [PATCH 1/4] feat: after update smart detects to use :update when lazy are present The document itself may not include the lazy attributes, and by performing :index request all the updated done through the lazy update attribute actions will be lost. So updated the logic to attempt push document using :update api with a :index fallback in case doc does not exist yet. This behaviour can be overriten by passing with: :index --- lib/esse/active_record/callbacks.rb | 3 +- .../callbacks/indexing_on_update.rb | 9 +----- .../callbacks/indexing_on_update_spec.rb | 29 +++++++++++++++++-- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/lib/esse/active_record/callbacks.rb b/lib/esse/active_record/callbacks.rb index e5f3873..70b1c9a 100644 --- a/lib/esse/active_record/callbacks.rb +++ b/lib/esse/active_record/callbacks.rb @@ -5,8 +5,9 @@ module ActiveRecord class Callback attr_reader :repo, :options, :block_result - def initialize(repo:, block_result: nil, **kwargs) + def initialize(repo:, block_result: nil, with: nil, **kwargs) @repo = repo + @with = with @options = kwargs @block_result = block_result end diff --git a/lib/esse/active_record/callbacks/indexing_on_update.rb b/lib/esse/active_record/callbacks/indexing_on_update.rb index 0901b38..5593d87 100644 --- a/lib/esse/active_record/callbacks/indexing_on_update.rb +++ b/lib/esse/active_record/callbacks/indexing_on_update.rb @@ -3,13 +3,6 @@ module Esse::ActiveRecord module Callbacks class IndexingOnUpdate < Callback - attr_reader :update_with - - def initialize(with: :index, **kwargs, &block) - @update_with = with - super(**kwargs, &block) - end - def call(model) record = block_result || model @@ -43,7 +36,7 @@ def call(model) def update_document(document) return if document.ignore_on_index? - if update_with == :update + if @with == :update || (@with.nil? && repo.lazy_document_attributes.any?) begin repo.index.update(document, **options) rescue Esse::Transport::NotFoundError diff --git a/spec/esse/active_record/callbacks/indexing_on_update_spec.rb b/spec/esse/active_record/callbacks/indexing_on_update_spec.rb index 44619d3..8ad7bd2 100644 --- a/spec/esse/active_record/callbacks/indexing_on_update_spec.rb +++ b/spec/esse/active_record/callbacks/indexing_on_update_spec.rb @@ -6,9 +6,9 @@ describe '.initialize' do let(:repo) { instance_double(Esse::Repository) } - it 'sets update_with' do + it 'sets @with' do callback = described_class.new(repo: repo, with: :update) - expect(callback.update_with).to eq(:update) + expect(callback.instance_variable_get(:@with)).to eq(:update) end it 'sets options' do @@ -96,5 +96,30 @@ state.update!(name: 'IL') end end + + context 'when not calling with in a repository with lazy attributes' do + before do + StatesIndex::State.lazy_document_attribute :total_counties do |docs| + ::County.where(state_id: docs.map(&:id)).group(:state_id).count + end + end + + after do + StatesIndex::State.instance_variable_set(:@lazy_document_attributes, {}.freeze) + end + + it { expect(StatesIndex::State.lazy_document_attributes).not_to be_empty } + + it 'updates the record using :update action to avoid losing lazy attributes' do + expect(StatesIndex).to receive(:update).and_call_original + expect(StatesIndex).to esse_receive_request(:update).with( + id: state.id, + index: StatesIndex.index_name, + body: {doc: { name: 'IL' } }, + ).and_return(ok_response) + + state.update!(name: 'IL') + end + end end end From bd5f9982537aca363ec1e2ab4fe353ffd4d09f9b Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Wed, 31 Jul 2024 19:15:20 -0300 Subject: [PATCH 2/4] feat: remove group from count query --- lib/esse/active_record/collection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/esse/active_record/collection.rb b/lib/esse/active_record/collection.rb index a7c7852..9c027b7 100644 --- a/lib/esse/active_record/collection.rb +++ b/lib/esse/active_record/collection.rb @@ -90,7 +90,7 @@ def each_batch_ids end def count - dataset.except(:includes, :preload, :eager_load, :order, :limit, :offset).count + dataset.except(:includes, :preload, :eager_load, :group, :order, :limit, :offset).count end alias_method :size, :count From d54e3f4afb67791207ec42ae74776ff3c2443505 Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Fri, 2 Aug 2024 09:46:12 -0300 Subject: [PATCH 3/4] feat: add note about async indexing callbacks --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index 4c4b835..f97a9d3 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,35 @@ User.without_indexing(AccountsIndex) do end ``` +### Asynchronous Indexing + +If you are using a background job processor like Sidekiq or Faktory, you may be interested in indexing documents asynchronously. For this, you can use the [esse-async_indexing](https://github.com/marcosgz/esse-async_indexing) gem. + +Add the `esse-async_indexing` gem to your Gemfile and require the `esse/async_indexing/active_record` file in your application initialization. Make sure to setup the gem configurationg according to the [esse-async_indexing documentation](https://github.com/marcosgz/esse-async_indexing). + + +```ruby +require 'esse/async_indexing/active_record' +``` + +Then, you can use the `async_index_callback` or `async_update_lazy_attribute_callback` methods to push the indexing job to the background job processor. + +```diff +class City < ApplicationRecord + include Esse::ActiveRecord::Model +- include Esse::ActiveRecord::Model ++ include Esse::AsyncIndexing::ActiveRecord::Model + + belongs_to :state, optional: true + + + async_indexing_callback('geos_index:city') { id } +- index_callback('geos_index:city') { id } +- update_lazy_attribute_callback('geos_index:state', 'cities_count', if: :state_id?) { state_id } ++ async_index_callback('geos_index:city', service_name: :sidekiq) { id } ++ async_update_lazy_attribute_callback('geos_index:state', 'cities_count', if: :state_id?, service_name: :sidekiq) { state_id } +end +``` ## Development From 67522dd01a44808fb3d55c547c144b0bb0e7d82e Mon Sep 17 00:00:00 2001 From: "Marcos G. Zimmermann" Date: Fri, 2 Aug 2024 09:50:41 -0300 Subject: [PATCH 4/4] bump version --- Gemfile.lock | 2 +- ci/Gemfile.rails-5.2.lock | 2 +- ci/Gemfile.rails-6.0.lock | 2 +- ci/Gemfile.rails-6.1.lock | 2 +- ci/Gemfile.rails-7.0.lock | 2 +- ci/Gemfile.rails-7.1.lock | 2 +- lib/esse/active_record/version.rb | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 181a27e..9fdb2e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - esse-active_record (0.3.5) + esse-active_record (0.3.6) activerecord (>= 4.2, < 8) esse (>= 0.3.0) diff --git a/ci/Gemfile.rails-5.2.lock b/ci/Gemfile.rails-5.2.lock index ed904df..7c8e3b2 100644 --- a/ci/Gemfile.rails-5.2.lock +++ b/ci/Gemfile.rails-5.2.lock @@ -1,7 +1,7 @@ PATH remote: .. specs: - esse-active_record (0.3.5) + esse-active_record (0.3.6) activerecord (>= 4.2, < 8) esse (>= 0.3.0) diff --git a/ci/Gemfile.rails-6.0.lock b/ci/Gemfile.rails-6.0.lock index 474f759..2934703 100644 --- a/ci/Gemfile.rails-6.0.lock +++ b/ci/Gemfile.rails-6.0.lock @@ -1,7 +1,7 @@ PATH remote: .. specs: - esse-active_record (0.3.5) + esse-active_record (0.3.6) activerecord (>= 4.2, < 8) esse (>= 0.3.0) diff --git a/ci/Gemfile.rails-6.1.lock b/ci/Gemfile.rails-6.1.lock index 05e889b..770b451 100644 --- a/ci/Gemfile.rails-6.1.lock +++ b/ci/Gemfile.rails-6.1.lock @@ -1,7 +1,7 @@ PATH remote: .. specs: - esse-active_record (0.3.5) + esse-active_record (0.3.6) activerecord (>= 4.2, < 8) esse (>= 0.3.0) diff --git a/ci/Gemfile.rails-7.0.lock b/ci/Gemfile.rails-7.0.lock index 474f759..2934703 100644 --- a/ci/Gemfile.rails-7.0.lock +++ b/ci/Gemfile.rails-7.0.lock @@ -1,7 +1,7 @@ PATH remote: .. specs: - esse-active_record (0.3.5) + esse-active_record (0.3.6) activerecord (>= 4.2, < 8) esse (>= 0.3.0) diff --git a/ci/Gemfile.rails-7.1.lock b/ci/Gemfile.rails-7.1.lock index a6d6f56..2b8fd78 100644 --- a/ci/Gemfile.rails-7.1.lock +++ b/ci/Gemfile.rails-7.1.lock @@ -1,7 +1,7 @@ PATH remote: .. specs: - esse-active_record (0.3.5) + esse-active_record (0.3.6) activerecord (>= 4.2, < 8) esse (>= 0.3.0) diff --git a/lib/esse/active_record/version.rb b/lib/esse/active_record/version.rb index c4f0160..6175be2 100644 --- a/lib/esse/active_record/version.rb +++ b/lib/esse/active_record/version.rb @@ -2,6 +2,6 @@ module Esse module ActiveRecord - VERSION = '0.3.5' + VERSION = '0.3.6' end end