<commit_before>class DocumentSearch
  include CacheIterator
  include SearchCache # this provides #cached_results and #cached_total_cnt
  attr_reader :page, :per_page, :offset, :event_type, :event_id,
    :document_type, :document_title

  def initialize(options, interface)
    initialize_params(options)
    initialize_query interface
  end

  def results
    if @interface == 'admin'
      results = @query.limit(@per_page).
        offset(@offset)
    else
      results = select_and_group_query
    end
  end

  def total_cnt
    @query.count
  end

  def taxon_concepts
    @taxon_concepts ||= TaxonConcept.where(id: @taxon_concepts_ids)
  end

  def geo_entities
    @geo_entities ||= GeoEntity.where(id: @geo_entities_ids)
  end

  def document_tags
    @document_tags ||= DocumentTag.where(id: @document_tags_ids)
  end

  private

  def initialize_params(options)
    @options = DocumentSearchParams.sanitize(options)
    @options.keys.each { |k| instance_variable_set("@#{k}", @options[k]) }
    @offset = @per_page * (@page - 1)
  end

  def initialize_query interface
    @interface = interface
    view = interface == 'admin' ? 'documents_view' : 'api_documents_view'
    @query = Document.from("#{view} AS documents")
    add_conditions_for_event
    add_conditions_for_document
    add_proposal_outcome_condition if @proposal_outcome_ids.present?
    add_review_phase_condition if @review_phase_ids.present?
    add_extra_conditions
    add_ordering if interface == 'admin'
  end

  def add_conditions_for_event
    return unless @event_id || @event_type
    @query = @query.joins('LEFT JOIN events ON events.id = documents.event_id')
    if @event_id.present?
      @query = @query.where(event_id: @event_id)
    elsif @event_type.present?
      @query = @query.where('events.type' => @event_type)
    end
  end

  def add_conditions_for_document
    @query = @query.search_by_title(@document_title) if @document_title.present?

    if @document_type.present?
      @query = @query.where('documents.type' => @document_type)
    end

    if @interface == 'admin'
      if !@document_date_start.blank?
        @query = @query.where("documents.date >= ?", @document_date_start)
      end
      if !@document_date_end.blank?
        @query = @query.where("documents.date <= ?", @document_date_end)
      end
    end
  end

  def add_extra_conditions
    add_taxon_concepts_condition if @taxon_concepts_ids.present?
    add_geo_entities_condition if @geo_entities_ids.present?
    add_document_tags_condition if @document_tags_ids.present?
  end

  def add_taxon_concepts_condition
    @query = @query.where("taxon_concept_ids && ARRAY[#{@taxon_concepts_ids.join(',')}]")
  end

  def add_geo_entities_condition
    @query = @query.where("geo_entity_ids && ARRAY[#{@geo_entities_ids.join(',')}]")
  end

  def add_document_tags_condition
    @query = @query.where("document_tags_ids && ARRAY[#{@document_tags_ids.join(',')}]")
  end

  def add_proposal_outcome_condition
    @query = @query.where("proposal_outcome_ids && ARRAY[#{@proposal_outcome_ids.join(',')}]")

  end

  def add_review_phase_condition
    @query = @query.where("review_phase_ids && ARRAY[#{@review_phase_ids.join(',')}]")
  end

  def add_ordering
    return if @document_title.present?

    @query = if @event_id.present?
      @query.order([:date, :title])
    else
      @query.order('documents.created_at DESC')
    end
  end

  def select_and_group_query
    columns = "event_name, event_date, event_type, is_public, document_type,<commit_msg>Fixed download bug caused by missing id column<commit_after>class DocumentSearch
  include CacheIterator
  include SearchCache # this provides #cached_results and #cached_total_cnt
  attr_reader :page, :per_page, :offset, :event_type, :event_id,
    :document_type, :document_title

  def initialize(options, interface)
    initialize_params(options)
    initialize_query interface
  end

  def results
    if @interface == 'admin'
      results = @query.limit(@per_page).
        offset(@offset)
    else
      results = select_and_group_query
    end
  end

  def total_cnt
    @query.count
  end

  def taxon_concepts
    @taxon_concepts ||= TaxonConcept.where(id: @taxon_concepts_ids)
  end

  def geo_entities
    @geo_entities ||= GeoEntity.where(id: @geo_entities_ids)
  end

  def document_tags
    @document_tags ||= DocumentTag.where(id: @document_tags_ids)
  end

  private

  def initialize_params(options)
    @options = DocumentSearchParams.sanitize(options)
    @options.keys.each { |k| instance_variable_set("@#{k}", @options[k]) }
    @offset = @per_page * (@page - 1)
  end

  def initialize_query interface
    @interface = interface
    view = interface == 'admin' ? 'documents_view' : 'api_documents_view'
    @query = Document.from("#{view} AS documents")
    add_conditions_for_event
    add_conditions_for_document
    add_proposal_outcome_condition if @proposal_outcome_ids.present?
    add_review_phase_condition if @review_phase_ids.present?
    add_extra_conditions
    add_ordering if interface == 'admin'
  end

  def add_conditions_for_event
    return unless @event_id || @event_type
    @query = @query.joins('LEFT JOIN events ON events.id = documents.event_id')
    if @event_id.present?
      @query = @query.where(event_id: @event_id)
    elsif @event_type.present?
      @query = @query.where('events.type' => @event_type)
    end
  end

  def add_conditions_for_document
    @query = @query.search_by_title(@document_title) if @document_title.present?

    if @document_type.present?
      @query = @query.where('documents.type' => @document_type)
    end

    if @interface == 'admin'
      if !@document_date_start.blank?
        @query = @query.where("documents.date >= ?", @document_date_start)
      end
      if !@document_date_end.blank?
        @query = @query.where("documents.date <= ?", @document_date_end)
      end
    end
  end

  def add_extra_conditions
    add_taxon_concepts_condition if @taxon_concepts_ids.present?
    add_geo_entities_condition if @geo_entities_ids.present?
    add_document_tags_condition if @document_tags_ids.present?
  end

  def add_taxon_concepts_condition
    @query = @query.where("taxon_concept_ids && ARRAY[#{@taxon_concepts_ids.join(',')}]")
  end

  def add_geo_entities_condition
    @query = @query.where("geo_entity_ids && ARRAY[#{@geo_entities_ids.join(',')}]")
  end

  def add_document_tags_condition
    @query = @query.where("document_tags_ids && ARRAY[#{@document_tags_ids.join(',')}]")
  end

  def add_proposal_outcome_condition
    @query = @query.where("proposal_outcome_ids && ARRAY[#{@proposal_outcome_ids.join(',')}]")

  end

  def add_review_phase_condition
    @query = @query.where("review_phase_ids && ARRAY[#{@review_phase_ids.join(',')}]")
  end

  def add_ordering
    return if @document_title.present?

    @query = if @event_id.present?
      @query.order([:date, :title])
    else
      @query.order('documents.created_at DESC')
    end
  end

  def select_and_group_query
    columns = "id, event_name, event_date, event_type, is_public, document_type,
{'fraction_non_alphanumeric': 0.11082946063934657, 'fraction_numerical': 0.0002816504717645402, 'mean_word_length': 4.1130309575233985, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1527967', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2359, 'n_words': 451}