我已经设置了一些索引,一切似乎工作得很好,直到我想尝试得到排序的结果。我有几个模型,但我在其中定义索引的是"Doctor"。我这样定义它们:
class SearchController < ApplicationController
define_index do
indexes :firstname, :sortable => true
indexes :lastname, :sortable => true
indexes :sex, :sortable => true
indexes practice(:name), :as => :practice_name, :sortable => true
indexes practice(:address), :as => :practice_address, :sortable => true
# Added after denormalization
indexes county(:name), :as => :county
indexes municipality(:name), :as => :municipality
# Can only filter on integers, thus CRC32-ing
has "CRC32(sex)", :as => :sex_filter, :type => :integer
end
end
搜索似乎工作得很好。当尝试对任何属性进行排序时,我得到如下错误
索引employee_core:排序属性'firstname'未找到
表示姓、名和性别。我可以通过添加
来部分"解决"这个问题。有名字,姓氏,性别
的define_index
块,但我不确定如何做同样的引用到另一个模型(实践),或者如果它甚至是可能的。
有人知道我可能做错了什么,首先使:sortable => true
不工作,或者我如何定义对实践的引用作为属性?
我打电话给Thinking Sphinx的一个例子,它在上面提到的方式中失败了:
ThinkingSphinx.search "<some search string>", {:with=>{}, :order=>:firstname, :sort_mode=>:desc, :page=>1}
所以,这里的问题是Sphinx字段根本无法排序。Thinking Sphinx通过添加:sortable
选项来解决这个问题—它添加了一个具有相同名称的属性加上一个_sort后缀—因此,在本例中为firstname_sort
。
当您在单个模型(Employee.search
)上进行搜索时,Thinking Sphinx能够引用该模型并查看哪些字段是可排序的,并自动将firstname切换为firstname_sort。但是,您是在全局搜索,因此没有TS的简单引用。
ThinkingSphinx.search "foo", :order => :firstname_sort, :sort_mode => :desc
还有:如果你在所有索引模型中搜索,确保过滤器和排序中使用的属性存在于所有索引定义中。
在关联字段中添加:sortable也可以很好地工作,给出相同的警告。