diff --git a/Lib/elasticsearch/__init__.py b/Lib/elasticsearch/__init__.py index 68749b7..fb7feab 100644 --- a/Lib/elasticsearch/__init__.py +++ b/Lib/elasticsearch/__init__.py @@ -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): @@ -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( @@ -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' diff --git a/Lib/elasticsearch/utils.py b/Lib/elasticsearch/utils.py index cfc5792..b8081ed 100644 --- a/Lib/elasticsearch/utils.py +++ b/Lib/elasticsearch/utils.py @@ -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]) @@ -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' diff --git a/base.py b/base.py index 2b626b4..2bf2e3a 100644 --- a/base.py +++ b/base.py @@ -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) diff --git a/docs.py b/docs.py index 54b8995..13c6da5 100644 --- a/docs.py +++ b/docs.py @@ -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) @@ -57,8 +59,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_source, self.index, self.doc_type, id) @@ -66,9 +70,16 @@ 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) @@ -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) @@ -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): @@ -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): @@ -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() @@ -162,4 +175,3 @@ def run(self): es = self.ESClient() body = self.selection() self.request(es.mtermvectors, self.index, self.doc_type, body=body) -