Skip to content

Commit 64ff08b

Browse files
committed
Move HEAD handling to Transport
1 parent 38bb0aa commit 64ff08b

File tree

3 files changed

+13
-79
lines changed

3 files changed

+13
-79
lines changed

elasticsearch/client/__init__.py

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44

55
from ..transport import Transport
6-
from ..exceptions import NotFoundError, TransportError
6+
from ..exceptions import TransportError
77
from ..compat import string_types, urlparse
88
from .indices import IndicesClient
99
from .cluster import ClusterClient
@@ -204,11 +204,7 @@ def ping(self, params=None):
204204
Returns True if the cluster is up, False otherwise.
205205
`<http://www.elastic.co/guide/>`_
206206
"""
207-
try:
208-
self.transport.perform_request('HEAD', '/', params=params)
209-
except NotFoundError:
210-
return False
211-
return True
207+
return self.transport.perform_request('HEAD', '/', params=params)
212208

213209
@query_params()
214210
def info(self, params=None):
@@ -299,12 +295,8 @@ def exists(self, index, doc_type, id, params=None):
299295
for param in (index, doc_type, id):
300296
if param in SKIP_IN_PATH:
301297
raise ValueError("Empty value passed for a required argument.")
302-
try:
303-
self.transport.perform_request('HEAD', _make_path(index, doc_type,
298+
return self.transport.perform_request('HEAD', _make_path(index, doc_type,
304299
id), params=params)
305-
except NotFoundError:
306-
return False
307-
return True
308300

309301
@query_params('_source', '_source_exclude', '_source_include', 'fields',
310302
'parent', 'preference', 'realtime', 'refresh', 'routing', 'version',
@@ -1158,53 +1150,6 @@ def delete_template(self, id, params=None):
11581150
return self.transport.perform_request('DELETE', _make_path('_search',
11591151
'template', id), params=params)
11601152

1161-
@query_params('allow_no_indices', 'analyze_wildcard', 'analyzer',
1162-
'default_operator', 'df', 'expand_wildcards', 'ignore_unavailable',
1163-
'lenient', 'lowercase_expanded_terms', 'min_score', 'preference', 'q',
1164-
'routing')
1165-
def search_exists(self, index=None, doc_type=None, body=None, params=None):
1166-
"""
1167-
The exists API allows to easily determine if any matching documents
1168-
exist for a provided query.
1169-
`<http://www.elastic.co/guide/en/elasticsearch/reference/current/search-exists.html>`_
1170-
1171-
:arg index: A comma-separated list of indices to restrict the results
1172-
:arg doc_type: A comma-separated list of types to restrict the results
1173-
:arg body: A query to restrict the results specified with the Query DSL
1174-
(optional)
1175-
:arg allow_no_indices: Whether to ignore if a wildcard indices
1176-
expression resolves into no concrete indices. (This includes `_all`
1177-
string or when no indices have been specified)
1178-
:arg analyze_wildcard: Specify whether wildcard and prefix queries
1179-
should be analyzed (default: false)
1180-
:arg analyzer: The analyzer to use for the query string
1181-
:arg default_operator: The default operator for query string query (AND
1182-
or OR), default 'OR', valid choices are: 'AND', 'OR'
1183-
:arg df: The field to use as default where no field prefix is given in
1184-
the query string
1185-
:arg expand_wildcards: Whether to expand wildcard expression to concrete
1186-
indices that are open, closed or both., default 'open', valid
1187-
choices are: 'open', 'closed', 'none', 'all'
1188-
:arg ignore_unavailable: Whether specified concrete indices should be
1189-
ignored when unavailable (missing or closed)
1190-
:arg lenient: Specify whether format-based query failures (such as
1191-
providing text to a numeric field) should be ignored
1192-
:arg lowercase_expanded_terms: Specify whether query terms should be
1193-
lowercased
1194-
:arg min_score: Include only documents with a specific `_score` value in
1195-
the result
1196-
:arg preference: Specify the node or shard the operation should be
1197-
performed on (default: random)
1198-
:arg q: Query in the Lucene query string syntax
1199-
:arg routing: Specific routing value
1200-
"""
1201-
try:
1202-
self.transport.perform_request('POST', _make_path(index,
1203-
doc_type, '_search', 'exists'), params=params, body=body)
1204-
except NotFoundError:
1205-
return False
1206-
return True
1207-
12081153
@query_params('allow_no_indices', 'expand_wildcards', 'fields',
12091154
'ignore_unavailable', 'level')
12101155
def field_stats(self, index=None, body=None, params=None):

elasticsearch/client/indices.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from .utils import NamespacedClient, query_params, _make_path, SKIP_IN_PATH
2-
from ..exceptions import NotFoundError
32

43
class IndicesClient(NamespacedClient):
54
@query_params('analyzer', 'char_filters', 'field', 'filters', 'format',
@@ -213,12 +212,8 @@ def exists(self, index, params=None):
213212
"""
214213
if index in SKIP_IN_PATH:
215214
raise ValueError("Empty value passed for a required argument 'index'.")
216-
try:
217-
self.transport.perform_request('HEAD', _make_path(index),
215+
return self.transport.perform_request('HEAD', _make_path(index),
218216
params=params)
219-
except NotFoundError:
220-
return False
221-
return True
222217

223218
@query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable',
224219
'local')
@@ -244,12 +239,8 @@ def exists_type(self, index, doc_type, params=None):
244239
for param in (index, doc_type):
245240
if param in SKIP_IN_PATH:
246241
raise ValueError("Empty value passed for a required argument.")
247-
try:
248-
self.transport.perform_request('HEAD', _make_path(index, doc_type),
242+
return self.transport.perform_request('HEAD', _make_path(index, doc_type),
249243
params=params)
250-
except NotFoundError:
251-
return False
252-
return True
253244

254245
@query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable',
255246
'master_timeout', 'timeout', 'update_all_types')
@@ -373,12 +364,8 @@ def exists_alias(self, index=None, name=None, params=None):
373364
:arg local: Return local information, do not retrieve the state from
374365
master node (default: false)
375366
"""
376-
try:
377-
self.transport.perform_request('HEAD', _make_path(index, '_alias',
367+
return self.transport.perform_request('HEAD', _make_path(index, '_alias',
378368
name), params=params)
379-
except NotFoundError:
380-
return False
381-
return True
382369

383370
@query_params('allow_no_indices', 'expand_wildcards', 'ignore_unavailable',
384371
'local')
@@ -491,12 +478,8 @@ def exists_template(self, name, params=None):
491478
"""
492479
if name in SKIP_IN_PATH:
493480
raise ValueError("Empty value passed for a required argument 'name'.")
494-
try:
495-
self.transport.perform_request('HEAD', _make_path('_template',
481+
return self.transport.perform_request('HEAD', _make_path('_template',
496482
name), params=params)
497-
except NotFoundError:
498-
return False
499-
return True
500483

501484
@query_params('flat_settings', 'local', 'master_timeout')
502485
def get_template(self, name=None, params=None):

elasticsearch/transport.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ def perform_request(self, method, url, params=None, body=None):
329329
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
330330

331331
except TransportError as e:
332+
if method == 'HEAD' and e.status_code == 404:
333+
return False
334+
332335
retry = False
333336
if isinstance(e, ConnectionTimeout):
334337
retry = self.retry_on_timeout
@@ -347,6 +350,9 @@ def perform_request(self, method, url, params=None, body=None):
347350
raise
348351

349352
else:
353+
if method == 'HEAD':
354+
return 200 <= status < 300
355+
350356
# connection didn't fail, confirm it's live status
351357
self.connection_pool.mark_live(connection)
352358
if data:

0 commit comments

Comments
 (0)