2018-11-18 06:04:51 -06:00
|
|
|
class ElasticSearchHelper
|
2019-02-01 22:19:36 -06:00
|
|
|
attr_accessor :server_uri, :index, :pastebinner, :doctype
|
|
|
|
DEFAULT_METHOD = :post
|
|
|
|
|
|
|
|
def initialize(server_uri, index, doctype='_doc')
|
2018-11-18 06:04:51 -06:00
|
|
|
@server_uri = server_uri
|
|
|
|
@index = index
|
2019-02-01 22:19:36 -06:00
|
|
|
@doctype = doctype
|
|
|
|
@pastebinner = Pastebinner.new(ENV['pastebin_api_key'], ENV['pastebin_username'], ENV['pastebin_password'])
|
2018-11-18 06:04:51 -06:00
|
|
|
end
|
|
|
|
|
2019-02-01 22:19:36 -06:00
|
|
|
def create_index
|
|
|
|
response = RestClient::Request.execute(
|
|
|
|
method: :put,
|
|
|
|
url: "#{server_uri}/#{index}")
|
2018-11-18 06:04:51 -06:00
|
|
|
end
|
2019-02-01 22:19:36 -06:00
|
|
|
|
|
|
|
def json_to_es(paste_json, method=nil)
|
2018-11-18 06:04:51 -06:00
|
|
|
header = { 'Content-type': 'application/json' }
|
|
|
|
response = RestClient::Request.execute(
|
2019-02-01 22:19:36 -06:00
|
|
|
method: method ||= DEFAULT_METHOD,
|
|
|
|
url: "#{server_uri}/#{index}/#{doctype}",
|
2018-11-18 06:04:51 -06:00
|
|
|
headers: header,
|
2019-02-01 22:19:36 -06:00
|
|
|
payload: paste_json)
|
2018-11-18 06:04:51 -06:00
|
|
|
end
|
|
|
|
|
2019-02-01 22:19:36 -06:00
|
|
|
def json_to_es_bulk(array_of_paste_json)
|
|
|
|
array_of_paste_json.each do |paste_json|
|
|
|
|
self.to_es(paste_json)
|
|
|
|
end
|
2018-11-18 06:04:51 -06:00
|
|
|
end
|
2019-02-01 22:19:36 -06:00
|
|
|
|
2018-11-18 06:04:51 -06:00
|
|
|
end
|