2013-10-06 47 views
0

您好,我正在尝试在我的项目中使用轮胎和elasticsearch。弹性搜索和轮胎

我能够索引模型并查询它们中的每一个,但我在连接表中遇到困难。

我的模型

class Item < ActiveRecord::Base 
attr_accessible :category_id, :description, :name, :rating 
belongs_to :category 
has_and_belongs_to_many :posts 
include Tire::Model::Search 
include Tire::Model::Callbacks 
after_save do 
    update_index 
end 

tire.mapping do 
    indexes :name, :analyzer => 'snowball', :boost => 100 
    indexes :posts 
end 
end 



class Post < ActiveRecord::Base 
include Tire::Model::Search 
include Tire::Model::Callbacks 
has_and_belongs_to_many :tags 
has_and_belongs_to_many :items 


attr_accessible :posted_at, :text, :thread_id, :username 

tire.mapping do 
    indexes :id, type: 'integer' 
    indexes :text, :analyzer => 'snowball', :boost => 100 
    indexes :thread_id, type: 'integer' 
    indexes :posted_at, type: 'date' 
end 
end 

正如你可以看到我有项目和邮政

之间的连接表,如果我有一个项目的名字,我怎么可以搜索Post.search(属于那些帖子此项目)与弹性搜索或Item.search(名称).posts?

+0

我认为轮胎不够灵活,无法做到这一点。它需要搜索两种不同的文档类型。我认为你将不得不使用轮胎查询dsl编写自己的查询。 – phoet

回答

1

可能是你能做的最好的就是从这里开始:

http://stackoverflow.com/questions/11692560/elasticsearch-tire-and-nested-queries-associations-with-activerecord/11711477#11711477 

,然后看http://railscasts.com/episodes/307-elasticsearch-part-2

,然后就浏览轮胎自上而下http://karmi.github.io/retire/

我解决类似的东西.. 。会让你知道我是如何解决这个问题的:)

+0

谢谢你Redrick。将尝试你的建议,并会等待,看看你是否提出了一个解决方案。同时我也会尝试。 – Leon

+0

你好,我用elasticsearch解决了我的问题,如果你愿意,我可以在http://blog.antasandrej.net上找到一些代码+博客文章(等待它加载托管在heroku上,它会睡很多:)) ,那里你可以找到github链接,如果你仍然无法使用这个想法,试着联系我,我给自己一个非常复杂的设置,所以我相信我现在也可以帮你:)祝你有美好的一天 – Redrick

相关问题