rails thinking sphinx,How do i set delta to true after reindexing? - ruby-on-rails

When I run a reindexing task(rake ts:reindex), it automatically sets delta value to false.But I definitely want delta indexing working after reindexing. So I want to set the delta value back to 'true'. How can I do that??

You don't need delta indexing after your reindex as the main index will be up to date and complete. Your model should only set the delta flag to true after your next update, which is when your main index will be incomplete.

Thinking Sphinx will automatically set delta to true when you make changes to a model instance.
The only cases where this is not the case is when you're actually changing an association instance, instead of the indexed model, or you're changing the indexed model in some way which doesn't fire the callbacks. #update_attribute (note: singular) does not fire callbacks. #save and #update_attributes do.
So: how are you changing your model instances? Is delta indexing not occurring when you make those changes?

Related

Why need to run delta indexing after new records and change in records

As per my search and understanding in delta indexing when we add new records or do changes in records we need to re index sphinx to show that data otherwise it will not show.
But I check that data is updating without re indexing. So what the purpose of re indexing delta
With Thinking Sphinx, there's the distinction between a full re-index where all the indices are reprocessed (via rake ts:index and rake Ts:rebuild), and processing a single index.
When you have delta indexing enabled, it means that the delta index for a given model is automatically processed straight after the change to a record, or adding a new record. This is either done as part of the standard callback process (when using :delta => true) or via a background worker (Sidekiq, DelayedJob, etc) if you're using the appropriate delta gem for those.
All of this means that you don't need to run a full reprocessing of all indices for the change to be present - the delta index is reprocessed automatically, and the record's changes are reflected in Sphinx.
One catch worth noting is that the more changes that happen, the larger the delta index gets, and thus the slower it is to process. So, a full re-index is still required on a regular basis (hourly? daily? depends on your app) to keep delta processing times fast.

Easiest way to increment a field in the DB?

Is there an easier way to increment a field in my database with Rails, than doing this:
#user.update_attribute(:last_seen_at, #user.last_seen_at+1)
You can use increment:
Initializes attribute to zero if nil and adds the value passed as by (default is 1). The increment is performed directly on the underlying attribute, no setter is invoked. Only makes sense for number-based attributes. Returns self.
Example:
#user.increment(:last_seen_at)
Yep! You can pretend it's a counter cache and use:
User.increment_counter(:last_seen_at, #user.id)
http://apidock.com/rails/ActiveRecord/CounterCache/increment_counter
Edit: I should mention that this won't work if you are relying on the behavior of dates or times (i.e. Date.today + 1). In that case, I think your best bet is to just do the update_attribute thing.

TS delayed delta - check for finished job

Is there a way to check whether a TS delayed delta job has finished? I have a scenario in which I need to run a new search in an after_save callback and of course I'd like to see the changes to the delta index reflected in the search results.
Here are some details of my example:
I have a model called Feature which has many annotations (Annotation model). The index looks like this:
define_index do
indexes annotations.value, :as => :annotations
# other indexes
set_property :delta => :delayed
end
When the "value" of an annotation changes I update the delta attribute of the associated feature in an Annotation model callback. Setting the delta attribute to true spins off a delayed_job task to update the delta index. In a separate callback I'd like to perform a new search against the updated delta index, but I noticed that the search never reflects the current state of the index. This is no doubt because the delta jobs are not finished yet.
What would be the best strategy to deal with this these timing issues?
I can think of only one. to query the delayed_jobs table
ActiveRecord::Base.connection.execute("select count(1) from delayed_jobs where handler like '%%'")
If the job has succeeded, the entry is sure to have gone. This is the only way i can think of. Or disable delayed delta for this model alone if its not a big deal.

Thinking sphinx updates delta index even when no fields are updated

Even when no fields specified in update attributes actually changes , thinking sphinx sets delta = 1 which results in large number of unwanted queries being fired . can we do something to let sphinx know that there was no actual update.

Thinking Sphinx delta indexing - delta index not getting updated

I have delta indexing setup for thinking sphinx on one of my models. When ever a record gets updated, the delta is being set to True, but I don't see the index getting updated with the changes made to the record. I have my sphinx configuration files updated with the delta property changes. Any idea why the delta indexing is not getting triggered?
According to the documentation after you update the database and the model, you should do this:
rake thinking_sphinx:rebuild
Maybe you've omit that step..

Resources