2013-10-06 110 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

相關問題