Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does :include inside a sunspot/solr searchable method do anything?

Tags:

ruby

solr

sunspot

I benchmarked 2 version of my solr index the first with the following include statement:

searchable(:auto_index => false, :auto_remove => true,
           :include => { :account => true,
           :user_practice_contact => [:city],
           :user_professional_detail => [:specialty, :subspecialties]}) do

The second:

searchable(:auto_index => false, :auto_remove => true) do

I was expecting to see a speed bump on the version with includes but here is the outcome:

version with includes:

Benchmark.measure { User.limit(50).each do |u|; u.index; end; Sunspot.commit; }
   => #<Benchmark::Tms:0x1130b34e8 @real=6.8079788684845, @utime=5.05, @cstime=0.0, @cutime=0.0, @total=5.2, @label="", @stime=0.149999999999999>

and without the includes:

Benchmark.measure { User.limit(50).each do |u|; u.index; end; Sunspot.commit; }
 => #<Benchmark::Tms:0x112ef0fe8 @real=6.82465195655823, @utime=4.92, @cstime=0.0, @cutime=0.0, @total=5.07, @label="", @stime=0.15> 

Does anybody know if the includes are supposed to work? And if so, am I doing it wrong? I looked at the docs: http://outoftime.github.com/sunspot/rails/docs/ and see no mention of that.

like image 623
brupm Avatar asked Apr 09 '26 16:04

brupm


2 Answers

According to the API, :include will:

allow ActiveRecord to load required associations when indexing.

Your benchmark does not work properly because you are indexing individual records in an ordinary Ruby iterator. As you are indexing a single record 50 times, Sunspot wouldn't be able to utilize the eager loading at all. Instead you should do:

Sunspot.index( User.limit(50) );
Sunspot.commit

Oh and could you test if the following is faster than above? I really want to know.

Sunspot.index( User.includes(:account).limit(50) );
Sunspot.commit

Also there is a bug currently that STI models will ignore the :include.

like image 86
lulalala Avatar answered Apr 11 '26 06:04

lulalala


By looking at the SQL queries in the Rails log, you can see that :include on searchable causes eager loading while indexing. :include on #search caused eager loading while searching.

like image 24
sampierson Avatar answered Apr 11 '26 08:04

sampierson