<commit_before>module Stretcher
  # Represents an index  scoped to a specific type.
  # Generally should be instantiated via Index#type(name).
  class IndexType < EsComponent
    attr_reader :server, :index, :name, :logger

    def initialize(index, name, options={})
      @index = index
      @server = index.server
      @name = name
      @logger = options[:logger] || index.logger
    end

    # Retrieves the document by ID
    # Normally this returns the contents of _source, however, the 'raw' flag is passed in, it will return the full response hash
    # Returns nil if the document does not exist
    def get(id, options={}, raw=false)
      if options == true # Support raw as second argument, legacy API
        raw = true
        options = {}
      end
      res = request(:get, id, options)
      raw ? res : res["_source"]
    end

    # Explains a query for a specific document
    def explain(id, query)
      request(:get, "#{id}/_explain") do |req|
        req.body = query
      end
    end

    # Index an item with a specific ID
    def put(id, source)
      request(:put, id, source)
    end

    # Index an item with automatic ID generation
    def post(source)
      request(:post, nil, source)
    end

    # Uses the update api to modify a document with a script
    # To update a doc with ID 987 for example:<commit_msg>Slightly more rigorous check for legacy 'raw' argument to IndexType#update<commit_after>module Stretcher
  # Represents an index  scoped to a specific type.
  # Generally should be instantiated via Index#type(name).
  class IndexType < EsComponent
    attr_reader :server, :index, :name, :logger

    def initialize(index, name, options={})
      @index = index
      @server = index.server
      @name = name
      @logger = options[:logger] || index.logger
    end

    # Retrieves the document by ID
    # Normally this returns the contents of _source, however, the 'raw' flag is passed in, it will return the full response hash
    # Returns nil if the document does not exist
    def get(id, options={}, raw=false)
      if options == true || options == false # Support raw as second argument, legacy API
        raw = true
        options = {}
      end
      res = request(:get, id, options)
      raw ? res : res["_source"]
    end

    # Explains a query for a specific document
    def explain(id, query)
      request(:get, "#{id}/_explain") do |req|
        req.body = query
      end
    end

    # Index an item with a specific ID
    def put(id, source)
      request(:put, id, source)
    end

    # Index an item with automatic ID generation
    def post(source)
      request(:post, nil, source)
    end

    # Uses the update api to modify a document with a script
    # To update a doc with ID 987 for example:
{'fraction_non_alphanumeric': 0.08456279237135661, 'fraction_numerical': 0.0021590500179920835, 'mean_word_length': 2.6103896103896105, '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': '212981', 'n_tokens_mistral': 794, 'n_tokens_neox': 769, 'n_words': 336}