I have serious problem with Thinking Sphinx in my Rails application. My app specifications are as follows:
- Rails 3.2.6
- Ruby 1.9.3
- Sphinx 2.0.4 (r3135)
- Thinking Sphinx 2.0.12
In my application I have a Page model and a Offer model and pages have many offers. An index defined in the Page model looks like this:
define_index do
#other indexes defined here
#...
has offers.width, as: :offers_width
has offers.height, as: :offers_height
has offers.price, as: :offers_price
set_property delta: true
end
Then I’m doing a search on the Page model, where pages are selected based on search query and conditions. However, when I’m trying to use a :with filter, Sphinx gave me wrong results.
When I use only one filter, for example price or width, the results are OK, however when I’m trying to combine filters, like price and width, I’m getting results containing offers with the given price OR width, not price AND width.
When I’m searching with ranges, most of the time :with parameters are ranges rather than just integer values.
Edit 1
Query im using:
Page.search Riddle.escape(query), conditions: conditions, with: has_cond, star: true, per_page: Page.per_page, page: page
where has_cond is:
{:offers_height=>[175..200], :offers_width=>[175..200], :offers_price=>[10..80]}
It still gives me pages where ANY offer has height in that range or ANY offer has width in that range or same thing with price.
Ok i found a solution. The reason sphinx gave me wrong results was because he was aggregating all offers inside single page. I had to move index from page to offers, and now query works as intended. My index in offer looks like this:
I need to query sphinx first, and then do active record query.