Conditional "or" in Thinking Sphinx search
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.c开发者_开发技巧ountry && (: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.
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.
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.)
精彩评论