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

Unscope the search_import scope in-case I'm indexing some attrs, #1639

Open
wahabmangat opened this issue Jun 11, 2023 · 1 comment
Open

Comments

@wahabmangat
Copy link

First
Done
Is your feature request related to a problem? Please describe.
I have a search_import scope like

  scope :search_import, -> { includes(:apps) }

I don't want to eager_load the apps when I'm only indexing let's say the domain name (FYI: domain has_many apps). How to unscope it

here's my search_data method

def search_data
  {
   apps_name: apps.map(&:name)
  }.merge(only_name)
end
def only_name
  {
    name: name
  }
end

Describe the solution you'd like

Domain.unscope(:search_import).reindex(:only_name)
@jaypandya73
Copy link

jaypandya73 commented Aug 22, 2024

I faced similar situation and in my case, I am performing partial reindex on prices columns and for that It eager loads all the association which is defined in search_import scope and it fires unnecessary queries.
Since we really can't avoid it right now I find a way to do it.

Searchkick.indexer.queue(YourDbRecords.map do |r| 
    Searchkick::RecordData.new(YourModel.searchkick_index, r).update_data(partial_data_method_name)
  end
)

Reference: https://github.com/ankane/searchkick/blob/master/lib/searchkick/index.rb#L167
Note: This code will skip ActiveSupport::Notifications publish event.

@ankane - We can easily implement this by introducing new flag on configuration level or we can also introduce new method for this. And I am happy to raise PR for this.

This is valid case because I am performing partial data reindexing on lots of data and that too frequently and I can't use Searchkick bulk callback option since I am not updating records individually(it's happening in batches and happens on direct DB level).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants