【发布时间】:2017-01-04 04:54:22
【问题描述】:
我想要一个只会搜索当前记录的 sphinx_scope。每个数据库记录都有一个字段status,其值为CURRENT 或ARCHIVED。
我已经做到了,但我必须使用一个奇怪的结构才能到达那里;可能有更好的方法。
这是我所拥有的:
索引/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'"}}
}
我遇到的问题是,如果我使用:with => {:status => 'ARCHIVED'},我的查询结果是
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'
但是,如果我将其构造为:with => {:status => "'ARCHIVED'"},那么它会添加单引号并且查询成功。 :)
这是编写作用域的正确方法,还是有更好的方法?
额外问题:我在哪里可以找到范围内允许的文档,例如 :order、:with、:conditions 等。
【问题讨论】:
标签: thinking-sphinx