<commit_before>module ElasticQueue
  module Filters
    extend ActiveSupport::Concern

    module ClassMethods
    end

    def options_to_filters(options)
      options.map { |k, v|
        next join_options(k, v) if [:or, :and].include? k
        option_to_filter(k, v)
      }.flatten
    end

    private

    def option_to_filter(key, value)
      # return and_options(value) if key == :and
      if value.is_a? Array
        or_filter(key, value)
      elsif value.is_a? Hash
        comparison_filter(key, value)
      elsif value.nil?
        null_filter(key, value)
      else
        term_filter(key, value)
      end
    end

    def join_options(operator, options)
      j = {}
      j[operator] = options.reduce([]) { |a, option| a += options_to_filters(option) }
      j
    end

    def or_filter(term, values)<commit_msg>Bits of cleanup to support :or, :and filters directly.<commit_after>module ElasticQueue
  module Filters
    extend ActiveSupport::Concern

    module ClassMethods
    end

    def options_to_filters(options)
      options.map { |k, v| option_to_filter(k, v) }.flatten
    end

    private

    def option_to_filter(key, value)
      # return and_options(value) if key == :and
      if [:or, :and].include?(key)
        join_options(key, value)
      elsif value.is_a? Array
        or_filter(key, value)
      elsif value.is_a? Hash
        comparison_filter(key, value)
      elsif value.nil?
        # e.g. name: nil
        null_filter(key, value)
      else
        # e.g. status: 'fresh'
        term_filter(key, value)
      end
    end

    def join_options(operator, options)
      conditions = options.map { |o| options_to_filters(o) }.flatten
      { operator => conditions }
    end

    def or_filter(term, values)
{'fraction_non_alphanumeric': 0.09580498866213152, 'fraction_numerical': 0.0, 'mean_word_length': 2.4007707129094413, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1435870', 'n_tokens_mistral': 579, 'n_tokens_neox': 562, 'n_words': 146}