<commit_before>namespace :gremlin do
  desc 'Creates indices for the CER entities in the Gremlin database.'
  task create_indices: :cer_environment do
    require 'notify_gremlin_indexer'
    NotifyGremlinIndexer.create_indices
  end

  desc 'Reindexes the envelope database in Gremlin.'
  task index_all: :cer_environment do
    require 'notify_gremlin_indexer'
    MR.redis_pool.with(&:flushall)
    ids = Envelope.not_deleted
                  .with_graph
                  .ordered_by_date
                  .pluck(:id)
                  .reverse
    ids.each { |id| NotifyGremlinIndexer.index_one(id) }
  end

  desc "Attempts to reindex documents that haven't been indexed yet."
  task index_not_indexed: :cer_environment do
    require 'notify_gremlin_indexer'
    ids = Envelope.not_deleted
                  .with_graph
                  .where(last_graph_indexed_at: nil)
                  .ordered_by_date
                  .pluck(:id)
                  .reverse
    ids.each { |id| NotifyGremlinIndexer.index_one(id) }
  end

  desc 'Updates the JSON context specs used to inform indexing.'
  task update_contexts: :environment do
    require 'json_context'
    urls = Envelope.select("distinct processed_resource->>'@context' as url").map(&:url)
    urls.each do |url|
      next if url.blank?
      puts "Updating context for #{url}."
      context = JSON.parse(RestClient.get(url).body)
      JsonContext.find_or_initialize_by(url: url).tap do |ctx|
        ctx.context = context
        ctx.save!
        puts 'Updated.'
      end
    end
    NotifyGremlinIndexer.update_contexts
  end
end
<commit_msg>Add rake task for rebuilding relationships<commit_after>namespace :gremlin do
  desc 'Creates indices for the CER entities in the Gremlin database.'
  task create_indices: :cer_environment do
    require 'notify_gremlin_indexer'
    NotifyGremlinIndexer.create_indices
  end

  desc 'Reindexes the envelope database in Gremlin.'
  task index_all: :cer_environment do
    require 'notify_gremlin_indexer'
    NotifyGremlinIndexer.index_all
  end

  desc 'Reindexes the envelope relationships in Gremlin.'
  task build_relationships: :cer_environment do
    require 'notify_gremlin_indexer'
    NotifyGremlinIndexer.build_relationships
  end

  desc "Attempts to reindex documents that haven't been indexed yet."
  task index_not_indexed: :cer_environment do
    require 'notify_gremlin_indexer'
    ids = Envelope.not_deleted
                  .with_graph
                  .where(last_graph_indexed_at: nil)
                  .ordered_by_date
                  .pluck(:id)
                  .reverse
    ids.each { |id| NotifyGremlinIndexer.index_one(id) }
  end

  desc 'Updates the JSON context specs used to inform indexing.'
  task update_contexts: :environment do
    require 'json_context'
    urls = Envelope.select("distinct processed_resource->>'@context' as url").map(&:url)
    urls.each do |url|
      next if url.blank?
      puts "Updating context for #{url}."
      context = JSON.parse(RestClient.get(url).body)
      JsonContext.find_or_initialize_by(url: url).tap do |ctx|
        ctx.context = context
        ctx.save!
        puts 'Updated.'
      end
    end
    NotifyGremlinIndexer.update_contexts
  end
end

{'fraction_non_alphanumeric': 0.07507692307692308, 'fraction_numerical': 0.0, 'mean_word_length': 2.728211009174312, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2935023', 'n_tokens_mistral': 1023, 'n_tokens_neox': 964, 'n_words': 255}