From 3c021e0c9bdc62f81afdd5285fd4e50319ea670e Mon Sep 17 00:00:00 2001 From: Honza Kral Date: Sun, 12 May 2013 20:37:16 +0200 Subject: [PATCH] Mock implementations of some client methods --- elasticsearch/client.py | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/elasticsearch/client.py b/elasticsearch/client.py index 90af5223..aa8c14d1 100644 --- a/elasticsearch/client.py +++ b/elasticsearch/client.py @@ -1,4 +1,7 @@ +from functools import wraps + from .transport import Transport +from .exceptions import NotFoundError def _normalize_hosts(hosts): # if hosts are empty, just defer to defaults down the line @@ -21,6 +24,76 @@ def _normalize_hosts(hosts): out.append(host) return out +def _normalize_list(list_or_string): + if isinstance(list_or_string, (type(''), type(u''))): + return list_or_string + return ','.join(list_or_string) + + +GLOBAL_PARAMS = ('pretty', ) + +def query_params(*es_query_params): + def _wrapper(func): + @wraps(func) + def _wrapped(*args, **kwargs): + params = kwargs.pop('params', {}) + for p in es_query_params + GLOBAL_PARAMS: + if p in kwargs: + params[p] = kwargs.pop(p) + return func(*args, params=params, **kwargs) + return _wrapped + return _wrapper + + class Elasticsearch(object): def __init__(self, hosts=None, **kwargs): self.transport = Transport(_normalize_hosts(hosts), **kwargs) + + @query_params('timeout') + def create_index(self, index, body=None, params=None): + status, data = self.transport.perform_request('PUT', '/%s' % index, params=params, body=body) + return data + + @query_params() + def delete_index(self, index='', ignore_missing=False, params=None): + index = _normalize_list(index) + try: + status, data = self.transport.perform_request('DELETE', '/%s' % index, params=params) + except NotFoundError: + if ignore_missing: + return + raise + return data + + @query_params() + def refresh(self, index=None, params=None): + url = '/_refresh' + if index: + url = '/%s/_refresh' % _normalize_list(index) + status, data = self.transport.perform_request('POST', url, params=params) + return data + + @query_params('consistency', 'op_type', 'parent', 'percolate', 'refresh', 'replication', 'routing', 'timeout', 'timestamp', 'ttl', 'version', 'version_type') + def index(self, index, doc_type, body, id=None, params=None): + status, data = self.transport.perform_request('PUT', '/%s/%s/%s' % (index, doc_type, id), params=params, body=body) + return data + + @query_params('fields', 'parent', 'preference', 'realtime', 'refresh', 'routing', 'timeout') + def get(self, index, id, doc_type=u'_all', params=None): + status, data = self.transport.perform_request('GET', '/%s/%s/%s' % (index, doc_type, id), params=params) + return data + + @query_params('explain', 'fields', 'from', 'ignore_indices', 'indices_boost', 'preference', 'routing', 'search_type', 'size', 'sort', 'source', 'stats', 'timeout', 'version') + def search(self, query, index='_all', doc_type=None, params=None): + body = None + if isinstance(query, (type(''), type(u''))): + params['q'] = query + else: + body = query + + url = '/%s/%s/_search' % (_normalize_list(index), _normalize_list(doc_type)) if doc_type else '/%s/_search' % _normalize_list(index) + + status, data = self.transport.perform_request('GET', url, params=params, body=body) + return data + +