Skip to content

Commit

Permalink
Add CSV Bulk Index Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunihiko Kido committed May 19, 2015
1 parent 47043c6 commit 91c5e2a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,7 @@
{"command": "dumpdata", "caption": "Elasticsearch: Dump Data"},
{"command": "loaddata", "caption": "Elasticsearch: Load Data"},
{"command": "copy_index", "caption": "Elasticsearch: Copy Data from ..."},

{"command": "csv_convert_bulk_format", "caption": "Elasticsearch: CSV Convert to Bulk Index format"},
{"command": "csv_bulk_index", "caption": "Elasticsearch: CSV Bulk Index"},
]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ open the Command Palette (``Shift + Command + P``) and enter ``Elasticsearch ...
* Elasticsearch: Dump Data
* Elasticsearch: Load Data
* Elasticsearch: Copy Data from ...
* Elasticsearch: CSV Convert to Bulk Index format
* Elasticsearch: CSV Bulk Index

### Command for Sublime User Settings

Expand Down
40 changes: 40 additions & 0 deletions convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sublime
import sublime_plugin
import csv
import io
import json


class CsvConvertBulkFormatCommand(sublime_plugin.TextCommand):

def new_file(self, title=''):
view = sublime.active_window().new_file()
view.set_name('**Bulk {} Data**'.format(title))
view.set_scratch(True)
view.set_syntax_file('Packages/JavaScript/JSON.tmLanguage')
return view

def run(self, edit, action='index'):

text = self.view.substr(sublime.Region(0, self.view.size()))

output_view = self.new_file(action)

for doc in csv.DictReader(io.StringIO(text.strip())):
bulk_action = {action: {}}

if 'id' in doc:
bulk_action[action]['_id'] = doc['id']

data = []
data.append(json.dumps(bulk_action))

if action in ('index', 'create'):
data.append(json.dumps(doc, ensure_ascii=False))
elif action == 'update':
data.append(json.dumps({'doc': doc}, ensure_ascii=False))

output_text = '\n'.join(data) + '\n'

output_view.insert(
edit, output_view.size(), output_text)
7 changes: 7 additions & 0 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,10 @@ def on_done(self, index):

def run(self):
self.show_select_inputfiles(self.on_done)


class CsvBulkIndexCommand(HelperBaseCommand):

def run(self):
self.window.run_command('csv_convert_bulk_format')
self.window.run_command('bulk')
3 changes: 2 additions & 1 deletion indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ class PutIndexSettingsCommand(IndicesClientCommand):
result_window_title = "Put Index Settings"

def run(self):
self.request_indices_api('put_settings', body=self.selection())
self.request_indices_api(
'put_settings', index=self.index, body=self.selection())


class PutIndexWarmerCommand(IndicesClientCommand):
Expand Down

0 comments on commit 91c5e2a

Please sign in to comment.