<commit_before>class CustomerSearch

  attr_accessor :c_opts, :c_query, :h_opts, :h_query, :customer_ids, :history


  def self.search(opts)
    new(opts).search
  end


  def initialize(opts)
    size    = opts.fetch(:size, 100)
    @c_opts = {q: opts.fetch(:c, ""), size: size}
    @h_opts = {q: opts.fetch(:h, ""), size: size}

    @c_query = c_opts[:q].size > 0
    @h_query = h_opts[:q].size > 0
  end<commit_msg>start support for time range queries<commit_after>class CustomerSearch

  attr_accessor :c_opts, :c_query, :h_opts, :h_query, :customer_ids, :history


  def self.search(opts)
    new(opts).search
  end


  def initialize(opts)
    size = sanitized_size(opts)

    @c_opts  = build_query_for(opts, :c, size)
    @h_opts  = build_query_for(opts, :h, size, 'now-1w')

    @c_query = !opts[:c].blank?
    @h_query = !opts[:h].blank?
  end


  def sanitized_size(opts)
    size = opts.fetch(:size, 100)
    size > 100 ? 100 : size
  end


  def build_query_for(opts, key, size, from=nil)
    {
      body: {
        query: {
          query_string: {
            query: opts.fetch(key, '')
          }
        },
        size: size
      }
    }.tap { |q|
      q[:body][:filter] = {
        range: {
          created_at: {
            from: from,
            to:  'now'
          }
        }
      } if from
    }
  end
{'fraction_non_alphanumeric': 0.14307116104868914, 'fraction_numerical': 0.011235955056179775, 'mean_word_length': 2.234866828087167, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, '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': '883484', 'n_tokens_mistral': 523, 'n_tokens_neox': 487, 'n_words': 104}