Symbols in query-string for elasticsearch

前端 未结 1 932
星月不相逢
星月不相逢 2021-02-20 08:42

I have \"documents\" (activerecords) with an attribute called deviations. The attribute has values like \"Bin X\" \"Bin $\" \"Bin q\" \"Bin %\" etc.

I am trying to use t

1条回答
  •  -上瘾入骨i
    2021-02-20 09:44

    You can sanitize your query string. Here is a sanitizer that works for everything that I've tried throwing at it:

    def sanitize_string_for_elasticsearch_string_query(str)
      # Escape special characters
      # http://lucene.apache.org/core/old_versioned_docs/versions/2_9_1/queryparsersyntax.html#Escaping Special Characters
      escaped_characters = Regexp.escape('\\/+-&|!(){}[]^~*?:')
      str = str.gsub(/([#{escaped_characters}])/, '\\\\\1')
    
      # AND, OR and NOT are used by lucene as logical operators. We need
      # to escape them
      ['AND', 'OR', 'NOT'].each do |word|
        escaped_word = word.split('').map {|char| "\\#{char}" }.join('')
        str = str.gsub(/\s*\b(#{word.upcase})\b\s*/, " #{escaped_word} ")
      end
    
      # Escape odd quotes
      quote_count = str.count '"'
      str = str.gsub(/(.*)"(.*)/, '\1\"\3') if quote_count % 2 == 1
    
      str
    end
    
    params[:query] = sanitize_string_for_elasticsearch_string_query(params[:query])
    

    0 讨论(0)
提交回复
热议问题