85 lines
2.3 KiB
Ruby
85 lines
2.3 KiB
Ruby
class ElasticSearchHelper
|
|
attr_accessor :server_uri, :index, :pastebinner, :doctype
|
|
DEFAULT_METHOD = :post
|
|
|
|
def initialize(server_uri, index, doctype='paste')
|
|
@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_with_mappings
|
|
header = { 'Content-type': 'application/json' }
|
|
response = RestClient::Request.execute(
|
|
method: :put,
|
|
url: "#{server_uri}/#{index}",
|
|
headers: header,
|
|
payload: self.mappings.to_json)
|
|
end
|
|
|
|
def delete_index
|
|
response = RestClient::Request.execute(
|
|
method: :delete,
|
|
url: "#{server_uri}/#{index}")
|
|
end
|
|
|
|
def get_mappings
|
|
response = RestClient::Request.execute(
|
|
method: :get,
|
|
url: "#{server_uri}/#{index}/_mappings")
|
|
end
|
|
|
|
|
|
def mappings
|
|
{
|
|
"mappings": {
|
|
"paste": {
|
|
"properties": {
|
|
"paste_metadata": {
|
|
"type": "nested",
|
|
"properties": {
|
|
"scrape_url": { "type": "text" },
|
|
"full_url": { "type": "text" },
|
|
"date": { "type": "text" },
|
|
"key": { "type": "text" },
|
|
"size": { "type": "text" },
|
|
"expire": { "type": "text" },
|
|
"title": { "type": "text" },
|
|
"syntax": { "type": "text" },
|
|
"user": { "type": "text" },
|
|
"hits": { "type": "integer" }
|
|
}
|
|
},
|
|
"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
|