问题
How do i index arabic profile translations with sunspot solr. Can i use globalize and sunspot or should use some other approach?
models/profile.rb
translates :name, :description
validates :name
validates :description
searchable do
text :name
text :description
end
回答1:
You can define separate fields for each of your locales in your search block:
I18n.available_locales.each do |locale|
# Separate name field for each locale
text "name_#{locale}".to_sym do
# read_Attribute is defined by Globalize.
read_attribute(:name, locale: locale)
end
text "description_#{locale}".to_sym do
read_attribute(:description, locale: locale)
end
end
When you are doing a search, you have to specify which locale you want to use:
# Search by actual locale
search = Sunspot.search(Profile) do
fulltext "search term" do
fields "name_#{I18n.locale}".to_sym,
"description_#{I18n.locale}".to_sym,
end
end
来源:https://stackoverflow.com/questions/19646808/how-to-use-globalize-and-sunspot-in-rails-4