忽略搜索中的大写,小写和重音符号 [英] Ignore uppercase, downcase and accent in search

查看:154
本文介绍了忽略搜索中的大写,小写和重音符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有过滤器的搜索系统>这里.该系统的工作原理很吸引人,但是我对小写/大写和重音符号有一些疑问.

I have a search system with filter here. This system work like a charm but I have some problem with downcase / uppercase and accent.

例如,如果我搜索marée",则有结果,但如果搜索"MAREE"或Marée"或"maree".我没有结果.

For example if I search "marée" I have result but if I search "MAREE" or "Marée" or "maree". I don't have result.

我想解决这个问题.我该如何解决?谢谢.

I want to fix that. How I can fix this ? Thank you.

我的控制器

    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

推荐答案

如果您坚持使用SQLite,那么您将没有很多好的选择.最常见的建议是在数据库中有多余的列,这些列是规范化的值,因此,如果您的plage列包含Marée",那么您也将有一个列plage_ascii包含"maree"

If you insist on using SQLite then you don't have many good options. The most common suggestion is to have extra columns in your database, that are normalized values so if your plage column contains "Marée" then you also have a column plage_ascii that contains "maree"

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

you need to create the additional columns with migrations, then you would have a before_save action in your model...

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

然后在您的搜索中...

Then in your search do...

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.

It's not great, as it basically forces you to duplicate data in your database into extra columns. If you can consider more sophisticated databases other than SQLite then you'd be better off... personally I wouldn't ever use SQLite in a production environment.

这篇关于忽略搜索中的大写,小写和重音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆