Indexing an instance method data

28 views
Skip to first unread message

Mike Disuza

unread,
Aug 5, 2010, 2:25:37 AM8/5/10
to Thinking Sphinx
Hi,
I have implemented the rating feature in my application which is using
sphinx/Thinking Sphinx for searching.
I have to add the rating in the indexing so that I can can sort my
result set depending the rating given by the user.

For rating I am using "acts_as_a_rateable" plugin which is giving a
rating by using "obj.rating" method.
I know that Sphinx does not do indexing of the method.

Can anyone have any idea how to solve this?

Thanks,
Mike.

James Healy

unread,
Aug 5, 2010, 2:38:21 AM8/5/10
to thinkin...@googlegroups.com
Mike Disuza wrote:
> For rating I am using "acts_as_a_rateable" plugin which is giving a
> rating by using "obj.rating" method.
> I know that Sphinx does not do indexing of the method.
>
> Can anyone have any idea how to solve this?

I'm not familiar with the acts_as_a_rateable plugin, but you probably
have 2 options:

* find out if the plugin stores the rating in your database somewhere
and add that column to your index

* if the rating is purely calculated in ruby, you will have to add a
model callback that caches the value in your database. I've described
this technique a few times on stack overflow, check out [1]

-- James Healy <ji...@deefa.com> Thu, 05 Aug 2010 16:37:45 +1000

[1] http://stackoverflow.com/questions/3391048/including-rails-activerecord-methods-in-sphinx-searches

Mike Disuza

unread,
Aug 6, 2010, 2:39:12 AM8/6/10
to Thinking Sphinx
Hi,
I am trying your solution like this
My model is like this:-
"
before_validate :cache_rating
define_index do
indexes :name,:sortable => true
indexes cached_rating, :as=>:property_rating
end
def cache_rating
self.rating
end
"
Whenever I am trying to rebuild the indexing using "rake ts:rebuild",
I am getting error "undefined method `before_validate' for #<Class:
0xb6cce9c4>"

Can anyone help me out.

Thanks,
Mike
> [1]http://stackoverflow.com/questions/3391048/including-rails-activereco...

James Healy

unread,
Aug 6, 2010, 2:42:44 AM8/6/10
to thinkin...@googlegroups.com
Mike Disuza wrote:
> Hi,
> I am trying your solution like this
> My model is like this:-
> "
> before_validate :cache_rating
> define_index do
> indexes :name,:sortable => true
> indexes cached_rating, :as=>:property_rating
> end
> def cache_rating
> self.rating
> end
> "
> Whenever I am trying to rebuild the indexing using "rake ts:rebuild",
> I am getting error "undefined method `before_validate' for #<Class:
> 0xb6cce9c4>"

My bad, that answer was untested. before_validate should be
before_validation.

Also, you're cache_rating method should look like:

def cache_rating
self.cached_rating = self.rating
end

Make sure your model has a cached_rating column in the DB.

-- James Healy <ji...@deefa.com> Fri, 06 Aug 2010 16:42:23 +1000

Pat Allan

unread,
Aug 6, 2010, 2:44:25 AM8/6/10
to thinkin...@googlegroups.com
Hi Mike

I think it's before_validation... and you'd probably want something more like:

before_validation :set_cached_rating

def set_cached_rating
self.cached_rating = rating
end

This way the value is actually being stored in the database :)

--
Pat

> --
> 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.
>

Mike Disuza

unread,
Aug 6, 2010, 3:42:47 AM8/6/10
to Thinking Sphinx
Hi
I am confused, Are you saying that I have to add the "cached_rating"
column in my table? If yes then it is better I will store the "rating"
value by creating a new column in the table. So that i will not be
having this headche.

Thanks,
Mike

Jim Ruther Nill

unread,
Aug 6, 2010, 4:03:51 AM8/6/10
to thinkin...@googlegroups.com
Hi Mike,

Yes. You have to add the ratings column in your database.  But since you are using the plugin which adds a 'rating' method for your model, you need to have a different column name for your database, hence Pat and James suggested the name to be 'cached_rating'.  The reason behind this is TS/Sphinx only deals with columns that are actually in your database.  So any method in your model can't be used.
  1. create a migration to add the cached_rating column to your table
  2. add the before_validation callback and necessary methods (as coded by Pat) to set the cached_rating value and save it in the database.
  3. rebuild your index
  4. happy searching :D
--
-------------------------------------------------------------
visit my blog at http://jimlabs.heroku.com

Mike Disuza

unread,
Aug 6, 2010, 4:19:02 AM8/6/10
to Thinking Sphinx
Thanks Guys For your reply.
I got the answer.

On Aug 6, 1:03 pm, Jim Ruther Nill <jvn...@gmail.com> wrote:
> Hi Mike,
>
> Yes. You have to add the ratings column in your database.  But since you are
> using the plugin which adds a 'rating' method for your model, you need to
> have a different column name for your database, hence Pat and James
> suggested the name to be 'cached_rating'.  The reason behind this is
> TS/Sphinx only deals with columns that are actually in your database.  So
> any method in your model can't be used.
>
>    1. create a migration to add the cached_rating column to your table
>    2. add the before_validation callback and necessary methods (as coded by
>    Pat) to set the cached_rating value and save it in the database.
>    3. rebuild your index
>    4. happy searching :D
> > thinking-sphi...@googlegroups.com<thinking-sphinx%2Bunsu...@googlegroups.com>
> > .
> > > > For more options, visit this group athttp://
> > 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 thinkin...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > thinking-sphi...@googlegroups.com<thinking-sphinx%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages