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' } project_search = IndexedProject.search().filter( Q({'term': { 'uuid._exact': uuid }})) res = project_search.execute() if res.hits.total == 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 == 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: doc.delete() project_ES = IndexedProject(**to_index) project_ES.save()
def index_projects_listing(projects): """ Index the result of an Agave projects listing Parameters ---------- projects: list list of project metadata objects (either dict or agavepy.agave.Attrdict) Returns ------- Void """ from designsafe.apps.projects.models.elasticsearch import IndexedProject idx = IndexedProject.Index.name client = IndexedProject._get_connection() ops = [] for _project in projects: # Iterate through projects and construct a bulk indexing operation. project_dict = dict(_project) project_dict = { key: value for key, value in project_dict.items() if key != '_links' } award_number = project_dict['value'].get('awardNumber', []) if not isinstance(award_number, list): award_number = [{'number': project_dict['value']['awardNumber']}] if not all(isinstance(el, dict) for el in award_number): # Punt if the list items are some type other than dict. award_number = [] project_dict['value']['awardNumber'] = award_number if project_dict['value'].get('guestMembers', []) == [None]: project_dict['value']['guestMembers'] = [] if project_dict['value'].get('nhEventStart', []) == '': project_dict['value']['nhEventStart'] = None if project_dict['value'].get('nhEventEnd', []) == '': project_dict['value']['nhEventEnd'] = None ops.append({ '_index': idx, '_id': project_dict['uuid'], 'doc': project_dict, '_op_type': 'update', 'doc_as_upsert': True }) bulk(client, ops)
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 ]