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

Redundant crutch call when :update_fields does not contain related field #864

Open
skcc321 opened this issue Nov 4, 2022 · 0 comments
Open

Comments

@skcc321
Copy link
Contributor

skcc321 commented Nov 4, 2022

I have an index definition with some crutches. The reindex operation always triggers a crutch execution even if I don't want to reindex that one by skipping the related field using :update_fields. Looks like the reason is this line of code https://github.com/toptal/chewy/blob/master/lib/chewy/index/import/bulk_builder.rb#L52 where we don't pass @fields option.


Expected behavior

crutch execution should respect :update_fields option.

Actual behavior

crutch is executed regardless :update_fields option

Steps to reproduce the problem

class UsersIndex < Chewy::Index
  index_scope User....
  
  crutch :driver_role do |collection|
      ::User.includes(employees: :group)
        .where(id: collection.map(&:id))
        .group(:id, "groups.kind", "groups.role")
        .pluck(
          :id,
          Arel.sql(
            "jsonb_build_object(
            'driver_role', CASE
                             WHEN (groups.kind = 'carrier' AND groups.role ='ca_driver') THEN TRUE
                             ELSE FALSE
                           END
            )"
          )
        ).to_h
  end
  
  field :first_name
  field :last_name
  field :driver_role, type: :boolean, value: ->(user, crutch) { crutch.driver_role.dig(user.id, "driver_role") }
  ...
end

call from the console

UsersIndex.reset [1], update_fields: [:first_name, :last_name]

look at logs and confirm that drivers crutch SQL call has been made

Version Information

Share here essential version information such as:

  • Chewy version
    7.2.4
  • Elasticsearch version
    7.x
  • Ruby version
    3.1.2
  • Rails version
    6.1.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant