思考Sphinx模型加入搜索

时间:2013-07-25 22:22:01

标签: ruby-on-rails thinking-sphinx

也许这里已经讨论过这样的问题,但我甚至无法意识到,如何正确地谷歌这样的案例。

在Rails中我有以下模型:

  

用户(has_many book_specimens,has_many友谊,has_many朋友通过友谊,has_many书籍通过book_specimens)

     

Book_specimen(belongs_to user,belongs_to book)

     

预订(has_many book_specimens,has_many所有者通过book_specimens)

     

友谊(belongs_to user,belongs_to friend)

我需要的是在用户的朋友之间搜索书籍。如果进入非-sql逻辑,它看起来像

  

results = [];

     

friends.each do | friend |

     
    

results.push(Book.search conditions:{title:' Lorem',owner_id:friend.id})

  
     

有什么办法可以在一个命令中完成吗?那我该如何准备指数呢?

提前致谢。

1 个答案:

答案 0 :(得分:2)

您的图书模型将定义以下索引(除了您可能选择的其他索引):

define_index do
  ....
  ....
  indexes title
  has owner_ids
end

使用此索引,假设您具有目标“user”,搜索命令将变为:

Book.search conditions: {title: 'Lorem'}, with: {owner_ids: user.friend_ids}