Skip to content

Commit

Permalink
Smart update with :update index with lazy attributes (#11)
Browse files Browse the repository at this point in the history
* 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

* feat: remove group from count query

* feat: add note about async indexing callbacks

* bump version
  • Loading branch information
marcosgz authored Aug 2, 2024
1 parent feea0e4 commit 6646e96
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 7 deletions.
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
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

0 comments on commit 6646e96

Please sign in to comment.