toptal / chewy

High-level Elasticsearch Ruby framework based on the official elasticsearch-ruby client
MIT License
1.88k stars 366 forks source link

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

Open skcc321 opened 1 year ago

skcc321 commented 1 year ago

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: