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 """ if query is None: query = {} if authorization_enabled is None: authorization_enabled = es.authorization_enabled if authorization_enabled: f = authz.permissions_filter(user) if not f: raise RuntimeError("Authorization filter creation failed") filtered_query = { 'filtered': { 'filter': f } } # Insert original query (if present) if 'query' in query: filtered_query['filtered']['query'] = query['query'] # Use the filtered query instead of the original query['query'] = filtered_query res = super(Annotation, cls).search_raw(query=query, params=params, raw_result=raw_result) return res
def _build_query(cls, query=None, offset=None, limit=None, user=None, **kwargs): if query is None: query = {} q = super(Annotation, cls)._build_query(query, offset, limit, **kwargs) # attempt to expand query to include uris for other representations # using information we may have on hand about the Document if 'uri' in query: term_filter = q['query']['filtered']['filter'] doc = document.Document.get_by_uri(query['uri']) if doc: new_terms = [] for term in term_filter['and']: if 'uri' in term['term']: term = {'or': []} for uri in doc.uris(): term['or'].append({'term': {'uri': uri}}) new_terms.append(term) term_filter['and'] = new_terms 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
def _build_query(cls, offset=0, limit=20, **kwargs): q = super(Annotation, cls)._build_query(offset, limit, **kwargs) if current_app.config.get('AUTHZ_ON'): f = authz.permissions_filter(g.user) if not f: return False # Refuse to perform the query q['query'] = {'filtered': {'query': q['query'], 'filter': f}} # attempt to expand query to include uris for other representations # using information we may have on hand about the Document if kwargs.has_key('uri'): doc = document.Document.get_by_uri(kwargs['uri']) if doc: new_terms = [] terms = q['query']['filtered']['query']['filtered']['filter']['and'] for term in terms: if term['term'].has_key('uri'): term = {"or": []} for uri in doc.uris(): term["or"].append({"term": {"uri": uri}}) new_terms.append(term) q['query']['filtered']['query']['filtered']['filter']['and'] = new_terms return q
def _build_query(cls, offset=0, limit=20, **kwargs): q = super(Annotation, cls)._build_query(offset, limit, **kwargs) # attempt to expand query to include uris for other representations # using information we may have on hand about the Document if 'uri' in kwargs: term_filter = q['query']['filtered']['filter'] doc = document.Document.get_by_uri(kwargs['uri']) if doc: new_terms = [] for term in term_filter['and']: if 'uri' in term['term']: term = {'or': []} for uri in doc.uris(): term['or'].append({'term': {'uri': uri}}) new_terms.append(term) term_filter['and'] = new_terms if current_app.config.get('AUTHZ_ON'): f = authz.permissions_filter(g.user) if not f: return False # Refuse to perform the query q['query'] = {'filtered': {'query': q['query'], 'filter': f}} return q
def _build_query_raw(cls, request): q, p = super(Annotation, cls)._build_query_raw(request) if current_app.config.get('AUTHZ_ON'): f = authz.permissions_filter(g.user) if not f: return {'error': "Authorization error!", 'status': 400}, None q['query'] = {'filtered': {'query': q['query'], 'filter': f}} return q, p
def _build_query_raw(cls, request): q, p = super(Annotation, cls)._build_query_raw(request) if current_app.config.get('AUTHZ_ON'): f = authz.permissions_filter(g.user) if not f: return {'error': 'Authorization error!', 'status': 400}, None q['query'] = {'filtered': {'query': q['query'], 'filter': f}} return q, p
def _build_query(cls, offset=0, limit=20, **kwargs): q = super(Annotation, cls)._build_query(offset, limit, **kwargs) if current_app.config.get('AUTHZ_ON'): f = authz.permissions_filter(g.user) if not f: return False # Refuse to perform the query q['query'] = {'filtered': {'query': q['query'], 'filter': f}} return q
def _build_query_raw(cls, request, user=None, **kwargs): q, p = super(Annotation, cls)._build_query_raw(request, **kwargs) if es.authorization_enabled: f = authz.permissions_filter(user) if not f: return {'error': 'Authorization error!', 'status': 400}, None q['query'] = {'filtered': {'query': q['query'], 'filter': f}} return q, p
def stats_for_user(cls, user): stats = {} q = { 'query': { 'match_all': {} }, 'filter': { 'and': [ permissions_filter(g.user), { 'or': [{ 'term': { 'user': user.id } }, { 'term': { 'user.id': user.id } }] } ] } } res = cls.es.conn.count(index=cls.es.index, doc_type=cls.__type__, body={'query': { 'filtered': q }}) stats['num_annotations'] = res['count'] uris_res = cls.es.conn.search(index=cls.es.index, doc_type=cls.__type__, body={ 'query': { 'filtered': q }, 'facets': { 'uri': { 'terms': { 'field': 'uri' } } }, 'size': 0 }) stats['num_uris'] = len(uris_res['facets']['uri']['terms']) return stats
def stats_for_user(cls, user): stats = {} q = {'query': {'match_all': {}}, 'filter': {'and': [permissions_filter(g.user), {'or': [{'term': {'user': user.id}}, {'term': {'user.id': user.id}}]}]}} res = cls.es.conn.count({'filtered': q}, cls.es.index, cls.__type__) stats['num_annotations'] = res['count'] uris_res = cls.es.conn.search_raw({ 'query': {'filtered': q}, 'facets': {'uri': {'terms': {'field': 'uri'}}}, 'size': 0 }, cls.es.index, cls.__type__) stats['num_uris'] = len(uris_res['facets']['uri']['terms']) return stats