排序 ThinkingSphinx. 搜索结果
有人能够对应用程序范围内的 ThinkingSphinx 查询的搜索结果进行排序吗?
我有以下设置:
class Resource < ActiveRecord::Base
# ...
define_index do
indexes :title, :as => :sortable_name, :sortable => true
indexes :tease
indexes :description
indexes authors(:name), :as => :author, :facet => true
end
end
我
class Author < ActiveRecord::Base
# ...
define_index do
indexes :name, :as => :sortable_name, :sortable => true
indexes :title
indexes :bio
end
end
希望能够搜索这些模型(以及其他 2 个模型),并按字母顺序对结果进行排序。我需要排序的字段可以有不同的名称,因此我使用 :as => 为它们建立索引:sortable_name(欢迎提出更好的建议)。
以下所有内容都像魅力一样工作:
Author.search 'something', :order => :sortable_name
Resource.search 'something', :order => :sortable_name
ThinkingSphinx.search 'something'
但是当我尝试时,
ThinkingSphinx.search 'something', :order => :sortable_name
我得到了这个:
ThinkingSphinx::SphinxError: index author_core,resource_core: sort-by attribute 'sortable_name' not found
使用 :classes => 显式地将搜索限制为这些类[作者,资源]没有帮助。显然,我不理解 Sphinx 在这里的工作方式......
我已经重建了索引并停止/启动了 searchd 进程,所有这些都得到了相同的结果。
Rails 3.0.5 和 ThinkingSphinx 2.0.2
有什么建议吗?
Has anyone been able to sort search results from an application-wide ThinkingSphinx query?
I have the following setup:
class Resource < ActiveRecord::Base
# ...
define_index do
indexes :title, :as => :sortable_name, :sortable => true
indexes :tease
indexes :description
indexes authors(:name), :as => :author, :facet => true
end
end
and
class Author < ActiveRecord::Base
# ...
define_index do
indexes :name, :as => :sortable_name, :sortable => true
indexes :title
indexes :bio
end
end
I want to be able to search across these models (as well as 2 others), and sort the results alphabetically. The fields I need to sort by can have different names, so I index them using :as => :sortable_name (suggestions for doing this a better way are welcome).
The following all work like a charm:
Author.search 'something', :order => :sortable_name
Resource.search 'something', :order => :sortable_name
ThinkingSphinx.search 'something'
But when I try
ThinkingSphinx.search 'something', :order => :sortable_name
I get this:
ThinkingSphinx::SphinxError: index author_core,resource_core: sort-by attribute 'sortable_name' not found
Explicitly limiting the search to those classes using :classes => [Author, Resource] doesn't help. There's clearly something I'm not understanding about the way that Sphinx works here...
I've rebuilt my index and stopped/started the searchd process, all with the same result.
Rails 3.0.5 and ThinkingSphinx 2.0.2
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的作者和资源搜索示例中,Thinking Sphinx 有一个模型引用,因此可以查看该模型以查找其字段,并将 sortable_name 转换为 sortable_name_sort (实际用于排序的属性)。
当您搜索多个模型时,Thinking Sphinx 不会深入研究每个模型 - 因此您需要更加耐心地告诉它要做什么。请尝试以下操作:
In your Author and Resource search examples, Thinking Sphinx has a single model reference, and so can look at that model to find its fields, and translate sortable_name to sortable_name_sort (the attribute which is actually used for sorting).
When you're searching across multiple models, Thinking Sphinx doesn't delve into each model - so you need to be a little more patient with telling it what to do. Try the following: