Thinking Sphinx 和acts_as_taggable_on 插件
我为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
思考狮身人面像依赖于模型中的关联。在常见情况下,您只需将索引定义放在关联下方即可。
使用 acts_as_taggable_on 插件,您在模型文件中和编写时没有与标签相关的关联
TS 将其解释为:(
在我的例子中,请查看development.sphinx.conf 中的sql_query)。
我想您在模型公告中有属性名称,并且在重建索引时不会遇到错误。
但我们期望:
并且:
为了让事情正常工作,只需在重建索引之前在模型中添加与标签相关的关联:
在 define_index 方法中:
在控制器中:
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
TS interprets it like:
(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:
and:
To get things working just add tag-related associations in your model before you rebuild index:
In define_index method:
In controller:
我发现简单地定义索引:
工作得很好。
I found that simply defining the index thus:
worked fine.
一种可能性是您需要将 tag_ids 的类型声明为 :multi 因为 TS 可能会感到困惑(我刚刚在这里发现了这一点 http://groups.google.com/group/thinking-sphinx/browse_thread/thread/9bd4572398f35712/14d 4c1503f5959a9?lnk=gst& q=yanowitz#14d4c1503f5959a9)。
但为什么不使用标签名称来搜索呢?例如,
或者,如果您需要搜索多个标签:(
顺便说一句,您可能需要在使用之前从用户提供的查询中清理/删除 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.,
Or, if you need to search for multiple tags:
(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.).