<commit_before>require 'logger'
require_relative '../../classes/elastic_search'

module Commands
  class ElasticSearchIndexForTextSearch
    include Pavlov::Command

    arguments :object

    def execute
      @missing_fields = []
      @document = {}

      @logger = pavlov_options[:logger] || Logger.new(STDERR)

      define_index

      @missing_fields << :id unless field_exists :id

      raise 'Type_name is not set.' unless @type_name

      raise "#{@type_name} missing fields (#{@missing_fields})." unless @missing_fields.count == 0

      index = ElasticSearch::Index.new @type_name
      index.add object.id, json_document
    end

    def json_document
      @document.to_json
    end

    private
    def type type_name
      @type_name = type_name
    end

    def field_exists name
      object.respond_to? name
    end

    def field name
      if field_exists name
        @document[name] = object.send name
      else
        @missing_fields << name
      end
    end
  end
end<commit_msg>Inlined document.to_json<commit_after>require 'logger'
require_relative '../../classes/elastic_search'

module Commands
  class ElasticSearchIndexForTextSearch
    include Pavlov::Command

    arguments :object

    def execute
      @missing_fields = []
      @document = {}

      @logger = pavlov_options[:logger] || Logger.new(STDERR)

      define_index

      @missing_fields << :id unless field_exists :id

      raise 'Type_name is not set.' unless @type_name

      raise "#{@type_name} missing fields (#{@missing_fields})." unless @missing_fields.count == 0

      index = ElasticSearch::Index.new @type_name
      index.add object.id, document.to_json
    end

    private
    def type type_name
      @type_name = type_name
    end

    def field_exists name
      object.respond_to? name
    end

    def field name
      if field_exists name
        @document[name] = object.send name
      else
        @missing_fields << name
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08388074785245073, 'fraction_numerical': 0.001010611419909045, 'mean_word_length': 2.6, 'pattern_counts': {'":': 0, '<': 11, '<?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': '4824696', 'n_tokens_mistral': 599, 'n_tokens_neox': 569, 'n_words': 156}