思维狮身人面像和act_as_taggable_on插件 [英] Thinking Sphinx and acts_as_taggable_on plugin

查看:136
本文介绍了思维狮身人面像和act_as_taggable_on插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在导轨2.3.2上安装了Sphinx和Think Sphinx用于红宝石.

I installed Sphinx and Thinking Sphinx for ruby on rails 2.3.2.

当我无条件搜索时,搜索正常.现在,我想做的是按标签过滤,因此,当我使用acts_as_taggable_on插件时,我的公告"模型如下所示:

When I search without conditions search works ok. Now, what I'd like to do is filter by tags, so, as I'm using the acts_as_taggable_on plugin, my Announcement model looks like this:

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

由于某种原因,当我运行以下命令时,它只会带来一个公告,与我的预期无关.我有很多公告,所以我期望有很多结果.

For some reason, when I run the following command, it will bring just one announcement, that has nothing to do with what I expect. I've got many announcements, so I expected a lot of results instead.

Announcement.search params[:announcement][:search].to_s, :with => {:tag_ids => 1}, :page => params[:page], :per_page => 10

我猜错了,而且搜索不正确.

I guess something is wrong, and it's not searching correctly.

任何人都可以告诉我发生了什么事吗?

Can anyone give my a clue of what's going on?

谢谢, 布莱恩

推荐答案

思维Sphinx依赖于模型中的关联.在常见情况下,您只需将索引定义放在关联的下方.

Thinking Sphinx relies on associations in model. In common situations you only have to put index definition below your associations.

使用 acts_as_taggable_on 插件,您在模型文件中以及编写时没有与标签相关的关联

With acts_as_taggable_on plug-in you don't have tag-related associations in model file and when you write

为tags.name索引,:as =>:tags

indexes tags.name, :as => :tags

TS将其解释为:

CAST(`announcements`.`name` AS CHAR) AS `tags`

(以我为例,请查看development.sphinx.conf中的sql_query). 我想您在模型公告中具有属性名称,并且在重建索引时不会出错.

(look at sql_query in development.sphinx.conf, in my case). I suppose that you have attribute name in model Announcement and don't run into error when rebuild index.

但是我们期望:

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'

要使一切正常,只需在模型中添加与标签相关的关联,然后再重建索引即可:

To get things working just add tag-related associations in your model before you rebuild index:

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'"

define_index 方法中:

indexes category_tags(:name), :as => :tags
has category_tags(:id), :as => :tag_ids, :facet => true

在控制器中:

@announcement_facets = Announcement.facets params[:search], :with => {:tag_ids => [...]} 
@announcements = @announcement_facets.for.paginate( :page => params[:page], :per_page => 10 )

这篇关于思维狮身人面像和act_as_taggable_on插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆