def generate_booktitle(record): booktitle = '' pubinfo = '' if 'publication_info' in record: pubinfo = record['publication_info'] for field in pubinfo: if 'reportnumber' in field: rn = field['reportnumber'] if rn: acronym = field['acronym'] if acronym: booktitle = "%s: %s" % (rn, acronym, ) else: records = perform_es_search( "reportnumber:%s" % (rn,), "records-hep" ) if records: rec = records.hits[0] for title in rec['titles']: booktitle = title.get('title', "") if title.get('subtitle'): booktitle += ': ' + title.get('subtitle') if not booktitle: result = [] for field in pubinfo: if 'pubinfo_freetext' in field: result.append(field['pubinfo_freetext']) if result: if any(isinstance(i, list) for i in result): nested_list = list(traverse(result)) booktitle = ', '.join(str(title) for title in nested_list) else: booktitle = ', '.join(str(title) for title in result) return booktitle
def proceedings_link(record): cnum = record.get('cnum', '') out = '' if not cnum: return out records = perform_es_search( 'cnum:%s and 980__a:proceedings' % cnum, 'records-hep') if len(records): if len(records) > 1: proceedings = [] for i, record in enumerate(records.hits, start=1): try: dois = record['dois'] proceedings.append( '<a href="/record/{recid}">#{i}</a> (DOI: <a ' 'href="http://dx.doi.org/{doi}">{doi}</a>'.format( recid=record['control_number'], doi=dois[0]['value'], i=i)) except KeyError: # Guards both against records not having a "dois" field # and doi values not having a "value" field. proceedings.append( '<a href="/record/{recid}">#{i}</a>'.format( recid=record['control_number'], i=i)) out = 'Proceedings: ' out += ', '.join(proceedings) else: out += '<a href="/record/{recid}">Proceedings</a>'.format( recid=records[0]['control_number']) return out
def experiment_people_from_es(experiment_name): """Query ES for conferences in the same series.""" query = 'experiments.name:"{}"'.format(experiment_name) return perform_es_search( query, 'records-authors' ).hits
def institutions(): """View for institutions collection landing page.""" institutions_list = perform_es_search('', 1, 250, 'institutions') return render_template( 'inspirehep_theme/search/collection_institutions.html', ctx={'results': institutions_list}, collection='institutions')
def _get_upcoming_conferences(): today = date.today() in_six_months = today + relativedelta(months=+6) upcoming_conferences = perform_es_search( 'opening_date:{0}->{1}'.format(str(today), str(in_six_months)), 'records-conferences', 1, 100, {'opening_date': 'asc'}) return [hit['_source'] for hit in upcoming_conferences.to_dict()['hits']['hits']]
def institutions(): """View for institutions collection landing page.""" institutions_list = perform_es_search('', 'records-institutions', 0, 250) results = [hit['_source'] for hit in institutions_list.to_dict()['hits']['hits']] return render_template( 'inspirehep_theme/search/collection_institutions.html', ctx={'results': results}, collection='institutions')
def conferences_in_the_same_series_from_es(seriesname): """Query ES for conferences in the same series.""" query = 'series:"{}"'.format(seriesname) return perform_es_search(query, 'records-conferences', sort="-opening_date", fields=[ 'control_number', 'titles', 'address', 'opening_date', 'closing_date' ] ).hits
def link_to_hep_affiliation(record): try: icn = record['ICN'] except KeyError: return '' records = perform_es_search('affiliation:%s' % icn, 'records-institutions') results = records.hits.total if results: if results == 1: return str(results) + ' Paper from ' + \ str(record['ICN']) else: return str(results) + ' Papers from ' + \ str(record['ICN']) else: return ''
def experiment_contributions_from_es(experiment_name): """Query ES for conferences in the same series.""" query = 'accelerator_experiments.experiment:"{}"'.format(experiment_name) return perform_es_search( query, 'records-hep', sort='-citation_count', size=100, fields=[ 'control_number', 'earliest_date', 'titles', 'authors', 'publication_info', 'citation_count', 'collaboration' ] ).hits
def conferences(): """View for conferences collection landing page.""" today = datetime.today() today_str = today.strftime('%Y-%m-%d') six_months_str = (today + relativedelta(months=+6)).strftime('%Y-%m-%d') upcoming_conferences = perform_es_search( 'opening_date:{0}->{1}'.format(today_str, six_months_str), 1, 100, 'conferences', 'opening_date:asc') return render_template( 'inspirehep_theme/search/collection_conferences.html', ctx={ 'conference_subject_areas': CONFERENCE_CATEGORIES_TO_SERIES, 'results': upcoming_conferences }, collection='conferences')
def conferences(): """View for conferences collection landing page.""" today = datetime.today() today_str = today.strftime('%Y-%m-%d') six_months_str = (today + relativedelta(months=+6)).strftime('%Y-%m-%d') upcoming_conferences = perform_es_search( 'opening_date:{0}->{1}'.format(today_str, six_months_str), 'records-conferences', 1, 100, {'opening_date': 'asc'}) results = [hit['_source'] for hit in upcoming_conferences.to_dict()['hits']['hits']] return render_template( 'inspirehep_theme/search/collection_conferences.html', ctx={'conference_subject_areas': CONFERENCE_CATEGORIES_TO_SERIES, 'results': results}, collection='conferences')
def number_of_search_results(query, collection_name): """ Filter used to show total number of results out of filtered ones. """ session_key = 'last-query' + query + collection_name if session.get(session_key): query_timestamp = session[session_key]['timestamp'] seconds_since_query = ( datetime.datetime.utcnow() - query_timestamp).total_seconds() if seconds_since_query < 300: # Only use the session value if it is newer than 5 minutes # This should allow for the common use case of navigating # facets and avoid using an outdated value when using a direct # link return session[session_key][ "number_of_hits" ] index = collection_to_index(collection_name) return perform_es_search(query, index).hits.total
def get_institution_papers_from_es(recid): """ Get papers where some author is affiliated with institution. :param recid: id of the institution. :type recid: string """ query = "authors.affiliations.recid:{}".format(recid) return perform_es_search( query, 'records-hep', sort='-earliest_date', size=100, fields=[ 'control_number', 'earliest_date', 'titles', 'authors', 'publication_info', 'citation_count', 'collaboration' ] ).hits
def get_institution_people_datatables_rows(recid): """ Datatable rows to render people working in an institution. :param recid: id of the institution. :type recid: string """ query = { "query": { "term": { "authors.affiliations.recid": recid } }, "aggs": { "authors": { "nested": { "path": "authors" }, "aggs": { "affiliated": { "filter": { "term": {"authors.affiliations.recid": recid} }, "aggs": { "byrecid": { "terms": { "field": "authors.recid" } } } } } } } } records_from_es = current_search_client.search( index='records-hep', doc_type='hep', body=query, search_type='count' ) # Extract all the record ids from the aggregation papers_per_author = records_from_es[ 'aggregations' ]['authors']['affiliated']['byrecid']['buckets'] recids = [int(paper['key']) for paper in papers_per_author] # Generate query to retrieve records from author index query = "" for i, recid in enumerate(recids): query += "recid:{}".format(recid) if i != len(recids) - 1: query += " OR " results = perform_es_search( query, 'records-authors', size=9999, fields=['control_number', 'name'] ) recid_map = dict( [(int(result.control_number), result.name) for result in results] ) result = [] author_html_link = "<a href='/authors/{recid}'>{name}</a>" for author in papers_per_author: row = [] try: row.append( author_html_link.format( recid=author['key'], name=recid_map[author['key']].preferred_name ) ) except: # No preferred name, use value row.append( author_html_link.format( recid=author['key'], name=recid_map[author['key']].value ) ) row.append(author['doc_count']) result.append(row) return result
def _get_some_institutions(): some_institutions = perform_es_search('', 'records-institutions', 0, 250) return [hit['_source'] for hit in some_institutions.to_dict()['hits']['hits']]
def number_of_records(collection_name): """Returns number of records for the collection.""" index = collection_to_index(collection_name) return perform_es_search('', index).hits.total