def get_related_resources(resourceid, lang, limit=1000, start=0, allowedtypes=[], is_anon=False): ret = { 'resource_relationships': [], 'related_resources': [] } se = SearchEngineFactory().create() query = Query(se, limit=limit, start=start) query.add_filter(Terms(field='entityid1', terms=resourceid).dsl, operator='or') query.add_filter(Terms(field='entityid2', terms=resourceid).dsl, operator='or') resource_relations = query.search(index='resource_relations', doc_type="all") entityids = set() for relation in resource_relations['hits']['hits']: relation['_source']['preflabel'] = get_preflabel_from_valueid(relation['_source']['relationshiptype'], lang) ret['resource_relationships'].append(relation['_source']) entityids.add(relation['_source']['entityid1']) entityids.add(relation['_source']['entityid2']) if len(entityids) > 0: entityids.remove(resourceid) # can't figure why passing allowed types to doc_type param doesn't work, # so filter is carried out later related_resources = se.search(index='entity', doc_type='_all', id=list(entityids)) filtered_ids = [] if related_resources: for resource in related_resources['docs']: if not resource['_type'] in allowedtypes: filtered_ids.append(resource['_source']['entityid']) continue if is_anon: # filter out protected resources if user is anonymous # (this is basically a subset of the get_protected_entityids below # they should be combined probably) from search import get_protection_conceptids protect_id = get_protection_conceptids(settings.PROTECTION_LEVEL_NODE) conceptids = [d['conceptid'] for d in resource['_source']['domains']] if protect_id in conceptids: filtered_ids.append(resource['_source']['entityid']) continue ret['related_resources'].append(resource['_source']) if len(filtered_ids) > 0: # remove all relationships in ret that match a filtered id (this lc is yuge but I think concise) filtered_relationships = [rel for rel in ret['resource_relationships'] if not rel['entityid1'] in filtered_ids and not rel['entityid2'] in filtered_ids] # update ret values ret['resource_relationships'] = filtered_relationships ret['total'] = len(ret['resource_relationships']) return ret
def get_protected_entityids(): '''returns list of entity ids for protected resources''' from search import get_protection_conceptids protect_id = get_protection_conceptids(settings.PROTECTION_LEVEL_NODE) filtered_ids = [] se = SearchEngineFactory().create() # for some reason doc_type must be speficied with INFORMATION RESOURCE in order for that type # to be queried. right now this is ok, because it's the only type with protection levels, # but this is very strange. all_resources = se.search(index='entity', doc_type="INFORMATION_RESOURCE.E73")['hits']['hits'] for resource in all_resources: conceptids = [d['conceptid'] for d in resource['_source']['domains']] if protect_id in conceptids: filtered_ids.append(resource['_source']['entityid']) return filtered_ids