diff --git a/annotator/annotation.py b/annotator/annotation.py index 33f6da7..11185af 100644 --- a/annotator/annotation.py +++ b/annotator/annotation.py @@ -85,7 +85,7 @@ def search_raw(cls, query=None, params=None, user=None, if authorization_enabled: f = authz.permissions_filter(user) if not f: - raise RunTimeError("Authorization filter creation failed") + raise RuntimeError("Authorization filter creation failed") filtered_query = { 'filtered': { 'filter': f @@ -128,13 +128,6 @@ def _build_query(cls, query=None, offset=None, limit=None, 'minimum_should_match': 1 } - if es.authorization_enabled: - # Apply a filter to the results. - f = authz.permissions_filter(user) - if not f: - return False # Refuse to perform the query - q['query'] = {'filtered': {'query': q['query'], 'filter': f}} - return q diff --git a/annotator/elasticsearch.py b/annotator/elasticsearch.py index d6b8a5c..6f1a2bb 100644 --- a/annotator/elasticsearch.py +++ b/annotator/elasticsearch.py @@ -143,21 +143,12 @@ def _build_query(cls, query=None, offset=None, limit=None, **kwargs): query = {} return _build_query(query, offset, limit) - @classmethod - def _build_query_raw(cls, request, **kwargs): - return _build_query_raw(request) - @classmethod def search(cls, query=None, offset=0, limit=RESULTS_DEFAULT_SIZE, **kwargs): q = cls._build_query(query=query, offset=offset, limit=limit, **kwargs) if not q: return [] - log.debug("doing search: %s", q) - res = cls.es.conn.search(index=cls.es.index, - doc_type=cls.__type__, - body=q) - docs = res['hits']['hits'] - return [cls(d['_source'], id=d['_id']) for d in docs] + return cls.search_raw(q, **kwargs) @classmethod def search_raw(cls, query=None, params=None, raw_result=False, **kwargs): @@ -185,19 +176,13 @@ def search_raw(cls, query=None, params=None, raw_result=False, **kwargs): @classmethod def count(cls, **kwargs): - q = cls._build_query(**kwargs) - if not q: - return 0 - - # Extract the query, and wrap it in the expected object. This has the - # effect of removing sort or paging parameters that aren't allowed by - # the count API. - q = {'query': q['query']} - - res = cls.es.conn.count(index=cls.es.index, - doc_type=cls.__type__, - body=q) - return res['count'] + """Like search, but only count the number of matches.""" + kwargs.setdefault('params', {}) + kwargs['params'].update({'search_type':'count'}) + kwargs['limit'] = 0 # just for optimisation + res = cls.search(raw_result=True, + **kwargs) + return res['hits']['total'] def save(self, refresh=True): _add_created(self) diff --git a/tests/test_annotation.py b/tests/test_annotation.py index c71a781..01f4bb0 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -232,8 +232,8 @@ def test_search_permissions_malicious(self): # Any user whose username starts with "group:" must be refused any results user = h.MockUser('group:anyone', 'testconsumer') - res = Annotation.search(user=user) - assert_equal(len(res), 0) + search_action = lambda: Annotation.search(user=user) + assert_raises(RuntimeError, search_action) def test_search_permissions_admin(self): anno = Annotation(text='Foobar',