2011-09-02 73 views
3

我基準2版我的Solr索引的第一個具有以下包括語句:做什麼:在太陽黑子/ solr搜索方法中包含什麼內容?

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

第二:

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

我期待看到一個減速的版本包括但在這裏是結果:

版本包括:

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> 

和不包括:

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> 

有誰知道,如果包括應該工作?如果是這樣,我做錯了嗎? 我看了一下文檔:http://outoftime.github.com/sunspot/rails/docs/,並沒有提到這一點。

回答

3

根據the API:include將會:

允許的ActiveRecord當索引加載所需的關聯。

您的基準測試無法正常工作,因爲您正在將普通Ruby迭代器中的單個記錄編入索引。當您將單個記錄編入索引50次時,太陽黑子根本無法利用急切的加載。相反,你應該這樣做:

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

哦,你可以測試,如果以下是比以上更快?我真的很想知道。

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

也有一個bug目前該STI車型將忽略:include

1

通過查看Rails日誌中的SQL查詢,您可以看到:include在可搜索的頁面上導致在索引編制時進行加載。 #search上的:include在搜索時導致急切加載。