【问题标题】:Thinking Sphinx: search across multiple models: best practices?Thinking Sphinx:跨多个模型搜索:最佳实践?
【发布时间】:2012-03-17 17:32:20
【问题描述】:

我想添加一个带有类别的 jquery 自动完成功能。 该请求将使用 Thinking Sphinx 搜索多个模型(论坛主题、新闻、用户...)

所以在控制器中,我认为它会是这样的

def autocomplete
 @news = Actu.search(params[:term]).map {|g| {:label => g.title, :category => "Actualités", :id => g.id}}
 @topics = Topic.search(params[:term]).map {|g| {:label => g.title, :category => "Topics", :id => g.id}}
 @anotherModel = ...

 respond_to do |format|
   format.js { render :json => @news+@topics+@anotherModel }
 end
end

这行得通,但是您如何看待这些做法?

【问题讨论】:

    标签: ruby-on-rails postgresql sphinx thinking-sphinx


    【解决方案1】:

    你可以试试这个很棒的语法

    ThinkingSphinx.search 'pancakes', :classes => [Article, Comment]
    

    阅读更多http://freelancing-god.github.com/ts/en/searching.html

    【讨论】:

    • 哦,我已多次阅读此页面...但我没有看到“应用程序范围的搜索”部分。谢谢!
    【解决方案2】:

    您可以搜索应用程序中的所有索引模型:

    ThinkingSphinx.search(params[:term])
    

    然后您可以为每个模型方法定义返回散列的autocomplete_json

    所以,你的行动

    def autocomplete
      render :json => ThinkingSphinx.search(params[:term]).map(&:autocomplete_json)
    end
    

    【讨论】:

    • 你为什么这么认为?这是用户设计的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    相关资源
    最近更新 更多