Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use globalize and sunspot in rails 4

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
like image 800
Murtza Avatar asked Nov 01 '22 11:11

Murtza


1 Answers

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
like image 181
dtengeri Avatar answered Nov 09 '22 10:11

dtengeri