diff --git a/lib/elastic_search_helper.rb b/lib/elastic_search_helper.rb index b74b071..286dbc1 100644 --- a/lib/elastic_search_helper.rb +++ b/lib/elastic_search_helper.rb @@ -10,9 +10,46 @@ class ElasticSearchHelper end def create_index + header = { 'Content-type': 'application/json' } response = RestClient::Request.execute( method: :put, - url: "#{server_uri}/#{index}") + url: "#{server_uri}/#{index}", + header: header + payload: self.mappings.to_json) + end + + def mappings + { + "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' } + }], + "paste_text": { "type": 'string' } + } + } + } + } + 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)