Using Delta Indexes for associations in Thinking Sphinx

╄→尐↘猪︶ㄣ 提交于 2019-12-09 01:38:29

问题


I have a Product model:

class Product < ActiveRecord::Base
    belongs_to :subcategory

    define_index do

        # fields
        indexes subcategory.name, :as => :subcategory, :sortable => true, :facet => true

        # attributes
        has subcategory_id, created_at, updated_at

        #properties
        set_property :delta => true

Now, suppose that a user updates a subcategory name, which is the proper way to update the products delta index?

According to this documentation: http://freelancing-god.github.com/ts/en/deltas.html, a save message should be sent to the product, so in this case I should go for each product related with the subcategory and send the save message, something like this:

class Subcategory < ActiveRecord::Base
    has_many :products

    after_save :set_product_delta_flag

    private

    def set_product_delta_flag
        products.each { |product|
        product.delta = true
        product.save
     }
    end
  end

I think that this is overkilling because we have like 100.000 products per subcategory. Is this the correct way to update the delta index? Am I missing something?

After adding this:

def set_product_delta_flag
    Product.update_all ['delta = ?', true], ['subcategory_id = ?', id]
    Product.index_delta
end

I'm always receiving this error:

NoMethodError (undefined method `index_delta' for #):

So, the solution to this problem was to send the message *define_indexes* to the Product model.

After fixing this issue, everything was ok, but the delta_index was not correctly updated, I needed to do save twice to the subcategory model.

So my final solution is this one:

after_commit :set_product_delta_flag

private

def set_product_delta_flag
    Product.define_indexes
    Product.update_all ['delta = ?', true], ['subcategory_id = ?', id]
    Product.index_delta
end

Using after_commit and define_indexes is the correct solution? Its the only one that I've found.


回答1:


Try the following instead:

def set_product_delta_flag
  Product.update_all ['delta = ?', true], ['subcategory_id = ?', id]
  Product.index_delta
end

A single SQL statement, a single delta re-indexing. Should perform far better :)



来源:https://stackoverflow.com/questions/4902804/using-delta-indexes-for-associations-in-thinking-sphinx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!