2014-02-19 23 views
0

當前我正在使用solr在數據庫中進行搜索。 我想要的是按日期排序。模型中的軌道條件,以便使用太陽黑子solr對結果進行排序

問題:日期被序列化爲數據庫中的數組,因爲它可以被本地化。

我的邏輯然後會尋找一個國家特定的條目,如果不是那裏,使用默認的條目。

因此,它可以是這樣的:

[{"DE" => "localizeddate"}, {"EN" => "localizeddate"}, ...] 

或像這樣:

[{"default" => "defaultdate"}] 

現在,我的類看起來像這樣

class Product < ActiveRecord::Base 
serialize :colors, Array 
serialize :variants, Array 
serialize :images, Array 
serialize :sizes, Array 
serialize :online_from, Array 


searchable do 

    text :dw_productid, :display_name, :short_description  
    time :online_from # This is the date that solr will pick up for sorting 
end 
end 

的問題是,我不能只通過:online_from來solr。 如何在這裏插入邏輯?我在模型中做到了嗎?

感謝

本傑明

回答

0

我終於想通了如何做到這一點:

所以搜索的方法中,你可以爲了做更多的東西與它添加time :something do

類產品<的ActiveRecord :: Base的

serialize :colors, Array 
serialize :variants, Array 
serialize :images, Array 
serialize :sizes, Array 
serialize :online_from, Array 



searchable do 

    text :dw_productid, :display_name, :short_description  

    # Sort products according to the online from date 
    time :online_from do |onlinefromdate| 
     if onlinefromdate.online_from[0] != nil 
      @onlinefromdate = onlinefromdate.online_from[0]["DE"] if onlinefromdate.online_from[0]["DE"] != nil 
      @onlinefromdate = onlinefromdate.online_from[0]["default"] if onlinefromdate.online_from[0]["default"] != nil 
      @onlinefromdate = onlinefromdate.online_from[0][0] 
     else 
      @onlinefromdate = Date.parse('2001-02-03') 
     end 
    end 
end 
end