This commit is contained in:
@@ -632,84 +632,6 @@ def _author_score(author: RankedAuthor) -> float:
|
||||
return author.score
|
||||
|
||||
|
||||
def _career_query(author_ids: List[str]) -> dict:
|
||||
"""Query that collects each author's distinct publication years.
|
||||
|
||||
The career stage is derived from all of an author's works (not only the
|
||||
keyword-matching ones), so we filter the works by the author ids and
|
||||
aggregate the publication years per author.
|
||||
"""
|
||||
return {
|
||||
"size": 0,
|
||||
"query": {
|
||||
"nested": {
|
||||
"path": "authorships",
|
||||
"query": {"terms": {"authorships.author.id": author_ids}},
|
||||
},
|
||||
},
|
||||
"aggs": {
|
||||
"authors": {
|
||||
"nested": {"path": "authorships"},
|
||||
"aggs": {
|
||||
"by_author": {
|
||||
"terms": {
|
||||
"field": "authorships.author.id",
|
||||
"include": author_ids,
|
||||
"size": len(author_ids),
|
||||
},
|
||||
"aggs": {
|
||||
"to_work": {
|
||||
"reverse_nested": {},
|
||||
"aggs": {
|
||||
"publication_years": {
|
||||
"terms": {
|
||||
"field": "publication_year",
|
||||
"size": 1000,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
async def _career_stages_for_authors(author_ids: List[str]) -> dict:
|
||||
"""Returns a mapping author_id -> CareerStage for the given authors."""
|
||||
if not author_ids:
|
||||
return {}
|
||||
|
||||
response: dict[str, Any] = await elasticsearch.search(
|
||||
index=config.settings.openalex_works_search_index,
|
||||
body=_career_query(author_ids),
|
||||
cluster=ESCluster.NES,
|
||||
)
|
||||
|
||||
author_buckets = (
|
||||
response.get("aggregations", {})
|
||||
.get("authors", {})
|
||||
.get("by_author", {})
|
||||
.get("buckets", [])
|
||||
)
|
||||
|
||||
stages = {}
|
||||
for author_bucket in author_buckets:
|
||||
author_id = author_bucket.get("key", "")
|
||||
year_buckets = (
|
||||
author_bucket.get("to_work", {})
|
||||
.get("publication_years", {})
|
||||
.get("buckets", [])
|
||||
)
|
||||
# Each bucket is one distinct publication year -> number of active years.
|
||||
active_years = len(year_buckets)
|
||||
stages[author_id] = CareerStage(_classify_career_stage(active_years))
|
||||
|
||||
return stages
|
||||
|
||||
|
||||
async def _enrich_with_company_counts(
|
||||
authors: List[RankedAuthor],
|
||||
company_status: CompanyStatusFilter = CompanyStatusFilter.ALL,
|
||||
|
||||
Reference in New Issue
Block a user