<commit_before>require 'whitehall/document_filter/filterer'

module Whitehall::DocumentFilter
  class Rummager < Filterer
    def announcements_search
      filter_args = standard_filter_args.merge(filter_by_announcement_type)
      @results = Whitehall.government_search_client.advanced_search(filter_args)
    end

    def publications_search
      filter_args = standard_filter_args.merge(filter_by_publication_type)
      @results = Whitehall.government_search_client.advanced_search(filter_args)
    end

    def policies_search
      filter_args = standard_filter_args.merge(search_format_types: [Policy.search_format_type])
      @results = Whitehall.government_search_client.advanced_search(filter_args)
    end

    def default_filter_args
      @default = {
        page: @page.to_s,
        per_page: @per_page.to_s
      }
    end

    def standard_filter_args
      default_filter_args
        .merge(filter_by_keywords)
        .merge(filter_by_relevance_to_local_government)
        .merge(filter_by_people)
        .merge(filter_by_topics)
        .merge(filter_by_organisations)
        .merge(filter_by_locations)
        .merge(filter_by_date)
        .merge(sort)
    end

    def filter_by_keywords
      if @keywords.present?
        {keywords: @keywords.to_s}
      else
        {}
      end
    end

    def filter_by_relevance_to_local_government
      if relevant_to_local_government
        {relevant_to_local_government: "1"}
      else
        {}
      end
    end

    def filter_by_people
      if @people_ids.present? && @people_ids != ["all"]
        {people: @people.map(&:slug)}
      else
        {}
      end
    end

    def filter_by_topics
      if selected_topics.any?
        {topics: selected_topics.map(&:slug)}
      else
        {}
      end
    end

    def filter_by_organisations
      if selected_organisations.any?
        {organisations: selected_organisations.map(&:slug)}
      else
        {}
      end
    end

    def filter_by_locations
      if selected_locations.any?
        {world_locations: selected_locations.map(&:slug)}
      else
        {}
      end
    end

    def filter_by_date
      dates_hash = {}
      if @form_date.present?
        dates_hash.merge(from: @from_date)
      end
      if @to_date.present?
        dates_hash.merge(to: @to_date)
      end
      if dates_hash.empty?
        {}
      else
        {public_timestamp: dates_hash}
      end
    end

    def sort
      if (@form_date.present? || @to_date.present?) && @keywords.blank?
        {order: { public_timestamp: "desc" } }
      else
        {}
      end
    end

    def filter_by_announcement_type
      announcement_types =
        if selected_announcement_type_option
          selected_announcement_type_option.search_format_types
        elsif include_world_location_news
          [Announcement.search_format_type]<commit_msg>Apply date ordering unless keywords are blank<commit_after>require 'whitehall/document_filter/filterer'

module Whitehall::DocumentFilter
  class Rummager < Filterer
    def announcements_search
      filter_args = standard_filter_args.merge(filter_by_announcement_type)
      @results = Whitehall.government_search_client.advanced_search(filter_args)
    end

    def publications_search
      filter_args = standard_filter_args.merge(filter_by_publication_type)
      @results = Whitehall.government_search_client.advanced_search(filter_args)
    end

    def policies_search
      filter_args = standard_filter_args.merge(search_format_types: [Policy.search_format_type])
      @results = Whitehall.government_search_client.advanced_search(filter_args)
    end

    def default_filter_args
      @default = {
        page: @page.to_s,
        per_page: @per_page.to_s
      }
    end

    def standard_filter_args
      default_filter_args
        .merge(filter_by_keywords)
        .merge(filter_by_relevance_to_local_government)
        .merge(filter_by_people)
        .merge(filter_by_topics)
        .merge(filter_by_organisations)
        .merge(filter_by_locations)
        .merge(filter_by_date)
        .merge(sort)
    end

    def filter_by_keywords
      if @keywords.present?
        {keywords: @keywords.to_s}
      else
        {}
      end
    end

    def filter_by_relevance_to_local_government
      if relevant_to_local_government
        {relevant_to_local_government: "1"}
      else
        {}
      end
    end

    def filter_by_people
      if @people_ids.present? && @people_ids != ["all"]
        {people: @people.map(&:slug)}
      else
        {}
      end
    end

    def filter_by_topics
      if selected_topics.any?
        {topics: selected_topics.map(&:slug)}
      else
        {}
      end
    end

    def filter_by_organisations
      if selected_organisations.any?
        {organisations: selected_organisations.map(&:slug)}
      else
        {}
      end
    end

    def filter_by_locations
      if selected_locations.any?
        {world_locations: selected_locations.map(&:slug)}
      else
        {}
      end
    end

    def filter_by_date
      dates_hash = {}
      if @form_date.present?
        dates_hash.merge(from: @from_date)
      end
      if @to_date.present?
        dates_hash.merge(to: @to_date)
      end
      if dates_hash.empty?
        {}
      else
        {public_timestamp: dates_hash}
      end
    end

    def sort
      if @keywords.blank?
        {order: { public_timestamp: "desc" } }
      else
        {}
      end
    end

    def filter_by_announcement_type
      announcement_types =
        if selected_announcement_type_option
          selected_announcement_type_option.search_format_types
        elsif include_world_location_news
          [Announcement.search_format_type]
{'fraction_non_alphanumeric': 0.07137883008356546, 'fraction_numerical': 0.00034818941504178273, 'mean_word_length': 2.4463107378524294, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3093943', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1751, 'n_words': 295}