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 @@ +
+ + Karrierestufe + + @for (option of CAREER_STAGE_OPTIONS; track option.value) { + {{ + option.label + }} + } + + +
+
date_range diff --git a/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.ts b/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.ts index 1dcc8bc2..a5f8bc14 100644 --- a/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.ts +++ b/webapp/src/app/modules/keywords-search/search-results/persons-results/persons-results.component.ts @@ -224,6 +224,25 @@ export class KeywordsToPersonsResultsComponent implements OnInit { } } + readonly CAREER_STAGE_OPTIONS = [ + { value: 'Early', label: 'Early' }, + { value: 'Emerging', label: 'Emerging' }, + { value: 'Established', label: 'Established' }, + { value: 'Senior', label: 'Senior' }, + ]; + + readonly careerStages = signal([]); + + onCareerStagesChange(selected: string[]) { + this.careerStages.set(selected); + const val = selected.length > 0 ? selected.join(',') : null; + this.router.navigate([], { + relativeTo: this.route, + queryParams: { careerStage: val, page: null }, + queryParamsHandling: 'merge', + }); + } + readonly authors = signal([]); readonly loading = signal(false); @@ -237,6 +256,10 @@ export class KeywordsToPersonsResultsComponent implements OnInit { if (status === 'active' || status === 'deleted') { this.companyStatus.set(status); } + const careerStage = this.route.snapshot.queryParamMap.get('careerStage'); + if (careerStage) { + this.careerStages.set(careerStage.split(',')); + } } // reads a year query param, clamped to the valid range; falls back on invalid input @@ -297,11 +320,13 @@ export class KeywordsToPersonsResultsComponent implements OnInit { this.timespanFrom.set(this.minYear); this.timespanTo.set(this.maxYear); this.companyStatus.set('all'); + this.careerStages.set([]); this.pubTypeSearchTerm.set(''); this.router.navigate([], { relativeTo: this.route, queryParams: { pubType: null, + careerStage: null, tFrom: null, tTo: null, companyStatus: null, @@ -380,6 +405,8 @@ export class KeywordsToPersonsResultsComponent implements OnInit { this.selectedPubTypes().length > 0 ? this.selectedPubTypes() : undefined, + career_stages: + this.careerStages().length > 0 ? this.careerStages() : undefined, weights: { works: rawWeights['works'], citation: rawWeights['citations'],