【问题标题】:Conditional "or" in Thinking Sphinx searchThinking Sphinx 搜索中的条件“或”
【发布时间】:2011-04-11 16:58:07
【问题描述】:

使用 RoR 2.3.8。

这是我的控制器代码:

class CitiesController < ApplicationController
  def show
    @city = City.find(params[:id])
    @shops = Shop.search @city.name, {
      :conditions => {:country => @city.country && (:city => @city.name || :state => @city.state)},
      :page => params[:page],
      :per_page => 100
      }
  end
end

:conditions =&gt; {:country =&gt; @city.country &amp;&amp; (:city =&gt; @city.name || :state =&gt; @city.state)} 显然不起作用,因为我只是想解释我想要实现的目标。

:city:state 将是 Spots 表中的列,而不是 Cities 表中的列。我希望结果返回其中任何一个满足条件。但是不知道怎么做。

谢谢。

【问题讨论】:

    标签: ruby-on-rails thinking-sphinx


    【解决方案1】:

    Tass 做对了 - 使用您的 TS 搜索调用,它应该看起来像这样:

    def show
      @city = City.find(params[:id])
      @shops = Shop.search "#{@city.name} @country #{@city.country} (@city #{@city.name} | @state #{@city.state})",
        :match_mode => :extended,
        :page       => params[:page],
        :per_page   => 100
        }
    end
    

    您会注意到我已经设置了匹配模式 - 如果您使用 :conditions 参数,Thinking Sphinx 会自动执行此操作 - 但是在手动构建查询时,您需要自己设置。

    【讨论】:

    • 谢谢帕特。当我尝试显示条目总数时,我在视图中得到了这个undefined method total_entries' for nil:NilClass`。
    【解决方案2】:

    将您的原始搜索放到 Sphinx - 您应该在 TS 文档中找到正确的方法。一个reference for raw search。你可能想要类似的东西

    "@city_country #{@city.country} (@city_name #{@city.name} | @city_state #{@city.state})"
    

    (我不确定 TS 是如何命名索引的。检查一下。)

    【讨论】:

      猜你喜欢
      • 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
      相关资源
      最近更新 更多