我为 ruby on rails 2.3.2 安装了 Sphinx 和 Thinking Sphinx。
当我无条件搜索时,搜索工作正常。现在,我想做的是按标签过滤,因此,当我使用 act_as_taggable_on 插件时,我的公告模型如下所示:
class Announcement < ActiveRecord::Base
acts_as_taggable_on :tags,:category
define_index do
indexes title, :as => :title, :sortable => true
indexes description, :as => :description, :sortable => true
indexes tags.name, :as => :tags
indexes category.name, :as => :category
has category(:id), :as => :category_ids
has tags(:id), :as => :tag_ids
end
出于某种原因,当我运行以下命令时,它只会带来一个公告,这与我的期望无关。我有很多公告,所以我期待很多结果。
Announcement.search params[:announcement][:search].to_s, :with => {:tag_ids => 1}, :page => params[:page], :per_page => 10
我想出了点问题,它没有正确搜索。
谁能告诉我发生了什么?
谢谢,
布赖恩
最佳答案
Thinking Sphinx 依赖于模型中的关联。在一般情况下,您只需要放置索引定义 below你的协会。
与 act_as_taggable_on 插件你在模型文件和你写的时候没有标签相关的关联
indexes tags.name, :as => :tags
TS 将其解释为:
CAST(`announcements`.`name` AS CHAR) AS `tags`
(在我的例子中,查看 development.sphinx.conf 中的 sql_query)。
我想您在模型公告中有属性名称,并且在重建索引时不会遇到错误。
但我们期望:
CAST(GROUP_CONCAT(DISTINCT IFNULL(`tags`.`name`, '0') SEPARATOR ' ') AS CHAR) AS `tags`
和:
LEFT OUTER JOIN `taggings` ON (`announcements`.`id` = `taggings`.`taggable_id`)
LEFT OUTER JOIN `tags` ON (`tags`.`id` = `taggings`.`tag_id`) AND taggings.taggable_type = 'Announcement'
要使事情正常工作,只需在重建索引之前在模型中添加与标签相关的关联:
class Announcement < ActiveRecord::Base
acts_as_taggable_on :tags,:category
has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag, :class_name => "ActsAsTaggableOn::Tagging",
:conditions => "taggings.taggable_type = 'Announcement'"
#for context-dependent tags:
has_many :category_tags, :through => :taggings, :source => :tag, :class_name => "ActsAsTaggableOn::Tag",
:conditions => "taggings.context = 'categories'"
在 定义索引方法:
indexes category_tags(:name), :as => :tags
has category_tags(:id), :as => :tag_ids, :facet => true
在 Controller 中:
@announcement_facets = Announcement.facets params[:search], :with => {:tag_ids => [...]}
@announcements = @announcement_facets.for.paginate( :page => params[:page], :per_page => 10 )
关于ruby-on-rails - Thinking Sphinx 和acts_as_taggable_on 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2082399/