def get(self, request, domain='all', format=None): query_dict = request.GET size = query_dict.get('size', 10000) if domain != 'all': result = es.search(index='domains', body={'size': size,'query': {'bool': {'must': [{'match_phrase': {'domain': {'query': domain}}}]}}}) count = result['hits']['total'] if count > 0: result = [el['_source'] for el in result['hits']['hits']][0] return Response({'count': count, 'data': result}, status=200) else: return Response(status=status.HTTP_404_NOT_FOUND) else: elastic_domain_query_struct = { "query" : { "match_all" : {} } } result = es.search(index='domains', body=elastic_domain_query_struct) count = result['hits']['total'] if count > 0: result = [el['_source'] for el in result['hits']['hits']] return Response({'count': count, 'data': result}, status=200) return Response(status=status.HTTP_404_NOT_FOUND)
def delete(self, request, format=None): payload = request.data if not payload: return Response({"details": "Missing domain."}, status=status.HTTP_400_BAD_REQUEST) domain = unicode(payload.get('domain', None)) if not domain: return Response({"details": "Missing domain."}, status=status.HTTP_400_BAD_REQUEST) if domain in ['all']: return Response({"details": "Domain not allowed."}, status=status.HTTP_400_BAD_REQUEST) try: d = Domain.objects.get(name__iexact=domain) if d.owner != request.user: return Response( {"details": "You are not the owner of this domain."}, status=status.HTTP_401_UNAUTHORIZED) d.domainresource_set.all().delete() d.delete() result = es.search(index='domains', body={ 'size': 10, 'query': { 'bool': { 'must': [{ 'match': { 'domain': { 'query': domain } } }] } } }) es.delete(index='domains', doc_type='subdomains', id=result['hits']['hits'][0]['_id']) except Domain.DoesNotExist: return Response({"details": "Domain does not exist."}, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_200_OK)
def put(self, request, domain=None, format=None): # cleanup and verification if not domain: return Response({"details": "Missing domain."}, status=status.HTTP_400_BAD_REQUEST) if domain == 'all': return Response({"details": "This domain not allowed."}, status=status.HTTP_400_BAD_REQUEST) payload = request.data if not payload: return Response({"details": "Expected payload."}, status=status.HTTP_400_BAD_REQUEST) if not isinstance(payload, dict): return Response({"details": "Invalid payload format."}, status=status.HTTP_400_BAD_REQUEST) if 'resources' not in payload: return Response({"details": "Expected payload: resources."}, status=status.HTTP_400_BAD_REQUEST) payload_resources = payload.get('resources', None) struct = {} for x in ['title', 'sub_title', 'description']: y = unicode(payload.get(x, None)) if len(y) == 0: y = None struct[x] = y if y != 'None' else None # check the payload if not isinstance(payload_resources, list): return Response({"details": "Invalid payload format."}, status=status.HTTP_400_BAD_REQUEST) for el in payload_resources: if not isinstance(el, dict): return Response({"details": "Invalid payload format."}, status=status.HTTP_400_BAD_REQUEST) textId = el.get('id', None) if not textId: return Response( {"details": "Missing id for one of the resources."}, status=status.HTTP_400_BAD_REQUEST) try: r = Resource.objects.get(textId__iexact=textId, visibility=1) except Resource.DoesNotExist: return Response( { "details": "Could not locate resource with id " + str(textId) + " ." }, status=status.HTTP_400_BAD_REQUEST) d = None try: d = Domain.objects.get(name__iexact=domain) if d.owner != request.user: return Response( {"details": "You are not the owner of this domain."}, status=status.HTTP_401_UNAUTHORIZED) ##### update & save domain attribute here d.title = struct['title'] d.sub_title = struct['sub_title'] d.description = struct['description'] d.save() ##### end d.domainresource_set.all().delete() result = es.search(index='domains', body={ 'size': 10, 'query': { 'bool': { 'must': [{ 'match': { 'domain': { 'query': domain } } }] } } }) count = result['hits']['total'] if count > 0: for el in payload_resources: r = None textId = el.get('id', None) try: r = Resource.objects.get(textId__iexact=el['id'], visibility=1) DomainResource(textId=r.textId, name=r.name, domain=d).save() except Resource.DoesNotExist: return Response( { "details": "Could not find resource with id " + el['id'] + "." }, status=status.HTTP_400_BAD_REQUEST) es.index(index='domains', doc_type='subdomains', body={ 'domain': d.name, 'title': d.title, 'sub_title': d.sub_title, 'description': d.description, 'resources': map( lambda x: { 'name': x.name, 'textId': x.textId }, d.domainresource_set.all()) }, id=result['hits']['hits'][0]['_id']) except Domain.DoesNotExist: raise Http404 return Response(status=status.HTTP_200_OK)
def get(self, request, ontology, format=None): query_struct = search.construct_es_query(request.GET) del query_struct['sort'] query_struct['aggs'] = {} fields = [] values = [] if ontology == 'name': fields = ['name.raw'] elif ontology == 'biotoolsID': fields = ['biotoolsID'] elif ontology == 'topic': fields = ['topic.term.raw'] elif ontology == 'operation': fields = ['function.operation.term.raw'] elif ontology == 'input': fields = [ 'function.input.data.term.keyword', 'function.input.format.term.keyword' ] elif ontology == 'output': fields = [ 'function.output.data.term.keyword', 'function.output.format.term.keyword' ] elif ontology == 'credit': fields = ['credit.name.keyword'] elif ontology == 'collectionID': fields = ['collectionID.raw'] elif ontology == 'toolType': fields = ['toolType.raw'] elif ontology == 'language': fields = ['language.raw'] elif ontology == 'accessibility': fields = ['accessibility.keyword'] elif ontology == 'cost': fields = ['cost.raw'] elif ontology == 'license': fields = ['license.raw'] elif ontology == 'all': fields = [ 'topic.term.raw', 'function.operation.term.raw', 'name.raw', 'function.input.data.term.keyword', 'function.input.format.term.keyword', 'function.output.data.term.keyword', 'function.output.format.term.keyword', 'toolType.raw', 'language.raw', 'accessibility.keyword', 'cost.raw', 'license.raw', 'credit.name.keyword', 'collectionID.raw', 'name.raw' ] else: return Response({'detail': 'Unsupported field.'}, status=status.HTTP_404_NOT_FOUND) for field in fields: query_struct['aggs'][field] = { 'terms': { 'field': field, 'size': 20000 } } result = es.search(index=settings.ELASTIC_SEARCH_INDEX, body=query_struct) for field in fields: values += [ x['key'] for x in result['aggregations'][field]['buckets'] ] return Response({'data': set(values)})
def get(self, request, format=None): query_dict = request.GET size = api_settings.PAGE_SIZE page = int(query_dict.get('page', '1')) searchLogger = logging.SearchLogger(query_dict) searchLogger.commit() domain = query_dict.get('domain', None) domain_resources = [] query_struct = search.construct_es_query(query_dict) result = es.search(index=settings.ELASTIC_SEARCH_INDEX_V2, body=query_struct) count = result['hits']['total'] results = [el['_source'] for el in result['hits']['hits']] # check if page is valid if (not results and count > 0): return Response({"detail": "Invalid page. That page contains no results."}, status=status.HTTP_404_NOT_FOUND) if domain: domain_result = es.search(index='domains', body={'size': 10000,'query': {'bool': {'must': [{'match': {'domain': {'query': domain}}}]}}}) domain_count = domain_result['hits']['total'] if domain_count > 0: domain_result = [el['_source'] for el in domain_result['hits']['hits']][0] domain_resources = set(map(lambda x: (x['id']), domain_result['resources'])) # get touples of returned tools returned_resource = set(map(lambda x: (x['id']), results)) if len(list(set(query_dict.keys()) - set([u'sort', u'domain', u'ord', u'page']))) == 0: diff = list(domain_resources) else: diff = list(returned_resource & domain_resources) if len(diff) > 0: count = len(diff) if len(diff) > 1000: results = [] for i in range(0,len(diff) / 1000): rest = len(diff) if len(diff) <= i*1000+1000 else i*1000+1000 query_struct['query'] = {'bool': {'should': map(lambda x: {'bool': {'must': [{'match': {'id': {'query': x[0]}}}]}}, diff[i*1000:rest])}} result = es.search(index='elixir', body=query_struct) sub_results = [el['_source'] for el in result['hits']['hits']] results += sub_results else: query_struct['query'] = {'bool': {'should': map(lambda x: {'bool': {'must': [{'match': {'id': {'query': x}}}]}}, diff)}} result = es.search(index='elixir', body=query_struct) count = result['hits']['total'] results = [el['_source'] for el in result['hits']['hits']] else: return Response({'count': 0, 'next': None if (page*size >= count) else "?page=" + str(page + 1), 'previous': None if page == 1 else "?page=" + str(page - 1), 'list': []}, status=200) else: return Response({'count': 0, 'next': None if (page*size >= count) else "?page=" + str(page + 1), 'previous': None if page == 1 else "?page=" + str(page - 1), 'list': []}, status=200) return Response({'count': count, 'next': None if (page*size >= count) else "?page=" + str(page + 1), 'previous': None if page == 1 else "?page=" + str(page - 1), 'list': results}, status=200)