在搜索中忽略大写,小写和重音

时间:2016-10-16 10:24:20

标签: ruby-on-rails search params

我有一个带过滤器here的搜索系统。这个系统就像魅力一样,但我有一些问题,包括羽毛/大写和重音。

例如,如果我搜索“marée”我有结果,但如果我搜索“MAREE”或“Marée”或“maree”。我没有结果。

我想解决这个问题。我怎么解决这个问题?谢谢。

我的控制器

    def resultnohome
          if params[:query].blank?
            redirect_to action: :index and return
          else
          @campings = Camping.searchi(params[:query], params[:handicap], params[:animaux], params[:television], params[:plage], params[:etang], params[:lac])
            if params[:query] == "aube"
              @pub = Camping.find_by_id(1)
            else
            end
        end
end

我的模特

 def self.searchi(query, handicap, animaux, television, plage, etang, lac)
    return scoped unless query.present?
         result = left_outer_joins(:caracteristiquetests, :situations).where('nomdep LIKE ? OR name LIKE ? OR nomregion LIKE ? OR commune LIKE?', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%")
         result = result.where('handicap LIKE ?', "%#{handicap}%") if handicap
         result = result.where('animaux LIKE ?', "%#{animaux}%") if animaux
         result = result.where('television LIKE ?', "%#{television}%") if television
         result = result.where('plage LIKE ?', "%#{plage}%") if plage
         result = result.where('etang LIKE ?', "%#{etang}%") if etang
         result = result.where('lac LIKE ?', "%#{lac}%") if lac
      return result
  end

1 个答案:

答案 0 :(得分:1)

如果你坚持使用SQLite,那么你就没有很多好的选择。最常见的建议是在数据库中添加额外的列,这些列是标准化值,因此如果您的plage列包含“Marée”,那么您还有一个包含“maree”的列plage_ascii

您需要使用迁移创建其他列,然后在模型中执行before_save操作...

before_save :create_normalized_strings

def create_normalized_strings
  self.handicap_ascii = handicap.downcase.mb_chars.normalize(:kd).gsub(/[^x00-\x7F]/n, '').to_s
  self.animaux_ascii = animaux.downcase.mb_chars.normalize(:kd).gsub(/[^x00-\x7F]/n, '').to_s
  # etc etc
end

然后在你的搜索中做...

if handicap
  test_handicap = handicap.downcase.mb_chars.normalize(:kd).gsub(/[^x00-\x7F]/n, '').to_s
  result = result.where('handicap_ascii LIKE ?', "%#{handicap}%")
end

它并不好,因为它基本上会迫使您将数据库中的数据复制到额外的列中。如果您可以考虑除SQLite之外的更复杂的数据库,那么您将会更好......我个人不会在生产环境中使用SQLite。

相关问题