<commit_before>require 'activesupport'

class SolrQuery
  AND = ' AND '
  OR  = ' OR '
  
  def self.escape( str )
    str.to_s.gsub(/([!+\-\(\)\{\}\[\]^\"\~\*\?\:\\])/, '\\\\\1')
  end
  
  def self.create( operator = AND )
    solr_query = new
    yield solr_query
    solr_query.to_s( operator )
  end
  
  attr_reader :conditions
  
  def initialize
    @conditions = []
    @stream     = [ [] ]
  end
  
  def union( &block )
    do_operation( OR, &block )
  end
  
  def intersection( &block )
    do_operation( AND, &block )
  end
  
  def push( condition )
    stream.push( condition )
  end
  
  def stream
    @stream[-1]
  end
  
  def condition( field, value, args = [] )
    return if value.blank?
    escaping = !args.include?( :raw )
    
    if args.include?(:not)
      stream.push("NOT(#{field}:#{quote(escape(value, escaping))})")
    else
      stream.push("#{field}:#{quote(escape(value, escaping))}")
    end
  end
  
  def term( value, escaping = true )
    return if value.to_s.strip.blank?
    stream.push( escape(value, escaping) )
  end
  
  def like( value, escaping = true )
    "#{escape(value.downcase, escaping)}*"
  end
  
  def to_s( operator = AND )
    stream.join( operator )
  end
  
  protected
    def parenthesize( str )
      "( #{str} )"
    end
    
    def do_operation( op )
      solr_query = SolrQuery.new
      @stream    << solr_query
      yield
      generated = @stream.pop.to_s( op )
      stream.push(parenthesize(generated)) if generated.present?
    end
    
    def escape( str, escaping = true )
      if escaping
        self.class.escape( str )
      else
        str
      end
    end
    
    def quote( str )
      if str.to_s.index(' ')
        "\"#{str}\""
      elsif str.to_s.index(':')<commit_msg>added quoting for like<commit_after>require 'activesupport'

class SolrQuery
  AND = ' AND '
  OR  = ' OR '
  
  def self.escape( str )
    str.to_s.gsub(/([!+\-\(\)\{\}\[\]^\"\~\*\?\:\\])/, '\\\\\1')
  end
  
  def self.create( operator = AND )
    solr_query = new
    yield solr_query
    solr_query.to_s( operator )
  end
  
  attr_reader :conditions
  
  def initialize
    @conditions = []
    @stream     = [ [] ]
  end
  
  def union( &block )
    do_operation( OR, &block )
  end
  
  def intersection( &block )
    do_operation( AND, &block )
  end
  
  def push( condition )
    stream.push( condition )
  end
  
  def stream
    @stream[-1]
  end
  
  def condition( field, value, args = [] )
    return if value.blank?
    escaping = !args.include?( :raw )
    
    if args.include?(:not)
      stream.push("NOT(#{field}:#{quote(escape(value, escaping))})")
    else
      stream.push("#{field}:#{quote(escape(value, escaping))}")
    end
  end
  
  def term( value, escaping = true )
    return if value.to_s.strip.blank?
    stream.push( escape(value, escaping) )
  end
  
  def like( value, escaping = true )
    "#{quote(escape(value.downcase, escaping))}*"
  end
  
  def to_s( operator = AND )
    stream.join( operator )
  end
  
  protected
    def parenthesize( str )
      "( #{str} )"
    end
    
    def do_operation( op )
      solr_query = SolrQuery.new
      @stream    << solr_query
      yield
      generated = @stream.pop.to_s( op )
      stream.push(parenthesize(generated)) if generated.present?
    end
    
    def escape( str, escaping = true )
      if escaping
        self.class.escape( str )
      else
        str
      end
    end
    
    def quote( str )
      if str.to_s.index(' ')
        "\"#{str}\""
      elsif str.to_s.index(':')
{'fraction_non_alphanumeric': 0.14430665163472378, 'fraction_numerical': 0.0011273957158962795, 'mean_word_length': 2.091463414634146, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1256458', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1260, 'n_words': 292}