diff --git a/service/src/main/inse/ranking/logic.py b/service/src/main/inse/ranking/logic.py index e32a9f23..661287b5 100644 --- a/service/src/main/inse/ranking/logic.py +++ b/service/src/main/inse/ranking/logic.py @@ -729,6 +729,63 @@ async def _enrich_with_company_counts( for author in authors: author.companies_count = counts.get(author.display_name, 0) +async def _filter_author_ids_by_career_stage( + author_ids: List[str], + career_stages: List[CareerStage], +) -> List[str]: + """Filters the given author ids down to those whose career stage is in the list.""" + + if not author_ids or not career_stages: + return author_ids + + # remove openalex.org prefix to match the multiplicators_v1 index, which only stores the id + short_to_full = {full.rsplit("/", 1)[-1]: full for full in author_ids} + + body = { + "size": 0, + "query": { + "bool": { + "filter": [ + {"terms": {"author_id": list(short_to_full.keys())}}, + { + "terms": { + "career_bucket": [stage.value for stage in career_stages] + } + }, + ], + }, + }, + "aggs": { + "matched_authors": { + "terms": { + "field": "author_id", + "size": len(short_to_full), + "execution_hint": "map", + }, + }, + }, + } + + response: dict[str, Any] = await elasticsearch.search( + index="multiplicators_v1", + body=body, + cluster=ESCluster.INSE, + ) + + buckets = ( + response.get("aggregations", {}) + .get("matched_authors", {}) + .get("buckets", []) + ) + + filtered_ids = [] + for bucket in buckets: + full_id = short_to_full.get(bucket.get("key", "")) + if full_id: + filtered_ids.append(full_id) + + return filtered_ids + async def rank_authors_by_keywords( keywords: List[str], @@ -748,8 +805,12 @@ async def rank_authors_by_keywords( query_start_time = time() author_ids = await _get_author_ids_from_uni(institution_name) + if not author_ids: return [] + + if career_stages: + author_ids = await _filter_author_ids_by_career_stage(author_ids, career_stages) query_body = _build_query( keywords, operator, author_ids, publication_types, timespan @@ -787,27 +848,6 @@ async def rank_authors_by_keywords( # Sort by the weighted score, highest first. ranked_authors.sort(key=_author_score, reverse=True) - # ToDo: improve speed career stage classification - career_stages = None - if career_stages: - # Enrich every ranked author with its career stage. - author_ids = [] - for author in ranked_authors: - author_ids.append(author.author_id) - - stages = await _career_stages_for_authors(author_ids) - - for author in ranked_authors: - author.career_stage = stages.get(author.author_id, CareerStage.UNKNOWN) - - allowed_stages = set(career_stages) - filtered_authors = [] - - for author in ranked_authors: - if author.career_stage in allowed_stages: - filtered_authors.append(author) - - ranked_authors = filtered_authors backend_processing_end_time = time() logger.info("Query time: %.3f s, backend processing time: %.3f s", query_end_time - query_start_time, backend_processing_end_time - backend_processing_start_time) diff --git a/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.html b/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.html index eb1e0842..8d701933 100644 --- a/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.html +++ b/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.html @@ -118,6 +118,24 @@ +