Question

I am trying to create a TS scope as follows:

include ThinkingSphinx::Scopes
sphinx_scope(:status_approved) {
      {:conditions => {:status => "approved"}}
    }
default_sphinx_scope :status_approved 

My indice file is:

indexes name, status
has user_id, created_at

Two questions:

  1. Does field status needs to be defined as an index in order for the conditions string filter to work? Seems like I get a no field in schema query error if I do not do that.
  2. If it is required to define as part of the indexes, then whenever someone updated the status field, it will not show in the results until the next reindex occurs. Is this the only way to use a string filtering TS scope? Or is there a better way to do this?

I'm using Rails 3.2.16 and TS 3.0.6

Was it helpful?

Solution

  1. Sphinx scopes are for your Sphinx searches, so yes, you'll need status as a field in your index definition if you're using it in a Sphinx scope.
  2. If you want your Sphinx index data up-to-date as quickly as possible, you may want to investigate using delta indices (perhaps using Delayed Job or Sidekiq, to remove it from your web processes). Or, you could use Sphinx's real-time indices instead.

OTHER TIPS

To answer my own questions after further research:

  1. I have search through the documentation but have not found any specific reference to this. I may have missed it. However by trial and errors , I found that if I want to use the string filter method, I have to add the field to the indexes. If not, it will complain that the field is not found in the schema.
  2. There is a 'new' feature introduced to TS called Realtime Indices which apparently addresses this issue. This was mentioned in the author's blog http://freelancing-gods.com/ I have not tried it. In any case, I have taken a different route which did away using the default scope and use the filtering in the indices instead. In my index file, i now have:

    indexes name, status
    has user_id, created_at
    where "status = 'approved'"
    

And I no longer need to define the default scope in my model as such. This will still require periodic reindexing in any case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top