在 Thinking Sphinx 中使用 Delta 索引进行关联 [英] Using Delta Indexes for associations in Thinking Sphinx

查看:43
本文介绍了在 Thinking Sphinx 中使用 Delta 索引进行关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产品型号:

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?

根据本文档:http://freelancing-god.github.com/ts/en/deltas.html,应该向产品发送save消息,所以在这种情况下,我应该去寻找与子类别相关的每个产品并发送保存消息,类似这样:

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

我认为这太过分了,因为我们每个子类别都有 100.000 种产品.这是更新增量索引的正确方法吗?我错过了什么吗?

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?

添加后:

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

我总是收到此错误:

NoMethodError(# 的未定义方法 `index_delta'):

NoMethodError (undefined method `index_delta' for #):

因此,解决此问题的方法是将消息 *define_indexes* 发送到 Product 模型.

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

解决此问题后,一切正常,但 delta_index 未正确更新,我需要对子类别模型进行两次保存.

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

使用 after_commitdefine_indexes 是正确的解决方案吗?这是我找到的唯一一个.

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

推荐答案

尝试以下方法:

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

单个 SQL 语句,单个增量重新索引.应该表现得更好:)

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

这篇关于在 Thinking Sphinx 中使用 Delta 索引进行关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆