用于Elasticsearch的查询字符串中的符号

用于Elasticsearch的查询字符串中的符号,第1张

用于Elasticsearch的查询字符串中的符号

您可以清理查询字符串。这是一种消毒剂,适用于我尝试扔给它的所有东西:

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})bs*/, " #{escaped_word} ")  end  # Escape odd quotes  quote_count = str.count '"'  str = str.gsub(/(.*)"(.*)/, '1"3') if quote_count % 2 == 1  strendparams[:query] = sanitize_string_for_elasticsearch_string_query(params[:query])


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4905463.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-12
下一篇2022-11-12

发表评论

登录后才能评论

评论列表(0条)

    保存