<commit_before>require 'gds_api/rummager'

module LegacyTaxonomy
  module Client
    class SearchApi
      class << self
        def content_tagged_to_browse_page(taxon_content_id)
          content_from_rummager(
            filter_mainstream_browse_page_content_ids: [taxon_content_id]
          )
        end

        def content_tagged_to_policy_area(policy_area_slug)
          content_from_rummager(filter_policy_areas: [policy_area_slug])
        end

        def content_tagged_to_policy(policy_slug)
          content_from_rummager(filter_policies: policy_slug)
        end

        def policy_areas
          areas = client.search(
            filter_format: 'topic',
            count: 1000
          )

          areas['results']
        end

        def content_from_rummager(query_params)
          results = []
          count = 1000
          start = 0
          fields = %w(content_id link)

          query = proc do |start_from, content_count|
            client
              .search(
                query_params.merge(
                  fields: fields,
                  start: start_from,
                  count: content_count
                )
              )
              .dig('results')
              .map { |result| result.slice(*fields) }
              .compact
          end

          loop do
            things = query.call(start, count)
            results += things
            start += count
            break if things.size.between?(0, count - 1)
          end

          results
        end

        def client
          @search ||= GdsApi::Rummager.new(
            Plek.new.find('rummager'),
            timeout: 20
          )
        end
      end
    end
  end
end
<commit_msg>Make sure we have a content_id<commit_after>require 'gds_api/rummager'

module LegacyTaxonomy
  module Client
    class SearchApi
      class << self
        def content_tagged_to_browse_page(taxon_content_id)
          content_from_rummager(
            filter_mainstream_browse_page_content_ids: [taxon_content_id]
          )
        end

        def content_tagged_to_policy_area(policy_area_slug)
          content_from_rummager(filter_policy_areas: [policy_area_slug])
        end

        def content_tagged_to_policy(policy_slug)
          content_from_rummager(filter_policies: policy_slug)
        end

        def policy_areas
          areas = client.search(
            filter_format: 'topic',
            count: 1000
          )

          areas['results']
        end

        def content_from_rummager(query_params)
          results = []
          count = 1000
          start = 0
          fields = %w(content_id link)

          query = proc do |start_from, content_count|
            client
              .search(
                query_params.merge(
                  fields: fields,
                  start: start_from,
                  count: content_count
                )
              )
              .dig('results')
              .map { |result| result.slice(*fields) }
              .delete_if { |_, value| value.empty? }
          end

          loop do
            things = query.call(start, count)
            results += things
            start += count
            break if things.size.between?(0, count - 1)
          end

          results
        end

        def client
          @search ||= GdsApi::Rummager.new(
            Plek.new.find('rummager'),
            timeout: 20
          )
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.06739317464869515, 'fraction_numerical': 0.007456266131344996, 'mean_word_length': 1.4155124653739612, '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': '3742322', 'n_tokens_mistral': 1005, 'n_tokens_neox': 957, 'n_words': 197}