Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions annotator/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
31 changes: 8 additions & 23 deletions annotator/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay to use search to implement count rather than the count call but let's update the kwargs with a limit of 0 so that the database isn't accessing the document source objects and serializing them back to us.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks. Done.

**kwargs)
return res['hits']['total']

def save(self, refresh=True):
_add_created(self)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down