From a0573f08b0028c32719164bfbf630ce0818eb2ac Mon Sep 17 00:00:00 2001 From: Gerben Date: Tue, 30 Sep 2014 20:37:41 +0300 Subject: [PATCH] Pass params to Elasticsearch.search separately again --- annotator/annotation.py | 11 ++++++----- annotator/elasticsearch.py | 11 +++++++---- annotator/store.py | 9 ++++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/annotator/annotation.py b/annotator/annotation.py index 479838d..ff26913 100644 --- a/annotator/annotation.py +++ b/annotator/annotation.py @@ -65,18 +65,18 @@ def save(self, *args, **kwargs): super(Annotation, self).save(*args, **kwargs) @classmethod - def search_raw(cls, query=None, user=None, authorization_enabled=None, - **kwargs): + def search_raw(cls, query=None, params=None, raw_result=False, + user=None, authorization_enabled=None): """Perform a raw Elasticsearch query Any ElasticsearchExceptions are to be caught by the caller. Keyword arguments: query -- Query to send to Elasticsearch + params -- Extra keyword arguments to pass to Elasticsearch.search + raw_result -- Return Elasticsearch's response as is user -- The user to filter the results for according to permissions authorization_enabled -- Overrides Annotation.es.authorization_enabled - raw_result -- Return Elasticsearch's response as is - Extra keyword arguments are passed to Elasticsearch.search """ if query is None: query = {} @@ -97,7 +97,8 @@ def search_raw(cls, query=None, user=None, authorization_enabled=None, # Use the filtered query instead of the original query['query'] = filtered_query - res = super(Annotation, cls).search_raw(query=query, **kwargs) + res = super(Annotation, cls).search_raw(query=query, params=params, + raw_result=raw_result) return res @classmethod diff --git a/annotator/elasticsearch.py b/annotator/elasticsearch.py index a87d6b0..285d358 100644 --- a/annotator/elasticsearch.py +++ b/annotator/elasticsearch.py @@ -151,22 +151,24 @@ def search(cls, query=None, offset=0, limit=RESULTS_DEFAULT_SIZE, **kwargs): return cls.search_raw(q, **kwargs) @classmethod - def search_raw(cls, query=None, raw_result=False, **kwargs): + def search_raw(cls, query=None, params=None, raw_result=False): """Perform a raw Elasticsearch query Any ElasticsearchExceptions are to be caught by the caller. Keyword arguments: query -- Query to send to Elasticsearch + params -- Extra keyword arguments to pass to Elasticsearch.search raw_result -- Return Elasticsearch's response as is - Extra keyword arguments are passed to Elasticsearch.search """ if query is None: query = {} + if params is None: + params = {} res = cls.es.conn.search(index=cls.es.index, doc_type=cls.__type__, body=query, - **kwargs) + **params) if not raw_result: docs = res['hits']['hits'] res = [cls(d['_source'], id=d['_id']) for d in docs] @@ -175,7 +177,8 @@ def search_raw(cls, query=None, raw_result=False, **kwargs): @classmethod def count(cls, **kwargs): """Like search, but only count the number of matches.""" - kwargs['search_type'] = 'count' + kwargs.setdefault('params', {}) + kwargs['params'].update({'search_type':'count'}) res = cls.search(raw_result=True, **kwargs) return res['hits']['total'] diff --git a/annotator/store.py b/annotator/store.py index 83dcea6..fc2e8ff 100644 --- a/annotator/store.py +++ b/annotator/store.py @@ -292,16 +292,19 @@ def search_annotations(): def search_annotations_raw(): try: - query, kwargs = _build_query_raw(request) + query, params = _build_query_raw(request) except ValueError: return jsonify('Could not parse request payload!', status=400) if current_app.config.get('AUTHZ_ON'): - kwargs['user'] = g.user + user = g.user + else: + user = None try: - res = g.annotation_class.search_raw(query, raw_result=True, **kwargs) + res = g.annotation_class.search_raw(query, params, raw_result=True, + user=user) except TransportError as err: if err.status_code is not 'N/A': status_code = err.status_code