How to use globalize and sunspot in rails 4

不羁的心 提交于 2020-01-24 04:30:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!