Conditional “or” in Thinking Sphinx search

那年仲夏 提交于 2019-12-11 13:28:22

问题


Using RoR 2.3.8.

Here's my controller code:

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

The :conditions => {:country => @city.country && (:city => @city.name || :state => @city.state)} obviously doesn't work because I am just trying to explain what I wanna achieve.

:city and :state will be columns from Spots table, not Cities table. I want results to return either one of them fulfills the condition. But have no clue how to do it.

Thanks.


回答1:


Tass has got it right - with your TS search call, it should look something like this:

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

You'll note I've set the match mode - Thinking Sphinx will do this automatically if you're using the :conditions argument - but when constructing the query manually, you need to set it yourself.




回答2:


Place your raw search to Sphinx - you should find the correct method in the TS docu. A reference for raw search. You probably want something like

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

(I'm not sure how TS names the indexes. Check that.)



来源:https://stackoverflow.com/questions/5624775/conditional-or-in-thinking-sphinx-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!