Let's focus on these two lines, because I think this is where the problem is:
results = Trip.search params[:keyword], {:conditions => conditions, :star =>
true}.merge(order), :group_by => 'trip_id'
@trips = results.paginate(:page => params[:page])
Firstly - you don't need the paginate call - Sphinx (and so Thinking Sphinx)
paginates automatically (you can't turn it off), and Thinking Sphinx accepts
:page as a search parameter.
Secondly - you've got two hashes in that search call - the first with the
conditions and star (and potential order clause), and the second with group_by.
It all needs to be in one hash, and you need the additional group_function
option as well. So it should be more like this (broken down over multiple lines
for better readability):
@trips = Trip.search params[:keyword], {
:conditions => conditions,
:star => true,
:group_by => 'trip_id',
:group_function => :attr,
:page => params[:page]
}.merge(options)
Of course, now that I'm writing the code, instead of copying and pasting, I've
realised another key issue: you're searching on trip, and yet trip_id belongs
to the TripDay index - which is why you're getting 0 results (Sphinx can't find
trip_id in the Trip index, understandably).
So, if you want Trip objects, but want to search on TripDay, then it actually
turns out to be something more like this:
@trips = TripDay.search(params[:keyword], {
:conditions => conditions,
:star => true,
:group_by => 'trip_id',
:group_function => :attr,
:page => params[:page]
}.merge(options)).collect { |trip_day| trip_day.trip }
BUT: your order options won't work, because they reference attributes in the
Trip index, not the TripDay index.
And so we return to the initial problem: the spot information not existing in
the Trip index.
I would re-add those fields back into the Trip index, then stop Sphinx (rake
ts:stop), and delete all the index files (have we tried this already? Not sure)
- by default they're in db/sphinx/production - everything in there can be
deleted.
Then, re-index and restart Sphinx - which can all be done by rake ts:rebuild.
This should get us back to a clean slate, and perhaps everything is happy now
with your original search?
Fingers crossed!
--
Pat
On 03/11/2010, at 7:34 PM, Victor wrote:
> I search the trips with spots from trips_controller index:
>
> class TripsController < ApplicationController
> before_filter :require_user, :except => [:show, :index]
>
> def index
> if params[:filter] == 'on'
> country = params[:country] == 'All' ? {} : {:spot_country =>
> params[:country]}
> duration = params[:days].blank? ? "" : params[:days]
>
> if !duration.blank? && country
> conditions = {:duration => duration}.merge(country)
> elsif !duration.blank? && country.blank?
> conditions = {:duration => duration}
> else country && duration.blank?
> conditions = country
> end
>
> keyword = params[:keyword].blank? ? "" : params[:keyword]
>
> case params[:order]
> when 'date_descend'
> order = {:order => 'created_at DESC'}
> when 'date_ascend'
> order = {:order => 'created_at ASC'}
> when 'rating_descend'
> order = {:order => 'rating_average DESC'}
> when 'rating_ascend'
> order = {:order => 'rating_average ASC'}
> when 'budget_dscend'
> order = {:order => 'budget DESC'}
> when 'budget_ascend'
> order = {:order => 'budget ASC'}
> else
> order = {}
> end
>
> results = Trip.search params[:keyword], {:conditions =>
> conditions, :star => true}.merge(order), :group_by => 'trip_id'
> @trips = results.paginate(:page => params[:page])
> else
> @trips = Trip.paginate(:all, :page => params[:page], :order =>
> 'created_at DESC')
> end
> end
> .
> .
> .
> end
>
> Already added "has trip_id" in my TripDay's index. Stopped Sphinx,
> rake ts:rebuild, tried to search, but returned zero result. Most
> likely it's something I had done wrong.
>
> On Nov 3, 4:11 pm, Victor <[email protected]> wrote:
>> Hi Pat
>>
>> Here are the models so far:
>>
>> trip_day.rb
>> =========
>> class TripDay < ActiveRecord::Base
>> belongs_to :trip
>>
>> has_many :trip_day_spots
>> has_many :spots, :through => :trip_day_spots
>>
>> # ThinkingSphinx Index
>> define_index do
>> indexes spots.name, :as => :spot_name
>> indexes spots.city, :as => :spot_city
>> indexes spots.state, :as => :spot_state
>> indexes spots.country, :as => :spot_country
>> end
>> end
>>
>> Trip.rb
>> =======
>> class Trip < ActiveRecord::Base
>> belongs_to :user, :counter_cache => true
>>
>> has_many :spot_trips, :dependent => :destroy
>> has_many :spots, :through => :spot_trips
>>
>> has_many :trip_days, :dependent => :destroy
>> has_many :trip_reviews, :dependent => :destroy
>>
>> # ThinkingSphinx Index
>> define_index do
>> indexes :name, :sortable => true
>> indexes duration
>> has budget, created_at, rating_average
>> end
>> end
>>
>> trip_day already belongs to Trip. Must I put in "has trip_id" in
>> trip_day.rb?
>>
>> Sorry for being a noob here.
>>
>> Thanks.
>>
>> On Nov 3, 2:43 pm, Pat Allan <[email protected]> wrote:
>>
>>> Hi Victor
>>
>>> Well, it's great that it works here.
>>
>>> 1. You are indeed right, you're indexing columns from the spots association
>>> as fields.
>>> 2. You could add an attribute of trip_id to TripDay's index:
>>> has trip_id
>>> And then group the results by
>>> trip_id...http://freelancing-god.github.com/ts/en/searching.html#grouping
>>
>>> That said, it *should* work for Trip. Perhaps try stopping Sphinx, deleting
>>> the index files, and then run rake ts:rebuild to generate it all from
>>> scratch for sure?
>>
>>> --
>>> Pat
>>
>>> On 03/11/2010, at 4:55 PM, Victor wrote:
>>
>>>> Hi Pat,
>>
>>>> I have added indexes to the following
>>
>>>> trip_day.rb
>>>> ========
>>>> class TripDay < ActiveRecord::Base
>>>> belongs_to :trip
>>
>>>> has_many :trip_day_spots
>>>> has_many :spots, :through => :trip_day_spots
>>
>>>> # ThinkingSphinx Index
>>>> define_index do
>>>> indexes spots.name, :as => :spot_name
>>>> indexes spots.city, :as => :spot_city
>>>> indexes spots.state, :as => :spot_state
>>>> indexes spots.country, :as => :spot_country
>>>> end
>>>> .
>>>> .
>>>> .
>>>> end
>>
>>>> I ran ts:rebuild, then search this in console:
>>
>>>>>> TripDay.search "Cititel Mid Valley"
>>>> => [#<TripDay id: 4, trip_date: "2010-10-06", trip_id: 2, created_at:
>>>> "2010-10-21 10:17:18", updated_at: "2010-10-21 10:17:18">]
>>
>>>> Few questions:
>>
>>>> 1. Previously, was the trip.rb indexing the spots name, city, country,
>>>> etc. correctly? They are indexed, am I right?
>>>> 2. With the TripDay now can be indexed, is there anyway for me to use
>>>> it as search for my Trips?
>>
>>>> Thanks.
>>
>>>> On Nov 2, 11:59 am, Pat Allan <[email protected]> wrote:
>>>>> Of course - though I would prefer a donation once the problem's solved
>>>>> ;)http://pledgie.com/campaigns/1752
>>
>>>>> --
>>>>> Pat
>>
>>>>> On 02/11/2010, at 2:43 PM, Victor wrote:
>>
>>>>>> No problem Pat. Can I have a link to make donation? :)
>>
>>>>>> On Nov 2, 8:06 am, Pat Allan <[email protected]> wrote:
>>>>>>> Sorry Victor, I'm out of ideas as to what's causing the problem, beyond
>>>>>>> investigating it on the server myself (and even then, I couldn't
>>>>>>> promise anything).
>>
>>>>>>> --
>>>>>>> Pat
>>
>>>>>>> On 01/11/2010, at 11:33 PM, Victor wrote:
>>
>>>>>>>> So when I did the Trip.searc:
>>
>>>>>>>>>> Trip.search "Inna Putri Bali"
>>
>>>>>>>> => []
>>
>>>>>>>> Spots in Trip is not indexed?
>>
>>>>>>>> I don't have any sphinx scopes.
>>
>>>>>>>> On Nov 1, 8:29 pm, Pat Allan <[email protected]> wrote:
>>>>>>>>> But you're running these commands via console - so we're avoiding the
>>>>>>>>> controllers and views, and it's still not working...
>>
>>>>>>>>> --
>>>>>>>>> Pat
>>
>>>>>>>>> On 01/11/2010, at 9:27 PM, Victor wrote:
>>
>>>>>>>>>> Could it be that my filter in view or controller wasn't setup
>>>>>>>>>> properly
>>>>>>>>>> to search the right place?
>>
>>>>>>>>>> On Nov 1, 6:21 pm, Pat Allan <[email protected]> wrote:
>>>>>>>>>>> I live in Melbourne, and I've not been to the Gaol yet ;)
>>
>>>>>>>>>>> I'm running out of ideas at the moment... the SQL looks correct,
>>>>>>>>>>> the data's being indexed, and searchd is restarting as expected.
>>>>>>>>>>> You don't have any sphinx scopes (or more importantly - a default
>>>>>>>>>>> sphinx scope) in the Trip model, do you?
>>
>>>>>>>>>>> --
>>>>>>>>>>> Pat
>>
>>>>>>>>>>> On 01/11/2010, at 9:13 PM, Victor wrote:
>>
>>>>>>>>>>>> If I did "Spot.search "Inna Putri Bali"
>>
>>>>>>>>>>>>>> Spot.search "Inna Putri Bali"
>>>>>>>>>>>> => [#<Spot id: 2, spot_type: "accommodation", name: "Inna Putri
>>>>>>>>>>>> Bali",
>>>>>>>>>>>> acc_type: "Hotel", operation_hours: "", check_in: "2000-01-01
>>>>>>>>>>>> 14:00:00", check_out: "2000-01-01 13:00:00", season: "No season",
>>>>>>>>>>>> prices: "Check their website for latest deals. It's much che...",
>>>>>>>>>>>> country: "Indonesia", zip: "80363", state: "Bali", city: "Kuta",
>>>>>>>>>>>> address: "Jalan Kawasan Nusa Dua Resort", contact: "+62 361771020",
>>>>>>>>>>>> email: "[email protected]", web: "http://hotelputribali.com",
>>>>>>>>>>>> description: "Inna Putri Bali is set on a wide of Nusa Dua. The
>>>>>>>>>>>> e...",
>>>>>>>>>>>> reviews_count: 0, rating_average:
>>>>>>>>>>>> #<BigDecimal:b65d3ffc,'0.0',4(8)>,
>>>>>>>>>>>> lat: #<BigDecimal:b65d3fac,'-0.8799857E1',12(16)>, lng:
>>>>>>>>>>>> #<BigDecimal:b65d3ee4,'0.115228862E3',12(20)>, created_at:
>>>>>>>>>>>> "2010-10-20
>>>>>>>>>>>> 04:12:17", updated_at: "2010-11-01 09:04:36", photos_count: 13>]
>>
>>>>>>>>>>>> On Nov 1, 5:48 pm, Victor <[email protected]> wrote:
>>>>>>>>>>>>> Trying "Inna Putri Bali" instead
>>
>>>>>>>>>>>>> Loading production environment (Rails 2.3.8)
>>>>>>>>>>>>> ** Erubis 2.6.6>> Trip.search "Inna Putri Bali"
>>
>>>>>>>>>>>>> => []
>>
>>>>>>>>>>>>> Looks like we have a problem here huh...
>>
>>>>>>>>>>>>> By the way, Old Melbourne Gaol was awesome. Went there once.
>>
>>>>>>>>>>>>> On Nov 1, 5:41 pm, Pat Allan <[email protected]> wrote:
>>
>>>>>>>>>>>>>> Right, that seems fine, so now let's look back within the Rails
>>>>>>>>>>>>>> app. On the server, in 'script/console production', what's the
>>>>>>>>>>>>>> output of the following:
>>>>>>>>>>>>>> Trip.search "Old Melbourne Gaol"
>>
>>>>>>>>>>>>>> --
>>>>>>>>>>>>>> Pat
>>
>>>>>>>>>>>>>> On 01/11/2010, at 8:37 PM, Victor wrote:
>>
>>>>>>>>>>>>>>> So I run 'ps aux | grep searchd'
>>
>>>>>>>>>>>>>>> 1000 17569 0.0 0.2 9168 1144 ? S 04:00 0:00
>>>>>>>>>>>>>>> searchd --pidfile --config /var/www/abc.com/public/abc/releases/
>>>>>>>>>>>>>>> 20101008073517/config/production.sphinx.conf
>>>>>>>>>>>>>>> 1000 20220 0.0 0.1 1864 700 pts/0 S+ 17:34
>>>>>>>>>>>>>>> 0:00 grep
>>>>>>>>>>>>>>> --color=auto searchd
>>
>>>>>>>>>>>>>>> Then 'rake ts:rebuild RAILS_ENV=production'
>>
>>>>>>>>>>>>>>> ** Erubis 2.6.6
>>>>>>>>>>>>>>> Stopped search daemon (pid 17569).
>>>>>>>>>>>>>>> Generating Configuration to
>>>>>>>>>>>>>>> /var/www/abc.com/public/abc/releases/
>>>>>>>>>>>>>>> 20101008073517/config/production.sphinx.conf
>>>>>>>>>>>>>>> Sphinx 1.10-beta (r2420)
>>>>>>>>>>>>>>> Copyright (c) 2001-2010, Andrew Aksyonoff
>>>>>>>>>>>>>>> Copyright (c) 2008-2010, Sphinx Technologies Inc (http://
>>>>>>>>>>>>>>> sphinxsearch.com)
>>
>>>>>>>>>>>>>>> using config file
>>>>>>>>>>>>>>> '/var/www/abc.com/public/abc/releases/20101008073517/
>>>>>>>>>>>>>>> config/production.sphinx.conf'...
>>>>>>>>>>>>>>> indexing index 'spot_core'...
>>>>>>>>>>>>>>> collected 7 docs, 0.0 MB
>>>>>>>>>>>>>>> sorted 0.0 Mhits, 100.0% done
>>>>>>>>>>>>>>> total 7 docs, 5351 bytes
>>>>>>>>>>>>>>> total 0.015 sec, 343828 bytes/sec, 449.78 docs/sec
>>>>>>>>>>>>>>> skipping non-plain index 'spot'...
>>>>>>>>>>>>>>> indexing index 'trip_core'...
>>>>>>>>>>>>>>> collected 3 docs, 0.0 MB
>>>>>>>>>>>>>>> sorted 0.0 Mhits, 100.0% done
>>>>>>>>>>>>>>> total 3 docs, 55 bytes
>>>>>>>>>>>>>>> total 0.004 sec, 13372 bytes/sec, 729.39 docs/sec
>>>>>>>>>>>>>>> skipping non-plain index 'trip'...
>>>>>>>>>>>>>>> total 12 reads, 0.000 sec, 8.1 kb/call avg, 0.0 msec/call avg
>>>>>>>>>>>>>>> total 24 writes, 0.000 sec, 9.0 kb/call avg, 0.0 msec/call avg
>>>>>>>>>>>>>>> Started successfully (pid 20236).
>>
>>>>>>>>>>>>>>> Then 'ps aux | grep searchd'
>>
>>>>>>>>>>>>>>> 1000 20236 0.0 0.1 9164 780 pts/0 S 17:35 0:00
>>>>>>>>>>>>>>> searchd --pidfile --config /var/www/abc.com/public/abc/releases/
>>>>>>>>>>>>>>> 20101008073517/config/production.sphinx.conf
>>>>>>>>>>>>>>> 1000 20238 0.0 0.1 1860 596 pts/0 S+ 17:37
>>>>>>>>>>>>>>> 0:00 grep
>>>>>>>>>>>>>>> --color=auto searchd
>>
>>>>>>>>>>>>>>> On Nov 1, 5:32 pm, Pat Allan <[email protected]> wrote:
>>>>>>>>>>>>>>>> Hi Victor
>>
>>>>>>>>>>>>>>>> That seems fine...
>>>>>>>>>>>>>>>> What is the output of 'ps aux | grep searchd' before *and*
>>>>>>>>>>>>>>>> after running 'rake ts:rebuild'?
>>
>>>>>>>>>>>>>>>> I want to make sure we're dealing with just a single searchd
>>>>>>>>>>>>>>>> instance that is noting the changes.
>>
>>>>>>>>>>>>>>>> Cheers
>>
>>>>>>>>>>>>>>>> --
>>>>>>>>>>>>>>>> Pat
>>
>>>>>>>>>>>>>>>> On 01/11/2010, at 8:24 PM, Victor wrote:
>>
>>>>>>>>>>>>>>>>> Here it is in my production.sphinx.conf instead.
>>
>>>>>>>>>>>>>>>>> indexer
>>>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>>>> }
>>
>>>>>>>>>>>>>>>>> searchd
>>>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>>>> listen = 127.0.0.1:9791
>>>>>>>>>>>>>>>>> log =
>>>>>>>>>>>>>>>>> /var/www/abc.com/public/abc/releases/20101008073517/log/
>>>>>>>>>>>>>>>>> searchd.log
>>>>>>>>>>>>>>>>> query_log =
>>>>>>>>>>>>>>>>> /var/www/abc.com/public/abc/releases/20101008073517/log/
>>>>>>>>>>>>>>>>> searchd.query.log
>>>>>>>>>>>>>>>>> pid_file =
>>>>>>>>>>>>>>>>> /var/www/abc.com/public/abc/releases/20101008073517/log/
>>>>>>>>>>>>>>>>> searchd.production.pid
>>>>>>>>>>>>>>>>> }
>>
>>>>>>>>>>>>>>>>> source spot_core_0
>>>>>>>>>>>>>>>>> {
>>>>>>>>>>>>>>>>> type = mysql
>>>>>>>>>>>>>>>>> sql_host = localhost
>>>>>>>>>>>>>>>>> sql_user = root
>>>>>>>>>>>>>>>>> sql_pass =
>>>>>>>>>>>>>>>>> sql_db = abc_production
>>>>>>>>>>>>>>>>> sql_query_pre = SET
>>
>> ...
>>
>> read more ยป
>
> --
> You received this message because you are subscribed to the Google Groups
> "Thinking Sphinx" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/thinking-sphinx?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Thinking Sphinx" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en.