Skip to content

Commit

Permalink
Update Commands for document apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunihiko Kido committed May 7, 2015
1 parent e59a0d4 commit d490f98
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
7 changes: 3 additions & 4 deletions Lib/elasticsearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .utils import make_url
from .utils import make_path
from .utils import show_result_json
from .utils import serialize_body


class Elasticsearch(object):
Expand All @@ -22,9 +23,7 @@ def __init__(self, base_url='http://localhost:9200/', headers=None):

def request(self, method, path, body=None, params=None):
url = make_url(self.base_url, path, params)

if body is not None:
body = body.encode('utf-8')
body = serialize_body(body)

try:
response = requests.request(
Expand All @@ -44,7 +43,7 @@ def info(self, params=None, command=None):
def create(self, index, doc_type, body, id=None, params=None, command=None):
params = params or {}
params['op_type'] = 'create'
return self.index(command, index, doc_type, body, id, params=params)
return self.index(index, doc_type, body, id, params=params, command=command)

def index(self, index, doc_type, body, id=None, params=None, command=None):
method = 'POST' if id is not None else 'PUT'
Expand Down
12 changes: 12 additions & 0 deletions Lib/elasticsearch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

SKIP_PATH = (None, '', b'', [], ())

string_types = str, bytes


def make_path(*parts):
return '/'.join([p for p in parts if p not in SKIP_PATH])
Expand All @@ -18,6 +20,16 @@ def make_url(base_url, path, params={}):
return url


def serialize_body(body):
if isinstance(body, string_types):
return body.encode('utf-8')

if body is None:
return None

return json.dumps(body, ensure_ascii=False)


def bulk_body(body):
if not body.endswith('\n'):
body += '\n'
Expand Down
3 changes: 3 additions & 0 deletions base.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def get_node_id(self, callback):
def get_document_id(self, callback):
self.show_input_panel('Document Id: ', '1', callback)

def get_document_ids(self, callback):
self.show_input_panel('Comma-separated list of ids: ', '', callback)

def get_query(self, callback):
self.show_input_panel('Query: ', '*', callback)

Expand Down
30 changes: 21 additions & 9 deletions docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ def run(self):
self.get_document_id(self.on_done)

def on_done(self, id):
es = self.ESClient()
if not id:
return

es = self.ESClient()
self.request(es.get, self.index, self.doc_type, id)


Expand All @@ -57,18 +59,27 @@ def run(self):
self.get_document_id(self.on_done)

def on_done(self, id):
es = self.ESClient()
if not id:
return

es = self.ESClient()
self.request(es.get_source, self.index, self.doc_type, id)


class GetMultipleDocumentsCommand(ElasticsearchCommand):
result_window_title = "Multi Get Doducments"

def run(self):
es = self.ESClient()
body = self.selection()
self.get_document_ids(self.on_done)

def on_done(self, ids):
if not ids:
return

ids = [id.strip() for id in ids.split(',') if id.strip()]

es = self.ESClient()
body = dict(ids=ids)
self.request(es.mget, body, self.index, self.doc_type)


Expand All @@ -79,9 +90,11 @@ def run(self):
self.get_document_id(self.on_done)

def on_done(self, id):
if not id:
return

es = self.ESClient()
body = self.selection()

self.request(es.update, self.index, self.doc_type, id, body=body)


Expand All @@ -92,7 +105,7 @@ def run(self):
self.get_document_id(self.on_done)

def on_done(self, id):
if id is None:
if not id:
return

if not delete_ok_cancel_dialog(id):
Expand All @@ -106,7 +119,7 @@ class DeletePercolaterCommand(DeleteDocumentCommand):
result_window_title = "Delete Percolator"

def on_done(self, id):
if id is None:
if not id:
return

if not delete_ok_cancel_dialog(id):
Expand Down Expand Up @@ -147,7 +160,7 @@ def run(self):
self.get_document_id(self.on_done)

def on_done(self, id):
if id is None:
if not id:
return

es = self.ESClient()
Expand All @@ -162,4 +175,3 @@ def run(self):
es = self.ESClient()
body = self.selection()
self.request(es.mtermvectors, self.index, self.doc_type, body=body)

0 comments on commit d490f98

Please sign in to comment.