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

Clarify use of ActiveRecord where in search_import #1667

Open
JasonBarnabe opened this issue Jan 20, 2024 · 0 comments
Open

Clarify use of ActiveRecord where in search_import #1667

JasonBarnabe opened this issue Jan 20, 2024 · 0 comments

Comments

@JasonBarnabe
Copy link

The readme explains search_import as such:

To eager load associations, use the search_import scope.
(...)
By default, all records are indexed. To control which records are indexed, use the should_index? method together with the search_import scope.

Now an obvious extrapolation here would be to add where to search_import to eliminate records that should not be indexed. This is a suggestion also present in some issues like #1503.

I've tested this out and it does work to eliminate records, but there is one major caveat - if a record is already indexed, then updated such that the where in search_import no longer matches it, the record remains in the index.

For example:

class User
  searchkick
  scope :search_import, -> { where.not(name: 'Jason') }

  def should_index?
    name != 'Jason'
  end
end

user = User.create(name: 'Fred')
# (User gets indexed)

user.update(name: 'Jason')
# (User remains in index!)

Removing search_import in the above example fixes the issue.

I'm not sure if this is intended functionality, but if it is, this should be documented - for example by saying that where should only added to search_import if the record will never be updated so that it shouldn't be indexed.

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

1 participant