require 'elasticsearch'

class ElasticSearchHelper
  attr_accessor :server_uri, :index
  
  def initialize(server_uri, index)
    @server_uri = server_uri
    @index = index
  end

  # will build an array of 50 pastes to ship to es
  def build_json_array(pb, keys)
    json_for_es = keys.map do |k|
      pb.encode_json(pb.raw_paste_data(k), pb.raw_paste_metadata(k))
    end
  end
  
  def puts_to_es(payload, increment_num)
    header = { 'Content-type': 'application/json' }
    response = RestClient::Request.execute(
      method: :put,
      url: "#{server_uri}/#{index}/#{index}s/#{increment_num}",
      headers: header,
      payload: payload)
  end

  def metadata_mappings
  # metadata mappings
  # send a PUT
    {
      "mappings": {
        "_doc": {
          "properties": {
            "type": { "type": "keyword" },
            "paste_metadata": { "type": "nested" },
            "properties": {
              "scrape_url": { "type": "string" },
              "full_url": { "type": "string" },
              "date": { "type": "string" },
              "size": { "type": "string" },
              "expire": { "type": "string" },
              "title": { "type": "string" },
              "syntax": { "type": "string" },
              "user": { "type": "string" },
              "hits": { "type": "string" }
            }
          }
        }
      }
    }
  end

  def set_paste_text_mappings
  # paste mappings 
  # send a PUT
    {
      "mappings": {
        "_doc": {
          "properties": {
            "type": {"type": "keyword" },
            "paste_text": { "type": "text" }
          }
        }
      }
    }
  end

end