def post_process_serialize_search(self, results, pid_fetcher): """Post process the search results. :param results: Elasticsearch search result. :param pid_fetcher: Persistent identifier fetcher. """ records = results.get('hits', {}).get('hits', {}) for record in records: metadata = record.get('metadata', {}) document = search_document_by_pid( metadata.get('document').get('pid')) metadata['ui_title_text'] = title_format_text_head( document['title'], with_subtitle=True) # Add library name for lib_term in results.get('aggregations', {}).get('library', {}).get('buckets', []): lib = Library.get_record_by_pid(lib_term.get('key')) lib_term['name'] = lib.get('name') # Add location name for loc_term in results.get('aggregations', {}).get('location', {}).get('buckets', []): loc = Location.get_record_by_pid(loc_term.get('key')) loc_term['name'] = loc.get('name') # Add library name for item_type_term in results.get('aggregations', {}).get('item_type', {}).get('buckets', []): item_type = ItemType.get_record_by_pid(item_type_term.get('key')) item_type_term['name'] = item_type.get('name') return super(ItemsJSONSerializer, self).post_process_serialize_search(results, pid_fetcher)
def preprocess_search_hit(self, pid, record_hit, links_factory=None, **kwargs): """Prepare a record hit from Elasticsearch for serialization. :param pid: Persistent identifier instance. :param record_hit: Record metadata retrieved via search. :param links_factory: Factory function for record links. """ record = record_hit['_source'] item_pid = pid.pid_value # process location locations_map = kwargs.get('locations_map') record['location_name'] = locations_map[record.get('location').get( 'pid')] # retrieve and process document document = search_document_by_pid(record['document']['pid']) record['document_title'] = title_format_text_head(document.title, with_subtitle=True) creator = [] for contribution in document.contribution: if any(role in contribution.role for role in role_filter): try: creator.append(contribution['agent']['preferred_name']) except KeyError: creator.append( contribution['agent']['authorized_access_point_en']) if creator: record['document_creator'] = ' ; '.join(creator) record['document_type'] = document.type # get loans information loans_count, loans = search_active_loans_for_item(item_pid) record['loans_count'] = loans_count if loans_count: # get first loan loan = next(loans) record['last_transaction_date'] = format_date_filter( loan.transaction_date, date_format='short', locale=current_i18n.locale.language, ) record['created'] = format_date_filter( record['_created'], date_format='short', locale=current_i18n.locale.language, ) # prevent csv key error # TODO: find other way to prevent csv key error del (record['type']) return record
def preprocess_search_hit(self, pid, record_hit, links_factory=None, **kwargs): """Prepare a record hit from Elasticsearch for serialization. :param pid: Persistent identifier instance. :param record_hit: Record metadata retrieved via search. :param links_factory: Factory function for record links. """ language = kwargs.get('language') record = record_hit['_source'] # inherit holdings call number when possible item = Item(record) issue_call_number = item.issue_inherited_first_call_number if issue_call_number: record['call_number'] = issue_call_number item_pid = pid.pid_value # process location locations_map = kwargs.get('locations_map') record['location_name'] = locations_map[record.get('location').get( 'pid')] # retrieve and process document document = search_document_by_pid(record['document']['pid']) record['document_title'] = title_format_text_head(document.title, with_subtitle=True) # process contributions creator = [] if 'contribution' in document: for contribution in document.contribution: if any(role in contribution.role for role in role_filter): authorized_access_point = \ f'authorized_access_point_{language}' if authorized_access_point in contribution['agent']: creator.append( contribution['agent'][authorized_access_point]) if creator: record['document_creator'] = ' ; '.join(creator) document_main_type = [] document_sub_type = [] for document_type in document.type: data = document_type.to_dict() document_main_type.append(data.get('main_type')) document_sub_type.append(data.get('subtype', '')) record['document_main_type'] = ', '.join(document_main_type) record['document_sub_type'] = ', '.join(document_sub_type) # get loans information loans_count, loans = search_active_loans_for_item(item_pid) record['loans_count'] = loans_count if loans_count: # get first loan loan = next(loans) record['last_transaction_date'] = format_date_filter( loan.transaction_date, date_format='short', locale=language, ) record['created'] = format_date_filter( record['_created'], date_format='short', locale=language, ) # prevent csv key error # TODO: find other way to prevent csv key error del (record['type']) return record
def post_process_serialize_search(self, results, pid_fetcher): """Post process the search results. :param results: Elasticsearch search result. :param pid_fetcher: Persistent identifier fetcher. """ records = results.get('hits', {}).get('hits', {}) orgs = {} libs = {} locs = {} for record in records: metadata = record.get('metadata', {}) document = search_document_by_pid( metadata.get('document').get('pid') ) metadata['ui_title_text'] = title_format_text_head( document['title'], with_subtitle=True ) item = Item.get_record_by_pid(metadata.get('pid')) metadata['availability'] = { 'available': item.available, 'status': metadata['status'], 'display_text': item.availability_text, 'request': item.number_of_requests() } if not metadata['available']: if metadata['status'] == ItemStatus.ON_LOAN: metadata['availability']['due_date'] =\ item.get_item_end_date(format='long', language='en') # Item in collection collection = item.in_collection() if collection: metadata['in_collection'] = collection # Organisation organisation = metadata['organisation'] if organisation['pid'] not in orgs: orgs[organisation['pid']] = Organisation \ .get_record_by_pid(organisation['pid']) organisation['viewcode'] = orgs[organisation['pid']].get('code') # Library library = metadata['library'] if library['pid'] not in libs: libs[library['pid']] = Library \ .get_record_by_pid(library['pid']) library['name'] = libs[library['pid']].get('name') # Location location = metadata['location'] if location['pid'] not in locs: locs[location['pid']] = Location \ .get_record_by_pid(location['pid']) location['name'] = locs[location['pid']].get('name') # Add library name for lib_term in results.get('aggregations', {}).get( 'library', {}).get('buckets', []): lib = Library.get_record_by_pid(lib_term.get('key')) lib_term['name'] = lib.get('name') # Add location name for loc_term in results.get('aggregations', {}).get( 'location', {}).get('buckets', []): loc = Location.get_record_by_pid(loc_term.get('key')) loc_term['name'] = loc.get('name') # Add item type name for item_type_term in results.get('aggregations', {}).get( 'item_type', {}).get('buckets', []): item_type = ItemType.get_record_by_pid(item_type_term.get('key')) item_type_term['name'] = item_type.get('name') # Add vendor name for vendor_term in results.get('aggregations', {}).get( 'vendor', {}).get('buckets', []): vendor = Vendor.get_record_by_pid(vendor_term.get('key')) vendor_term['name'] = vendor.get('name') # Correct document type buckets buckets = results['aggregations']['document_type']['buckets'] results['aggregations']['document_type']['buckets'] = \ filter_document_type_buckets(buckets) return super().post_process_serialize_search(results, pid_fetcher) # Correct document type buckets buckets = results['aggregations']['document_type']['buckets'] results['aggregations']['document_type']['buckets'] = \ filter_document_type_buckets(buckets)
def post_process_serialize_search(self, results, pid_fetcher): """Post process the search results. :param results: Elasticsearch search result. :param pid_fetcher: Persistent identifier fetcher. """ records = results.get('hits', {}).get('hits', {}) orgs = {} libs = {} locs = {} # enrich library bucket JSONSerializer.enrich_bucket_with_data(results, 'library', LibrariesSearch, 'name') # enrich location bucket JSONSerializer.enrich_bucket_with_data(results, 'location', LocationsSearch, 'name') # enrich item type bucket JSONSerializer.enrich_bucket_with_data(results, 'item_type', ItemTypesSearch, 'name') # enrich temporary item type bucket JSONSerializer.enrich_bucket_with_data(results, 'temporary_item_type', ItemTypesSearch, 'name') # enrich temporary location bucket JSONSerializer.enrich_bucket_with_data(results, 'temporary_location', LocationsSearch, 'name') # enrich vendor bucket JSONSerializer.enrich_bucket_with_data(results, 'vendor', VendorsSearch, 'name') for record in records: metadata = record.get('metadata', {}) document = search_document_by_pid( metadata.get('document').get('pid')) metadata['ui_title_text'] = title_format_text_head( document['title'], with_subtitle=True) item = Item.get_record_by_pid(metadata.get('pid')) metadata['available'] = item.available metadata['availability'] = { 'available': metadata['available'], 'status': metadata['status'], 'display_text': item.availability_text, 'request': item.number_of_requests() } if not metadata['available']: if metadata['status'] == ItemStatus.ON_LOAN: metadata['availability']['due_date'] =\ item.get_item_end_date(format='long', language='en') # Item in collection collection = item.in_collection() if collection: metadata['in_collection'] = collection # Temporary location temp_location = metadata.get('temporary_location') if temp_location: temp_location_pid = temp_location['pid'] if temp_location_pid not in locs: locs[temp_location_pid] = Location \ .get_record_by_pid(temp_location_pid) temp_location['name'] = locs[temp_location_pid].get('name') # Organisation organisation = metadata['organisation'] if organisation['pid'] not in orgs: orgs[organisation['pid']] = Organisation \ .get_record_by_pid(organisation['pid']) organisation['viewcode'] = orgs[organisation['pid']].get('code') # Library library = metadata['library'] if library['pid'] not in libs: libs[library['pid']] = Library \ .get_record_by_pid(library['pid']) library['name'] = libs[library['pid']].get('name') # Location location = metadata['location'] if location['pid'] not in locs: locs[location['pid']] = Location \ .get_record_by_pid(location['pid']) location['name'] = locs[location['pid']].get('name') # Correct document type buckets if results.get('aggregations', {}).get('document_type'): buckets = results['aggregations']['document_type']['buckets'] results['aggregations']['document_type']['buckets'] = \ filter_document_type_buckets(buckets) return super().post_process_serialize_search(results, pid_fetcher)