def ES_search(cls, agave_client, query_string, username, **kwargs): from designsafe.apps.projects.models.elasticsearch import IndexedProject from elasticsearch_dsl import Q pi_query = Q({'term': {'value.pi._exact': username}}) copi_query = Q({'term': {'value.coPis._exact': username}}) team_query = Q({'term': {'value.teamMembers._exact': username}}) records = IndexedProject.search().query('query_string', query=query_string, default_operator='and') records = records.filter(pi_query | copi_query | team_query ) records = records.extra(from_=kwargs.get('offset', 0), size=kwargs.get('limit',100)) records = records.execute() return [cls(agave_client=agave_client, **r.to_dict()) for r in records]
def index_or_update_project(self, uuid): """ Takes a project UUID and either creates a new document in the des-projects index or updates the document if one already exists for that project. """ from designsafe.apps.api.projects.models import Project client = get_service_account_client() project_model = Project(client) project = project_model.search({'uuid': uuid}, client)[0] project_meta = project.to_dict() to_index = { key: value for key, value in project_meta.iteritems() if key != '_links' } to_index['value'] = { key: value for key, value in project_meta['value'].iteritems() if key != 'teamMember' } if not isinstance(to_index['value'].get('awardNumber', []), list): to_index['value']['awardNumber'] = [{ 'number': to_index['value']['awardNumber'] }] if to_index['value'].get('guestMembers', []) == [None]: to_index['value']['guestMembers'] = [] project_search = IndexedProject.search().filter( Q({'term': { 'uuid._exact': uuid }})) res = project_search.execute() if res.hits.total.value == 0: # Create an ES record for the new metadata. # project_info_args = {key:value for key,value in project_info.iteritems() if key != '_links'} project_ES = IndexedProject(**to_index) project_ES.save() elif res.hits.total.value == 1: # Update the record. doc = res[0] doc.update(**to_index) else: # If we're here we've somehow indexed the same project multiple times. # Delete all records and replace with the metadata passed to the task. for doc in res: IndexedProject.get(doc.meta.id).delete() project_ES = IndexedProject(**to_index) project_ES.save()
def ES_search(cls, agave_client, query_string, **kwargs): """ Query Elasticsearch for projects matching a query string. Query the returned UUIDs with the user's client so that we return only projects that the user has permission to view. """ from designsafe.apps.projects.models.elasticsearch import IndexedProject records = IndexedProject.search().query('query_string', query=query_string, default_operator='and') records = records.extra(from_=kwargs.get('offset', 0), size=kwargs.get('limit', 100)) records = records.execute() record_uuids = list(map(lambda hit: hit.uuid, records)) result_query = {"uuid": {"$in": record_uuids}} meta_records = agave_client.meta.listMetadata( q=json.dumps(result_query), privileged=False) return [ cls(agave_client=agave_client, **dict(r, **kwargs)) for r in meta_records ]