def vendor3_martigny(app, org_martigny, vendor3_martigny_data): """Load vendor record.""" vendor = Vendor.create(data=vendor3_martigny_data, delete_pid=False, dbcommit=True, reindex=True) flush_index(VendorsSearch.Meta.index) return vendor
def test_budgets_es_mapping(es, db, org_martigny, vendor_martigny_data): """Test vendors elasticsearch mapping.""" search = VendorsSearch() mapping = get_mapping(search.Meta.index) assert mapping vendor = Vendor.create(vendor_martigny_data, dbcommit=True, reindex=True, delete_pid=True) assert mapping == get_mapping(search.Meta.index) vendor.delete(force=True, dbcommit=True, delindex=True)
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 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') return super(ItemsJSONSerializer, self).post_process_serialize_search(results, pid_fetcher)
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)