Defining index when more than 1 accessor involved - need help.

2 views
Skip to first unread message

ct9a

unread,
Jan 6, 2010, 11:00:55 PM1/6/10
to Thinking Sphinx
Hi, guys,

I need some help defining index when more than 1 accessor involved.
I have looked up the docs (http://rdoc.info/projects/freelancing-god/
thinking-sphinx) to no success :(

Consider this scenario.

Search index to be built on a 'parts' object.
Each 'parts' object belongs to a 'fit' object.
A 'vehicle' object has many attributes among which is 'name'.
Each 'fit' object links a part with a vehicle (by vehicle_id and
part_id).

I am trying to build an index by means of "indexes fit.vehicle
(:name), :as => :vehicle_name".


However, when I run "rake thinking_sphinx:index", I get the following
error:

-------------------- Extract start --------------------
(in /home/ct9a/projects/myApp)
Generating Configuration to /home/ct9a/projects/myApp/config/
development.sphinx.conf
Sphinx 0.9.9-release (r2117)
Copyright (c) 2001-2009, Andrew Aksyonoff

using config file '/home/ct9a/projects/myApp/config/
development.sphinx.conf'...
indexing index 'part_core'...
ERROR: index 'part_core': sql_range_query: Unknown column
'parts.name' in 'field list'
(DSN=mysql://root:***@localhost:3306/myApp-development).
total 0 docs, 0 bytes
-------------------- Extract end --------------------

Here's how my model files look like.

part.rb
-------
class Part < ActiveRecord::Base
belongs_to :category
has_many :fits

define_index do
indexes title
indexes fit.vehicle(:name), :as => :vehicle_name
end
end

vehicle.rb
----------
class Vehicle < ActiveRecord::Base
has_many :fit
end

fit.rb
------
class Fit < ActiveRecord::Base
belongs_to :part
belongs_to :vehicle
end

I suspect that my syntax (indexes fit.vehicle(:name), :as
=> :vehicle_name)
doesn't work when more than 1 accessor is involved (ie. parts to
access fits,
and fits to access vehicle name). Can anyone please guide me?

Thank you.

ct9a

unread,
Jan 7, 2010, 3:03:50 AM1/7/10
to Thinking Sphinx
I took another poke at the issue.
Figured I set up another define_index block in my fit.rb model file
because a fit object has many related vehicle objects.
Will be trying it out and test the solution.
Stay tuned :)

ct9a

unread,
Jan 7, 2010, 11:22:15 PM1/7/10
to Thinking Sphinx
hmmm no good.
The search is usually done on the parts models using thinking sphinx.

Since the 'fit' models are to be searched by thinking sphinx, how to
append the results of fit to parts?

here's the algorithm:

@search = Parts.search (... )

then
@search = @search + Fits.search (...). <-- what's a valid ruby
syntax to achieve this?

thanks

Jeremy Peterson

unread,
Jan 10, 2010, 6:11:06 PM1/10/10
to thinkin...@googlegroups.com
This is what I do:


@search = Parts.search (... )

then
fits_search = Fits.search(...)
@search = @search | fits_search 

The "|" merges arrays together.  so parts results will be first, then fits search is appended to the end.

Jeremy

Jeremy Peterson
Applications Developer



--
You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
To post to this group, send email to thinkin...@googlegroups.com.
To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.




Gordon Yeong

unread,
Jan 10, 2010, 6:12:30 PM1/10/10
to thinkin...@googlegroups.com
Ultra cool. Will try it out when I get to work. Cheers, Jeremy.


2010/1/11 Jeremy Peterson <jeremy.g...@gmail.com>

Pat Allan

unread,
Jan 10, 2010, 6:13:57 PM1/10/10
to thinkin...@googlegroups.com
Jumping back to your first approach... if you use fits.vehicle.name, instead of fit.vehicle.name, then it should index without problems... you need to use the association name - so if it's plural, keep it plural, even though it may read better as singular.

--
Pat

Gordon Yeong

unread,
Jan 10, 2010, 10:24:43 PM1/10/10
to thinkin...@googlegroups.com
ah ha!!! thanks !! It works now, Pat.
Cheers, champ!

Gordon

2010/1/11 Pat Allan <p...@freelancing-gods.com>
Reply all
Reply to author
Forward
0 new messages