chewy icon indicating copy to clipboard operation
chewy copied to clipboard

Redundant crutch call when :update_fields does not contain related field

Open skcc321 opened this issue 3 years ago • 0 comments

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

skcc321 avatar Nov 04 '22 17:11 skcc321