【问题标题】:Thinking Sphinx Model joining search思考狮身人面像模型加入搜索
【发布时间】:2013-07-26 00:58:36
【问题描述】:

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

在 Rails 中,我有以下模型:

用户(has_many book_specimens,has_many Friendship,has_many Friends through Friendship,has_many book through book_specimens)

Book_specimen(belongs_to user,belongs_to book)

书(有_many book_specimens,通过book_specimens拥有_many所有者)

友谊(belongs_to user,belongs_tofriend)

我需要在用户朋友拥有的书籍中搜索书籍。如果打破非sql逻辑,它看起来像

结果 = [];

friends.each 做 |friend|

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

结束

有什么方法可以在一个命令中完成吗?那我应该如何准备索引呢?

提前致谢。

【问题讨论】:

    标签: ruby-on-rails thinking-sphinx


    【解决方案1】:

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

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

    有了这个索引,假设你有目标“用户”,搜索命令会变成:

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

    【讨论】:

    • 谢谢!按预期工作,但有一处更改:has owner_ids 应为 has owners.id, :as => :owner_ids
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多