Using Delta Indexes for Associations in Thinking Sphinx

Using Delta Indexes for associations in Thinking Sphinx

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 :)

Thinking sphinx: multiple delta indices on single model

  1. Yes, if you update field_a, both deltas will be updated. Thinking Sphinx does not try to figure out whether model changes are relevant to a specific index.

  2. a. It depends on how deltas are processed. If it's the default delta
    processor (:delta => true), then they're processed one after the
    other. You're using Resque, so the processing of each index could
    indeed overlap. That's fine, though.

    b. The delta flag is only set to false/zero when a full index happens via the rake ts:index task, and only set to true/one by delta processing. It remains set to true until the rake task is run - it's an indication of whether there are changes not present in core (rather than an indication that it just needs to be processed for the delta index).

    c. The core indices load all data when rake ts:index is run - there is no filter on the delta column.

Sorting by association count in Thinking Sphinx

Your index looks fine, but if you're using deltas (and I think that's the wisest approach here, to have the data up-to-date), then you want to fire deltas for the related channels when a subscription or content is created/edited/deleted. This is covered in the documentation (see the "Deltas and Associations" section), but you'd be looking at something like this in both Subscription and Content:

after_save :set_channel_delta_flag
after_destroy :set_channel_delta_flag

# ...

private

def set_channel_delta_flag
channel.update_attributes :delta => true
end

Given you're using Delayed Job, I'd recommend investigating ts-delayed-delta to ensure delta updates are happening out of your normal HTTP request flow. And I highly recommend not running a full index after every change - that has the potential of getting quite slow quite quickly (and adding to the server load unnecessarily).

What is the equivalent of 'index_delta' in Thinking Sphinx v3?

This is covered here: https://github.com/pat/thinking-sphinx/issues/780 - but the short answer is:

ThinkingSphinx::Deltas::IndexJob.new('product_delta').perform

If you want to queue it up in Delayed Job, though, then the following is what you're after:

Delayed::Job.enqueue(
ThinkingSphinx::Deltas::DelayedDelta::DeltaJob.new('product_delta')
)

thinking-sphinx filtering with belongs_to association

Ok, I've got the solution. The issue was related to joiner gem update. Updating rails to 4.1.0 and adding joiner gem to Gemfile solved my issue:

gem 'joiner', '~> 0.3.4'

Later, I've got following post related to association issue:

Rails 4.1 - thinking-sphinx association not working

How to index aggregate association fields with thinking sphinx

Because you're not referring to columns directly, you'll need to specify the type manually:

has 'COUNT(business_feedbacks.id)',      as: :feedback_count,     type: :integer
has 'AVG(business_feedbacks.recommend)', as: :feedback_recommend, type: :integer

… or if the recommend column is something other than an integer, you can change the average type accordingly.

Thinking Sphinx with Rails - Delta indexing seems to work fine for one model but not for the other

Alright I figured it out. The problem was with the attribute definition:

has user(:id)

When I removed it from the definition and ran a rebuild, everything seemed to work perfectly.

ThinkingSphinx: association field UPDATE does not reindex

Thinking Sphinx does not automatically detect your associations and how they relate to your indexed data whenever you make an update to your models, as that could potentially be very complex (and thus, slow).

You've linked to the exactly correct part of the documentation, though it's slightly out of date - you'll want to use after_commit instead. Whenever you have a DocRequest updating, you'll want to set the delta flags for all of the associated DocResponse objects:

class DocRequest < ActiveRecord::Base
# ...

after_commit :set_response_delta_flags

# ...

private

def set_response_delta_flags
doc_responses.each { |response|
response.update_attributes :delta => true
}
end

# ...
end

Updating each response will fire off the delta processing for the affected response objects, which will in turn ensure their Sphinx data is up-to-date.



Related Topics



Leave a reply



Submit