missing quotes in thinking sphinx scoped search

114 views Asked by At

I'm wanting a sphinx_scope that will only search for records that are current. Each database record has a field, status, whose value is either CURRENT or ARCHIVED.

I have achieved this, but I have to use an odd construct to get there; there is probably a much better way to do it.

Here's what I have:

indices/letter_index.rb

ThinkingSphinx::Index.define :letter, :with => :real_time do
  # fields
  indexes title, :sortable => true
  indexes content

  # attributes
  has status, :type => :string
  has issue_date, :type => :timestamp
  has created_at, :type => :timestamp
  has updated_at, :type => :timestamp
end

models/letter.rb

class Letter < ActiveRecord::Base
  include ThinkingSphinx::Scopes
  after_save ThinkingSphinx::RealTime.callback_for(:letter)

  .. snip ..

  sphinx_scope(:archived) {
    {:with => {:status => "'ARCHIVED'"}}
  }

The problem that I ran into was that if I used :with => {:status => 'ARCHIVED'}, my query came out as

SELECT * FROM `letter_core` WHERE MATCH('search term') AND `status` = ARCHIVED AND `sphinx_deleted` = 0 LIMIT 0, 20
ThinkingSphinx::SyntaxError: sphinxql: syntax error, unexpected IDENT, expecting CONST_INT (or 4 other tokens) near 'ARCHIVED AND `sphinx_deleted` = 0 LIMIT 0, 20; SHOW META'

but, if I construct it as :with => {:status => "'ARCHIVED'"}, it then adds the single quotes and the query succeeds. :)

Is this the proper way to write the scope, or is there a better way?

Bonus question: where do I find the docs for what is allowed in the scopes, such as :order, :with, :conditions, etc.

1

There are 1 answers

3
pat On BEST ANSWER

Firstly: the need for quotes was a bug - Sphinx has only relatively recently allowed for filtering on string attributes, hence why this wasn't something in place a good while ago. I've patched Riddle (the gem that is a pure Ruby wrapper around Sphinx functionality and used by Thinking Sphinx), you can give the latest a spin with this in your Gemfile:

gem 'riddle', '~> 2.0',
  :git    => 'git://github.com/pat/riddle.git',
  :branch => 'develop',
  :ref    => '8aec79fdf4'

As for what can go in a Sphinx scope: anything that can go in a normal search call.