add career stage filter (use index multiplicators_v1)

This commit is contained in:
Steven Tischer
2026-07-18 15:11:26 +02:00
parent c203a31842
commit 925a01e6a4
3 changed files with 106 additions and 21 deletions
+61 -21
View File
@@ -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)
@@ -118,6 +118,24 @@
</mat-form-field>
</div>
<div
class="weight-row filter-row career-stage-row"
style="display: block">
<mat-form-field appearance="outline" style="width: 100%">
<mat-label>Karrierestufe</mat-label>
<mat-select
multiple
[ngModel]="careerStages()"
(ngModelChange)="onCareerStagesChange($event)">
@for (option of CAREER_STAGE_OPTIONS; track option.value) {
<mat-option [value]="option.value">{{
option.label
}}</mat-option>
}
</mat-select>
</mat-form-field>
</div>
<div class="weight-row filter-row timespan-row">
<div class="weight-label">
<mat-icon class="icon-small">date_range</mat-icon>
@@ -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<string[]>([]);
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<RankedAuthor[]>([]);
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'],