Thinking Sphinx 和acts_as_taggable_on 插件

发布于 2024-08-18 16:28:11 字数 860 浏览 6 评论 0原文

我为 ruby​​ on Rails 2.3.2 安装了 Sphinx 和 Thinking Sphinx。

当我无条件搜索时,搜索工作正常。现在,我想做的是按标签过滤,因此,当我使用acts_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}, :页=> params[:page], :per_page =>; 10

我猜想出了什么问题,并且它没有正确搜索。

谁能告诉我发生了什么事吗?

谢谢, 布莱恩

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

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?

Thanks,
Brian

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋竹姑娘 2024-08-25 16:28:11

思考狮身人面像依赖于模型中的关联。在常见情况下,您只需将索引定义放在关联下方即可。

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

索引tags.name, :as => :标签

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

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 )

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

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

indexes tags.name, :as => :tags

TS interprets it like:

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

(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.

But we expect:

CAST(GROUP_CONCAT(DISTINCT IFNULL(`tags`.`name`, '0') SEPARATOR ' ') AS CHAR) AS `tags`

and:

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

In define_index method:

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

In controller:

@announcement_facets = Announcement.facets params[:search], :with => {:tag_ids => [...]} 
@announcements = @announcement_facets.for.paginate( :page => params[:page], :per_page => 10 )
掩耳倾听 2024-08-25 16:28:11

我发现简单地定义索引:

Class Thing < ActiveRecord::Base    

acts_as_taggable

     define_index do
        ..other indexing...
        indexes taggings.tag.name, :as => :tags
     end
end

工作得很好。

I found that simply defining the index thus:

Class Thing < ActiveRecord::Base    

acts_as_taggable

     define_index do
        ..other indexing...
        indexes taggings.tag.name, :as => :tags
     end
end

worked fine.

紫南 2024-08-25 16:28:11

一种可能性是您需要将 tag_ids 的类型声明为 :multi 因为 TS 可能会感到困惑(我刚刚在这里发现了这一点 http://groups.google.com/group/thinking-sphinx/browse_thread/thread/9bd4572398f35712/14d 4c1503f5959a9?lnk=gst& q=yanowitz#14d4c1503f5959a9)。

但为什么不使用标签名称来搜索呢?例如,

Announcement.search params[:announcement][:search].to_s, :conditions => {:tags => "my_tag"}, :page => params[:page], :per_page => 10

或者,如果您需要搜索多个标签:(

Announcement.search( "#{params[:announcement][:search].to_s} (@tags my_tag | @tags your_tag)", :page => params[:page], :per_page => 10 )

顺便说一句,您可能需要在使用之前从用户提供的查询中清理/删除 sphinx-control-characters )。

为了调试,我会进入控制台并尽可能地删除您的查询(消除分页参数,甚至查询(只需执行“”)等)。

One possibility is that you need to declare the type for tag_ids as :multi because TS can get confused (I just discovered this here http://groups.google.com/group/thinking-sphinx/browse_thread/thread/9bd4572398f35712/14d4c1503f5959a9?lnk=gst&q=yanowitz#14d4c1503f5959a9).

But why not use the tag names to search? E.g.,

Announcement.search params[:announcement][:search].to_s, :conditions => {:tags => "my_tag"}, :page => params[:page], :per_page => 10

Or, if you need to search for multiple tags:

Announcement.search( "#{params[:announcement][:search].to_s} (@tags my_tag | @tags your_tag)", :page => params[:page], :per_page => 10 )

(as aside, you may want to sanitize/remove sphinx-control-characters from the user-provided query before using it).

For debugging, I would go into console and strip down your query as much as possible (eliminate pagination arguments, even the query (just do ""), etc.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文