From 38bb0aafbdc933a9a67dfb289850d27c36de5d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Tue, 8 Dec 2015 22:19:07 +0100 Subject: [PATCH 1/3] Have transport.perform_request only return data --- elasticsearch/client/__init__.py | 91 +++++++++------------------ elasticsearch/client/cat.py | 54 ++++++---------- elasticsearch/client/cluster.py | 21 +++---- elasticsearch/client/indices.py | 105 +++++++++++-------------------- elasticsearch/client/nodes.py | 10 +-- elasticsearch/client/snapshot.py | 28 +++------ elasticsearch/transport.py | 2 +- 7 files changed, 103 insertions(+), 208 deletions(-) diff --git a/elasticsearch/client/__init__.py b/elasticsearch/client/__init__.py index b7275c93..84ea9455 100644 --- a/elasticsearch/client/__init__.py +++ b/elasticsearch/client/__init__.py @@ -216,8 +216,7 @@ class Elasticsearch(object): Get the basic info from the current cluster. ``_ """ - _, data = self.transport.perform_request('GET', '/', params=params) - return data + return self.transport.perform_request('GET', '/', params=params) @query_params('consistency', 'parent', 'refresh', 'routing', 'timeout', 'timestamp', 'ttl', 'version', 'version_type') @@ -275,9 +274,8 @@ class Elasticsearch(object): for param in (index, doc_type, body): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('POST' if id in SKIP_IN_PATH else 'PUT', + return self.transport.perform_request('POST' if id in SKIP_IN_PATH else 'PUT', _make_path(index, doc_type, id), params=params, body=body) - return data @query_params('parent', 'preference', 'realtime', 'refresh', 'routing') def exists(self, index, doc_type, id, params=None): @@ -342,9 +340,8 @@ class Elasticsearch(object): for param in (index, doc_type, id): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, id), params=params) - return data @query_params('_source', '_source_exclude', '_source_include', 'parent', 'preference', 'realtime', 'refresh', 'routing', 'version', @@ -379,9 +376,8 @@ class Elasticsearch(object): for param in (index, doc_type, id): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, id, '_source'), params=params) - return data @query_params('_source', '_source_exclude', '_source_include', 'fields', 'preference', 'realtime', 'refresh') @@ -411,9 +407,8 @@ class Elasticsearch(object): """ if body in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'body'.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_mget'), params=params, body=body) - return data @query_params('consistency', 'detect_noop', 'fields', 'lang', 'parent', 'refresh', 'retry_on_conflict', 'routing', 'script', 'script_id', @@ -456,9 +451,8 @@ class Elasticsearch(object): for param in (index, doc_type, id): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, doc_type, id, '_update'), params=params, body=body) - return data @query_params('_source', '_source_exclude', '_source_include', 'allow_no_indices', 'analyze_wildcard', 'analyzer', 'default_operator', @@ -544,9 +538,8 @@ class Elasticsearch(object): if doc_type and not index: index = '_all' - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_search'), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'local', 'preference', 'routing') @@ -575,9 +568,8 @@ class Elasticsearch(object): performed on (default: random) :arg routing: Specific routing value """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_search_shards'), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'preference', 'routing', 'scroll', 'search_type') @@ -609,9 +601,8 @@ class Elasticsearch(object): 'query_then_fetch', 'query_and_fetch', 'dfs_query_then_fetch', 'dfs_query_and_fetch', 'count', 'scan' """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_search', 'template'), params=params, body=body) - return data @query_params('_source', '_source_exclude', '_source_include', 'analyze_wildcard', 'analyzer', 'default_operator', 'df', 'fields', @@ -654,9 +645,8 @@ class Elasticsearch(object): for param in (index, doc_type, id): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, id, '_explain'), params=params, body=body) - return data @query_params('scroll') def scroll(self, scroll_id=None, body=None, params=None): @@ -676,9 +666,8 @@ class Elasticsearch(object): elif scroll_id: params['scroll_id'] = scroll_id - _, data = self.transport.perform_request('GET', '/_search/scroll', + return self.transport.perform_request('GET', '/_search/scroll', params=params, body=body) - return data @query_params() def clear_scroll(self, scroll_id=None, body=None, params=None): @@ -691,9 +680,8 @@ class Elasticsearch(object): :arg body: A comma-separated list of scroll IDs to clear if none was specified via the scroll_id parameter """ - _, data = self.transport.perform_request('DELETE', _make_path('_search', + return self.transport.perform_request('DELETE', _make_path('_search', 'scroll', scroll_id), params=params, body=body) - return data @query_params('consistency', 'parent', 'refresh', 'routing', 'timeout', 'version', 'version_type') @@ -718,9 +706,8 @@ class Elasticsearch(object): for param in (index, doc_type, id): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('DELETE', _make_path(index, + return self.transport.perform_request('DELETE', _make_path(index, doc_type, id), params=params) - return data @query_params('allow_no_indices', 'analyze_wildcard', 'analyzer', 'default_operator', 'df', 'expand_wildcards', 'ignore_unavailable', @@ -764,9 +751,8 @@ class Elasticsearch(object): if doc_type and not index: index = '_all' - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_count'), params=params, body=body) - return data @query_params('consistency', 'fields', 'refresh', 'routing', 'timeout') def bulk(self, body, index=None, doc_type=None, params=None): @@ -791,9 +777,8 @@ class Elasticsearch(object): """ if body in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'body'.") - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, doc_type, '_bulk'), params=params, body=self._bulk_body(body)) - return data @query_params('search_type') def msearch(self, body, index=None, doc_type=None, params=None): @@ -812,9 +797,8 @@ class Elasticsearch(object): """ if body in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'body'.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_msearch'), params=params, body=self._bulk_body(body)) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'preference', 'routing') @@ -842,9 +826,8 @@ class Elasticsearch(object): """ if body in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'body'.") - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_suggest'), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'percolate_format', 'percolate_index', 'percolate_preference', @@ -892,9 +875,8 @@ class Elasticsearch(object): for param in (index, doc_type): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, id, '_percolate'), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable') def mpercolate(self, body, index=None, doc_type=None, params=None): @@ -921,9 +903,8 @@ class Elasticsearch(object): """ if body in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'body'.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_mpercolate'), params=params, body=self._bulk_body(body)) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'percolate_index', 'percolate_type', 'preference', 'routing', 'version', @@ -965,9 +946,8 @@ class Elasticsearch(object): for param in (index, doc_type): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, id, '_percolate', 'count'), params=params, body=body) - return data @query_params('dfs', 'field_statistics', 'fields', 'offsets', 'parent', 'payloads', 'positions', 'preference', 'realtime', 'routing', @@ -1014,9 +994,8 @@ class Elasticsearch(object): for param in (index, doc_type): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, id, '_termvectors'), params=params, body=body) - return data @query_params('field_statistics', 'fields', 'ids', 'offsets', 'parent', 'payloads', 'positions', 'preference', 'realtime', 'routing', @@ -1067,9 +1046,8 @@ class Elasticsearch(object): :arg version_type: Specific version type, valid choices are: 'internal', 'external', 'external_gte', 'force' """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_mtermvectors'), params=params, body=body) - return data @query_params('op_type', 'version', 'version_type') def put_script(self, lang, id, body, params=None): @@ -1089,9 +1067,8 @@ class Elasticsearch(object): for param in (lang, id, body): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('PUT', _make_path('_scripts', + return self.transport.perform_request('PUT', _make_path('_scripts', lang, id), params=params, body=body) - return data @query_params('version', 'version_type') def get_script(self, lang, id, params=None): @@ -1108,9 +1085,8 @@ class Elasticsearch(object): for param in (lang, id): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('GET', _make_path('_scripts', + return self.transport.perform_request('GET', _make_path('_scripts', lang, id), params=params) - return data @query_params('version', 'version_type') def delete_script(self, lang, id, params=None): @@ -1127,9 +1103,8 @@ class Elasticsearch(object): for param in (lang, id): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('DELETE', + return self.transport.perform_request('DELETE', _make_path('_scripts', lang, id), params=params) - return data @query_params('op_type', 'version', 'version_type') def put_template(self, id, body, params=None): @@ -1148,9 +1123,8 @@ class Elasticsearch(object): for param in (id, body): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('PUT', _make_path('_search', + return self.transport.perform_request('PUT', _make_path('_search', 'template', id), params=params, body=body) - return data @query_params('version', 'version_type') def get_template(self, id, params=None): @@ -1165,9 +1139,8 @@ class Elasticsearch(object): """ if id in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'id'.") - _, data = self.transport.perform_request('GET', _make_path('_search', + return self.transport.perform_request('GET', _make_path('_search', 'template', id), params=params) - return data @query_params('version', 'version_type') def delete_template(self, id, params=None): @@ -1182,9 +1155,8 @@ class Elasticsearch(object): """ if id in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'id'.") - _, data = self.transport.perform_request('DELETE', _make_path('_search', + return self.transport.perform_request('DELETE', _make_path('_search', 'template', id), params=params) - return data @query_params('allow_no_indices', 'analyze_wildcard', 'analyzer', 'default_operator', 'df', 'expand_wildcards', 'ignore_unavailable', @@ -1261,9 +1233,8 @@ class Elasticsearch(object): level or on a cluster wide level, default 'cluster', valid choices are: 'indices', 'cluster' """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_field_stats'), params=params, body=body) - return data @query_params() def render_search_template(self, id=None, body=None, params=None): @@ -1273,7 +1244,5 @@ class Elasticsearch(object): :arg id: The id of the stored search template :arg body: The search definition template and its params """ - _, data = self.transport.perform_request('GET', _make_path('_render', + return self.transport.perform_request('GET', _make_path('_render', 'template', id), params=params, body=body) - return data - diff --git a/elasticsearch/client/cat.py b/elasticsearch/client/cat.py index 092ce300..d900c1b4 100644 --- a/elasticsearch/client/cat.py +++ b/elasticsearch/client/cat.py @@ -16,9 +16,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', _make_path('_cat', + return self.transport.perform_request('GET', _make_path('_cat', 'aliases', name), params=params) - return data @query_params('bytes', 'h', 'help', 'local', 'master_timeout', 'v') def allocation(self, node_id=None, params=None): @@ -39,9 +38,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', _make_path('_cat', + return self.transport.perform_request('GET', _make_path('_cat', 'allocation', node_id), params=params) - return data @query_params('h', 'help', 'local', 'master_timeout', 'v') def count(self, index=None, params=None): @@ -60,9 +58,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', _make_path('_cat', + return self.transport.perform_request('GET', _make_path('_cat', 'count', index), params=params) - return data @query_params('h', 'help', 'local', 'master_timeout', 'ts', 'v') def health(self, params=None): @@ -80,9 +77,8 @@ class CatClient(NamespacedClient): :arg ts: Set to false to disable timestamping, default True :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', '/_cat/health', + return self.transport.perform_request('GET', '/_cat/health', params=params) - return data @query_params('help') def help(self, params=None): @@ -92,8 +88,7 @@ class CatClient(NamespacedClient): :arg help: Return help information, default False """ - _, data = self.transport.perform_request('GET', '/_cat', params=params) - return data + return self.transport.perform_request('GET', '/_cat', params=params) @query_params('bytes', 'h', 'help', 'local', 'master_timeout', 'pri', 'v') def indices(self, index=None, params=None): @@ -115,9 +110,8 @@ class CatClient(NamespacedClient): False :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', _make_path('_cat', + return self.transport.perform_request('GET', _make_path('_cat', 'indices', index), params=params) - return data @query_params('h', 'help', 'local', 'master_timeout', 'v') def master(self, params=None): @@ -133,9 +127,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', '/_cat/master', + return self.transport.perform_request('GET', '/_cat/master', params=params) - return data @query_params('h', 'help', 'local', 'master_timeout', 'v') def nodes(self, params=None): @@ -151,9 +144,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', '/_cat/nodes', + return self.transport.perform_request('GET', '/_cat/nodes', params=params) - return data @query_params('bytes', 'h', 'help', 'master_timeout', 'v') def recovery(self, index=None, params=None): @@ -171,9 +163,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', _make_path('_cat', + return self.transport.perform_request('GET', _make_path('_cat', 'recovery', index), params=params) - return data @query_params('bytes', 'h', 'help', 'local', 'master_timeout', 'v') def shards(self, index=None, params=None): @@ -193,9 +184,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', _make_path('_cat', + return self.transport.perform_request('GET', _make_path('_cat', 'shards', index), params=params) - return data @query_params('bytes', 'h', 'help', 'v') def segments(self, index=None, params=None): @@ -211,9 +201,8 @@ class CatClient(NamespacedClient): :arg help: Return help information, default False :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', _make_path('_cat', + return self.transport.perform_request('GET', _make_path('_cat', 'segments', index), params=params) - return data @query_params('h', 'help', 'local', 'master_timeout', 'v') def pending_tasks(self, params=None): @@ -231,9 +220,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', '/_cat/pending_tasks', + return self.transport.perform_request('GET', '/_cat/pending_tasks', params=params) - return data @query_params('full_id', 'h', 'help', 'local', 'master_timeout', 'v') def thread_pool(self, params=None): @@ -250,9 +238,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', '/_cat/thread_pool', + return self.transport.perform_request('GET', '/_cat/thread_pool', params=params) - return data @query_params('bytes', 'h', 'help', 'local', 'master_timeout', 'v') def fielddata(self, fields=None, params=None): @@ -272,9 +259,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', _make_path('_cat', + return self.transport.perform_request('GET', _make_path('_cat', 'fielddata', fields), params=params) - return data @query_params('h', 'help', 'local', 'master_timeout', 'v') def plugins(self, params=None): @@ -290,9 +276,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', '/_cat/plugins', + return self.transport.perform_request('GET', '/_cat/plugins', params=params) - return data @query_params('h', 'help', 'local', 'master_timeout', 'v') def nodeattrs(self, params=None): @@ -307,9 +292,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', '/_cat/nodeattrs', + return self.transport.perform_request('GET', '/_cat/nodeattrs', params=params) - return data @query_params('h', 'help', 'local', 'master_timeout', 'v') def repositories(self, params=None): @@ -324,9 +308,8 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', '/_cat/repositories', + return self.transport.perform_request('GET', '/_cat/repositories', params=params) - return data @query_params('h', 'help', 'master_timeout', 'v') def snapshots(self, repository=None, params=None): @@ -341,6 +324,5 @@ class CatClient(NamespacedClient): node :arg v: Verbose mode. Display column headers, default False """ - _, data = self.transport.perform_request('GET', _make_path('_cat', + return self.transport.perform_request('GET', _make_path('_cat', 'snapshots', repository), params=params) - return data diff --git a/elasticsearch/client/cluster.py b/elasticsearch/client/cluster.py index 2f9e0631..ee626262 100644 --- a/elasticsearch/client/cluster.py +++ b/elasticsearch/client/cluster.py @@ -26,9 +26,8 @@ class ClusterClient(NamespacedClient): :arg wait_for_status: Wait until cluster is in a specific state, default None, valid choices are: 'green', 'yellow', 'red' """ - _, data = self.transport.perform_request('GET', _make_path('_cluster', + return self.transport.perform_request('GET', _make_path('_cluster', 'health', index), params=params) - return data @query_params('local', 'master_timeout') def pending_tasks(self, params=None): @@ -42,9 +41,8 @@ class ClusterClient(NamespacedClient): master node (default: false) :arg master_timeout: Specify timeout for connection to master """ - _, data = self.transport.perform_request('GET', + return self.transport.perform_request('GET', '/_cluster/pending_tasks', params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'flat_settings', 'ignore_unavailable', 'local', 'master_timeout') @@ -71,9 +69,8 @@ class ClusterClient(NamespacedClient): """ if index and not metric: metric = '_all' - _, data = self.transport.perform_request('GET', _make_path('_cluster', + return self.transport.perform_request('GET', _make_path('_cluster', 'state', metric, index), params=params) - return data @query_params('flat_settings', 'human', 'timeout') def stats(self, node_id=None, params=None): @@ -95,8 +92,7 @@ class ClusterClient(NamespacedClient): url = '/_cluster/stats' if node_id: url = _make_path('_cluster/stats/nodes', node_id) - _, data = self.transport.perform_request('GET', url, params=params) - return data + return self.transport.perform_request('GET', url, params=params) @query_params('dry_run', 'explain', 'master_timeout', 'metric', 'timeout') def reroute(self, body=None, params=None): @@ -116,9 +112,8 @@ class ClusterClient(NamespacedClient): 'metadata', 'nodes', 'routing_table', 'master_node', 'version' :arg timeout: Explicit operation timeout """ - _, data = self.transport.perform_request('POST', '/_cluster/reroute', + return self.transport.perform_request('POST', '/_cluster/reroute', params=params, body=body) - return data @query_params('flat_settings', 'master_timeout', 'timeout') def get_settings(self, params=None): @@ -131,9 +126,8 @@ class ClusterClient(NamespacedClient): node :arg timeout: Explicit operation timeout """ - _, data = self.transport.perform_request('GET', '/_cluster/settings', + return self.transport.perform_request('GET', '/_cluster/settings', params=params) - return data @query_params('flat_settings', 'master_timeout', 'timeout') def put_settings(self, body=None, params=None): @@ -148,7 +142,6 @@ class ClusterClient(NamespacedClient): node :arg timeout: Explicit operation timeout """ - _, data = self.transport.perform_request('PUT', '/_cluster/settings', + return self.transport.perform_request('PUT', '/_cluster/settings', params=params, body=body) - return data diff --git a/elasticsearch/client/indices.py b/elasticsearch/client/indices.py index 55dad3c2..87b9a16c 100644 --- a/elasticsearch/client/indices.py +++ b/elasticsearch/client/indices.py @@ -25,9 +25,8 @@ class IndicesClient(NamespacedClient): request body is not used) :arg tokenizer: The name of the tokenizer to use for the analysis """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_analyze'), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'force', 'ignore_unavailable', 'operation_threading') @@ -50,9 +49,8 @@ class IndicesClient(NamespacedClient): ignored when unavailable (missing or closed) :arg operation_threading: TODO: ? """ - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_refresh'), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'force', 'ignore_unavailable', 'wait_if_ongoing') @@ -82,9 +80,8 @@ class IndicesClient(NamespacedClient): to be thrown on the shard level if another flush operation is already running. """ - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_flush'), params=params) - return data @query_params('master_timeout', 'timeout', 'update_all_types') def create(self, index, body=None, params=None): @@ -101,9 +98,8 @@ class IndicesClient(NamespacedClient): """ if index in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'index'.") - _, data = self.transport.perform_request('PUT', _make_path(index), + return self.transport.perform_request('PUT', _make_path(index), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'flat_settings', 'human', 'ignore_unavailable', 'local') @@ -128,9 +124,8 @@ class IndicesClient(NamespacedClient): """ if index in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'index'.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, feature), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'master_timeout', 'timeout') @@ -153,9 +148,8 @@ class IndicesClient(NamespacedClient): """ if index in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'index'.") - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_open'), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'master_timeout', 'timeout') @@ -179,9 +173,8 @@ class IndicesClient(NamespacedClient): """ if index in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'index'.") - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_close'), params=params) - return data @query_params('master_timeout', 'timeout') def delete(self, index, params=None): @@ -196,9 +189,8 @@ class IndicesClient(NamespacedClient): """ if index in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'index'.") - _, data = self.transport.perform_request('DELETE', _make_path(index), + return self.transport.perform_request('DELETE', _make_path(index), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'local') @@ -287,9 +279,8 @@ class IndicesClient(NamespacedClient): for param in (doc_type, body): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('PUT', _make_path(index, + return self.transport.perform_request('PUT', _make_path(index, '_mapping', doc_type), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'local') @@ -311,9 +302,8 @@ class IndicesClient(NamespacedClient): :arg local: Return local information, do not retrieve the state from master node (default: false) """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_mapping', doc_type), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'include_defaults', 'local') @@ -340,9 +330,8 @@ class IndicesClient(NamespacedClient): """ if fields in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'fields'.") - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_mapping', doc_type, 'field', fields), params=params) - return data @query_params('master_timeout', 'timeout') def put_alias(self, index, name, body=None, params=None): @@ -361,9 +350,8 @@ class IndicesClient(NamespacedClient): for param in (index, name): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('PUT', _make_path(index, + return self.transport.perform_request('PUT', _make_path(index, '_alias', name), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'local') @@ -412,9 +400,8 @@ class IndicesClient(NamespacedClient): :arg local: Return local information, do not retrieve the state from master node (default: false) """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_alias', name), params=params) - return data @query_params('local', 'timeout') def get_aliases(self, index=None, name=None, params=None): @@ -428,9 +415,8 @@ class IndicesClient(NamespacedClient): master node (default: false) :arg timeout: Explicit operation timeout """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_aliases', name), params=params) - return data @query_params('master_timeout', 'timeout') def update_aliases(self, body, params=None): @@ -444,9 +430,8 @@ class IndicesClient(NamespacedClient): """ if body in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'body'.") - _, data = self.transport.perform_request('POST', '/_aliases', + return self.transport.perform_request('POST', '/_aliases', params=params, body=body) - return data @query_params('master_timeout', 'timeout') def delete_alias(self, index, name, params=None): @@ -465,9 +450,8 @@ class IndicesClient(NamespacedClient): for param in (index, name): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('DELETE', _make_path(index, + return self.transport.perform_request('DELETE', _make_path(index, '_alias', name), params=params) - return data @query_params('create', 'flat_settings', 'master_timeout', 'order', 'timeout') @@ -490,9 +474,8 @@ class IndicesClient(NamespacedClient): for param in (name, body): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('PUT', _make_path('_template', + return self.transport.perform_request('PUT', _make_path('_template', name), params=params, body=body) - return data @query_params('local', 'master_timeout') def exists_template(self, name, params=None): @@ -528,9 +511,8 @@ class IndicesClient(NamespacedClient): :arg master_timeout: Explicit operation timeout for connection to master node """ - _, data = self.transport.perform_request('GET', _make_path('_template', + return self.transport.perform_request('GET', _make_path('_template', name), params=params) - return data @query_params('master_timeout', 'timeout') def delete_template(self, name, params=None): @@ -544,9 +526,8 @@ class IndicesClient(NamespacedClient): """ if name in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'name'.") - _, data = self.transport.perform_request('DELETE', + return self.transport.perform_request('DELETE', _make_path('_template', name), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'flat_settings', 'human', 'ignore_unavailable', 'local') @@ -572,9 +553,8 @@ class IndicesClient(NamespacedClient): :arg local: Return local information, do not retrieve the state from master node (default: false) """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_settings', name), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'flat_settings', 'ignore_unavailable', 'master_timeout') @@ -599,9 +579,8 @@ class IndicesClient(NamespacedClient): """ if body in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'body'.") - _, data = self.transport.perform_request('PUT', _make_path(index, + return self.transport.perform_request('PUT', _make_path(index, '_settings'), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'master_timeout', 'request_cache') @@ -636,9 +615,8 @@ class IndicesClient(NamespacedClient): for param in (name, body): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('PUT', _make_path(index, + return self.transport.perform_request('PUT', _make_path(index, doc_type, '_warmer', name), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'local') @@ -664,9 +642,8 @@ class IndicesClient(NamespacedClient): :arg local: Return local information, do not retrieve the state from master node (default: false) """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_warmer', name), params=params) - return data @query_params('master_timeout') def delete_warmer(self, index, name, params=None): @@ -686,9 +663,8 @@ class IndicesClient(NamespacedClient): for param in (index, name): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('DELETE', _make_path(index, + return self.transport.perform_request('DELETE', _make_path(index, '_warmer', name), params=params) - return data @query_params('completion_fields', 'fielddata_fields', 'fields', 'groups', 'human', 'level', 'types') @@ -715,9 +691,8 @@ class IndicesClient(NamespacedClient): :arg types: A comma-separated list of document types for the `indexing` index metric """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_stats', metric), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'human', 'ignore_unavailable', 'operation_threading') @@ -740,9 +715,8 @@ class IndicesClient(NamespacedClient): ignored when unavailable (missing or closed) :arg operation_threading: TODO: ? """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_segments'), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'flush', 'ignore_unavailable', 'max_num_segments', 'only_expunge_deletes', @@ -772,9 +746,8 @@ class IndicesClient(NamespacedClient): :arg wait_for_merge: Specify whether the request should block until the merge process is finished (default: true) """ - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_optimize'), params=params) - return data @query_params('allow_no_indices', 'analyze_wildcard', 'analyzer', 'default_operator', 'df', 'expand_wildcards', 'explain', @@ -816,9 +789,8 @@ class IndicesClient(NamespacedClient): :arg rewrite: Provide a more detailed explanation showing the actual Lucene query that will be executed. """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, doc_type, '_validate', 'query'), params=params, body=body) - return data @query_params('allow_no_indices', 'expand_wildcards', 'field_data', 'fielddata', 'fields', 'ignore_unavailable', 'query', 'recycler', @@ -845,9 +817,8 @@ class IndicesClient(NamespacedClient): :arg recycler: Clear the recycler cache :arg request: Clear request cache """ - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_cache', 'clear'), params=params) - return data @query_params('active_only', 'detailed', 'human') def recovery(self, index=None, params=None): @@ -866,9 +837,8 @@ class IndicesClient(NamespacedClient): :arg human: Whether to return time and byte values in human-readable format., default False """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_recovery'), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'only_ancient_segments', 'wait_for_completion') @@ -892,9 +862,8 @@ class IndicesClient(NamespacedClient): :arg wait_for_completion: Specify whether the request should block until the all segments are upgraded (default: false) """ - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_upgrade'), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'human', 'ignore_unavailable') @@ -916,9 +885,8 @@ class IndicesClient(NamespacedClient): :arg ignore_unavailable: Whether specified concrete indices should be ignored when unavailable (missing or closed) """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_upgrade'), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable') def flush_synced(self, index=None, params=None): @@ -937,9 +905,8 @@ class IndicesClient(NamespacedClient): :arg ignore_unavailable: Whether specified concrete indices should be ignored when unavailable (missing or closed) """ - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_flush', 'synced'), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'operation_threading', 'status') @@ -962,9 +929,8 @@ class IndicesClient(NamespacedClient): to get store information for, valid choices are: 'green', 'yellow', 'red', 'all' """ - _, data = self.transport.perform_request('GET', _make_path(index, + return self.transport.perform_request('GET', _make_path(index, '_shard_stores'), params=params) - return data @query_params('allow_no_indices', 'expand_wildcards', 'flush', 'ignore_unavailable', 'max_num_segments', 'only_expunge_deletes', @@ -993,6 +959,5 @@ class IndicesClient(NamespacedClient): :arg wait_for_merge: Specify whether the request should block until the merge process is finished (default: true) """ - _, data = self.transport.perform_request('POST', _make_path(index, + return self.transport.perform_request('POST', _make_path(index, '_forcemerge'), params=params) - return data diff --git a/elasticsearch/client/nodes.py b/elasticsearch/client/nodes.py index b81fcf65..6adc61d1 100644 --- a/elasticsearch/client/nodes.py +++ b/elasticsearch/client/nodes.py @@ -19,9 +19,8 @@ class NodesClient(NamespacedClient): format., default False :arg timeout: Explicit operation timeout """ - _, data = self.transport.perform_request('GET', _make_path('_nodes', + return self.transport.perform_request('GET', _make_path('_nodes', node_id, metric), params=params) - return data @query_params('completion_fields', 'fielddata_fields', 'fields', 'groups', 'human', 'level', 'timeout', 'types') @@ -56,9 +55,8 @@ class NodesClient(NamespacedClient): :arg types: A comma-separated list of document types for the `indexing` index metric """ - _, data = self.transport.perform_request('GET', _make_path('_nodes', + return self.transport.perform_request('GET', _make_path('_nodes', node_id, 'stats', metric, index_metric), params=params) - return data @query_params('doc_type', 'ignore_idle_threads', 'interval', 'snapshots', 'threads', 'timeout') @@ -85,7 +83,5 @@ class NodesClient(NamespacedClient): # avoid python reserved words if params and 'type_' in params: params['type'] = params.pop('type_') - _, data = self.transport.perform_request('GET', _make_path('_cluster', + return self.transport.perform_request('GET', _make_path('_cluster', 'nodes', node_id, 'hotthreads'), params=params) - return data - diff --git a/elasticsearch/client/snapshot.py b/elasticsearch/client/snapshot.py index 321580e0..edcffb1c 100644 --- a/elasticsearch/client/snapshot.py +++ b/elasticsearch/client/snapshot.py @@ -18,9 +18,8 @@ class SnapshotClient(NamespacedClient): for param in (repository, snapshot): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('PUT', _make_path('_snapshot', + return self.transport.perform_request('PUT', _make_path('_snapshot', repository, snapshot), params=params, body=body) - return data @query_params('master_timeout') def delete(self, repository, snapshot, params=None): @@ -36,9 +35,8 @@ class SnapshotClient(NamespacedClient): for param in (repository, snapshot): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('DELETE', + return self.transport.perform_request('DELETE', _make_path('_snapshot', repository, snapshot), params=params) - return data @query_params('master_timeout') def get(self, repository, snapshot, params=None): @@ -54,9 +52,8 @@ class SnapshotClient(NamespacedClient): for param in (repository, snapshot): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('GET', _make_path('_snapshot', + return self.transport.perform_request('GET', _make_path('_snapshot', repository, snapshot), params=params) - return data @query_params('master_timeout', 'timeout') def delete_repository(self, repository, params=None): @@ -71,9 +68,8 @@ class SnapshotClient(NamespacedClient): """ if repository in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'repository'.") - _, data = self.transport.perform_request('DELETE', + return self.transport.perform_request('DELETE', _make_path('_snapshot', repository), params=params) - return data @query_params('local', 'master_timeout') def get_repository(self, repository=None, params=None): @@ -87,9 +83,8 @@ class SnapshotClient(NamespacedClient): :arg master_timeout: Explicit operation timeout for connection to master node """ - _, data = self.transport.perform_request('GET', _make_path('_snapshot', + return self.transport.perform_request('GET', _make_path('_snapshot', repository), params=params) - return data @query_params('master_timeout', 'timeout', 'verify') def create_repository(self, repository, body, params=None): @@ -107,9 +102,8 @@ class SnapshotClient(NamespacedClient): for param in (repository, body): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('PUT', _make_path('_snapshot', + return self.transport.perform_request('PUT', _make_path('_snapshot', repository), params=params, body=body) - return data @query_params('master_timeout', 'wait_for_completion') def restore(self, repository, snapshot, body=None, params=None): @@ -128,9 +122,8 @@ class SnapshotClient(NamespacedClient): for param in (repository, snapshot): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - _, data = self.transport.perform_request('POST', _make_path('_snapshot', + return self.transport.perform_request('POST', _make_path('_snapshot', repository, snapshot, '_restore'), params=params, body=body) - return data @query_params('master_timeout') def status(self, repository=None, snapshot=None, params=None): @@ -145,9 +138,8 @@ class SnapshotClient(NamespacedClient): :arg master_timeout: Explicit operation timeout for connection to master node """ - _, data = self.transport.perform_request('GET', _make_path('_snapshot', + return self.transport.perform_request('GET', _make_path('_snapshot', repository, snapshot, '_status'), params=params) - return data @query_params('master_timeout', 'timeout') def verify_repository(self, repository, params=None): @@ -163,7 +155,5 @@ class SnapshotClient(NamespacedClient): """ if repository in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'repository'.") - _, data = self.transport.perform_request('POST', _make_path('_snapshot', + return self.transport.perform_request('POST', _make_path('_snapshot', repository, '_verify'), params=params) - return data - diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index ad2c9021..c1935590 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -351,7 +351,7 @@ class Transport(object): self.connection_pool.mark_live(connection) if data: data = self.deserializer.loads(data, headers.get('content-type')) - return status, data + return data def close(self): """ From 64ff08b2aed568b90257d870c0718e096ec9b4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Thu, 10 Mar 2016 19:06:20 +0100 Subject: [PATCH 2/3] Move HEAD handling to Transport --- elasticsearch/client/__init__.py | 61 ++------------------------------ elasticsearch/client/indices.py | 25 +++---------- elasticsearch/transport.py | 6 ++++ 3 files changed, 13 insertions(+), 79 deletions(-) diff --git a/elasticsearch/client/__init__.py b/elasticsearch/client/__init__.py index 84ea9455..7b8ff559 100644 --- a/elasticsearch/client/__init__.py +++ b/elasticsearch/client/__init__.py @@ -3,7 +3,7 @@ import weakref import logging from ..transport import Transport -from ..exceptions import NotFoundError, TransportError +from ..exceptions import TransportError from ..compat import string_types, urlparse from .indices import IndicesClient from .cluster import ClusterClient @@ -204,11 +204,7 @@ class Elasticsearch(object): Returns True if the cluster is up, False otherwise. ``_ """ - try: - self.transport.perform_request('HEAD', '/', params=params) - except NotFoundError: - return False - return True + return self.transport.perform_request('HEAD', '/', params=params) @query_params() def info(self, params=None): @@ -299,12 +295,8 @@ class Elasticsearch(object): for param in (index, doc_type, id): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - try: - self.transport.perform_request('HEAD', _make_path(index, doc_type, + return self.transport.perform_request('HEAD', _make_path(index, doc_type, id), params=params) - except NotFoundError: - return False - return True @query_params('_source', '_source_exclude', '_source_include', 'fields', 'parent', 'preference', 'realtime', 'refresh', 'routing', 'version', @@ -1158,53 +1150,6 @@ class Elasticsearch(object): return self.transport.perform_request('DELETE', _make_path('_search', 'template', id), params=params) - @query_params('allow_no_indices', 'analyze_wildcard', 'analyzer', - 'default_operator', 'df', 'expand_wildcards', 'ignore_unavailable', - 'lenient', 'lowercase_expanded_terms', 'min_score', 'preference', 'q', - 'routing') - def search_exists(self, index=None, doc_type=None, body=None, params=None): - """ - The exists API allows to easily determine if any matching documents - exist for a provided query. - ``_ - - :arg index: A comma-separated list of indices to restrict the results - :arg doc_type: A comma-separated list of types to restrict the results - :arg body: A query to restrict the results specified with the Query DSL - (optional) - :arg allow_no_indices: Whether to ignore if a wildcard indices - expression resolves into no concrete indices. (This includes `_all` - string or when no indices have been specified) - :arg analyze_wildcard: Specify whether wildcard and prefix queries - should be analyzed (default: false) - :arg analyzer: The analyzer to use for the query string - :arg default_operator: The default operator for query string query (AND - or OR), default 'OR', valid choices are: 'AND', 'OR' - :arg df: The field to use as default where no field prefix is given in - the query string - :arg expand_wildcards: Whether to expand wildcard expression to concrete - indices that are open, closed or both., default 'open', valid - choices are: 'open', 'closed', 'none', 'all' - :arg ignore_unavailable: Whether specified concrete indices should be - ignored when unavailable (missing or closed) - :arg lenient: Specify whether format-based query failures (such as - providing text to a numeric field) should be ignored - :arg lowercase_expanded_terms: Specify whether query terms should be - lowercased - :arg min_score: Include only documents with a specific `_score` value in - the result - :arg preference: Specify the node or shard the operation should be - performed on (default: random) - :arg q: Query in the Lucene query string syntax - :arg routing: Specific routing value - """ - try: - self.transport.perform_request('POST', _make_path(index, - doc_type, '_search', 'exists'), params=params, body=body) - except NotFoundError: - return False - return True - @query_params('allow_no_indices', 'expand_wildcards', 'fields', 'ignore_unavailable', 'level') def field_stats(self, index=None, body=None, params=None): diff --git a/elasticsearch/client/indices.py b/elasticsearch/client/indices.py index 87b9a16c..7545166a 100644 --- a/elasticsearch/client/indices.py +++ b/elasticsearch/client/indices.py @@ -1,5 +1,4 @@ from .utils import NamespacedClient, query_params, _make_path, SKIP_IN_PATH -from ..exceptions import NotFoundError class IndicesClient(NamespacedClient): @query_params('analyzer', 'char_filters', 'field', 'filters', 'format', @@ -213,12 +212,8 @@ class IndicesClient(NamespacedClient): """ if index in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'index'.") - try: - self.transport.perform_request('HEAD', _make_path(index), + return self.transport.perform_request('HEAD', _make_path(index), params=params) - except NotFoundError: - return False - return True @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'local') @@ -244,12 +239,8 @@ class IndicesClient(NamespacedClient): for param in (index, doc_type): if param in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument.") - try: - self.transport.perform_request('HEAD', _make_path(index, doc_type), + return self.transport.perform_request('HEAD', _make_path(index, doc_type), params=params) - except NotFoundError: - return False - return True @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'master_timeout', 'timeout', 'update_all_types') @@ -373,12 +364,8 @@ class IndicesClient(NamespacedClient): :arg local: Return local information, do not retrieve the state from master node (default: false) """ - try: - self.transport.perform_request('HEAD', _make_path(index, '_alias', + return self.transport.perform_request('HEAD', _make_path(index, '_alias', name), params=params) - except NotFoundError: - return False - return True @query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable', 'local') @@ -491,12 +478,8 @@ class IndicesClient(NamespacedClient): """ if name in SKIP_IN_PATH: raise ValueError("Empty value passed for a required argument 'name'.") - try: - self.transport.perform_request('HEAD', _make_path('_template', + return self.transport.perform_request('HEAD', _make_path('_template', name), params=params) - except NotFoundError: - return False - return True @query_params('flat_settings', 'local', 'master_timeout') def get_template(self, name=None, params=None): diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index c1935590..d09f1343 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -329,6 +329,9 @@ class Transport(object): status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout) except TransportError as e: + if method == 'HEAD' and e.status_code == 404: + return False + retry = False if isinstance(e, ConnectionTimeout): retry = self.retry_on_timeout @@ -347,6 +350,9 @@ class Transport(object): raise else: + if method == 'HEAD': + return 200 <= status < 300 + # connection didn't fail, confirm it's live status self.connection_pool.mark_live(connection) if data: From c51b8fdeef657a11319c4cefb40f2de0ff07f45b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Mon, 14 Mar 2016 22:23:04 +0100 Subject: [PATCH 3/3] Refactored sniff_hosts for easier implementation of AsyncTransport --- elasticsearch/connection/http_requests.py | 4 +-- elasticsearch/transport.py | 40 +++++++++++------------ 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/elasticsearch/connection/http_requests.py b/elasticsearch/connection/http_requests.py index 88f0e119..99da09f3 100644 --- a/elasticsearch/connection/http_requests.py +++ b/elasticsearch/connection/http_requests.py @@ -31,7 +31,7 @@ class RequestsHttpConnection(Connection): if not REQUESTS_AVAILABLE: raise ImproperlyConfigured("Please install requests to use RequestsHttpConnection.") - super(RequestsHttpConnection, self).__init__(host= host, port=port, **kwargs) + super(RequestsHttpConnection, self).__init__(host=host, port=port, **kwargs) self.session = requests.session() if http_auth is not None: if isinstance(http_auth, (tuple, list)): @@ -91,4 +91,4 @@ class RequestsHttpConnection(Connection): """ Explicitly closes connections """ - self.session.close() \ No newline at end of file + self.session.close() diff --git a/elasticsearch/transport.py b/elasticsearch/transport.py index d09f1343..08d7c4c2 100644 --- a/elasticsearch/transport.py +++ b/elasticsearch/transport.py @@ -218,6 +218,24 @@ class Transport(object): return list(node_info['nodes'].values()) + def _get_host_info(self, host_info): + address_key = self.connection_class.transport_schema + '_address' + host = {} + address = host_info.get(address_key, '') + if '/' in address: + host['host'], address = address.split('/', 1) + + # malformed address + if ':' not in address: + return None + + ip, port = address.rsplit(':', 1) + + # use the ip if not overridden by publish_host + host.setdefault('host', ip) + host['port'] = int(port) + + return self.host_info_callback(host_info, host) def sniff_hosts(self, initial=False): """ @@ -231,27 +249,7 @@ class Transport(object): """ node_info = self._get_sniff_data(initial) - hosts = [] - address_key = self.connection_class.transport_schema + '_address' - for n in node_info: - host = {} - address = n.get(address_key, '') - if '/' in address: - host['host'], address = address.split('/', 1) - - # malformed address - if ':' not in address: - continue - - ip, port = address.rsplit(':', 1) - - # use the ip if not overridden by publish_host - host.setdefault('host', ip) - host['port'] = int(port) - - host = self.host_info_callback(n, host) - if host is not None: - hosts.append(host) + hosts = list(filter(None, (self._get_host_info(n) for n in node_info))) # we weren't able to get any nodes, maybe using an incompatible # transport_schema or host_info_callback blocked all - raise error.