Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart update with :update index with lazy attributes #11

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-5.2.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-6.0.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-6.1.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-7.0.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion ci/Gemfile.rails-7.1.lock
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
3 changes: 2 additions & 1 deletion lib/esse/active_record/callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions lib/esse/active_record/callbacks/indexing_on_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/esse/active_record/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/esse/active_record/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Esse
module ActiveRecord
VERSION = '0.3.5'
VERSION = '0.3.6'
end
end
29 changes: 27 additions & 2 deletions spec/esse/active_record/callbacks/indexing_on_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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