pastebinner/lib/elastic_search_helper.rb

69 lines
2 KiB
Ruby

class ElasticSearchHelper
attr_accessor :server_uri, :index, :pastebinner, :doctype
DEFAULT_METHOD = :post
def initialize(server_uri, index, doctype='_doc')
@server_uri = server_uri
@index = index
@doctype = doctype
@pastebinner = Pastebinner.new(ENV['pastebin_api_key'], ENV['pastebin_username'], ENV['pastebin_password'])
end
def create_index
header = { 'Content-type': 'application/json' }
response = RestClient::Request.execute(
method: :put,
url: "#{server_uri}/#{index}",
headers: header,
payload: self.mappings.to_json)
end
def mappings
{
"mappings": {
"_doc": {
"properties": {
"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" }
},
"paste_text": { "type": "text" }
}
}
}
}
end
def update_mapping(mapping_json)
header = { 'Content-type': 'application/json' }
response = RestClient::Request.execute(
method: :put,
url: "#{server_uri}/#{index}/_mapping/#{doctype}",
payload: mapping_json,
headers: header)
end
def json_to_es(paste_json, method=nil)
header = { 'Content-type': 'application/json' }
response = RestClient::Request.execute(
method: method ||= DEFAULT_METHOD,
url: "#{server_uri}/#{index}/#{doctype}",
headers: header,
payload: paste_json)
end
def json_to_es_bulk(array_of_paste_json)
array_of_paste_json.each do |paste_json|
self.json_to_es(paste_json)
end
end
end