From 3b9436474e78446cfe23ee2d42c107ba83c511f0 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Thu, 9 Jul 2026 10:20:14 +0200 Subject: [PATCH] add filter publication_types to filter works --- service/src/main/inse/ranking/logic.py | 32 ++++++++++++++++++++----- service/src/main/inse/ranking/model.py | 31 ++++++++++++++++++++++++ service/src/main/inse/ranking/router.py | 4 +++- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/service/src/main/inse/ranking/logic.py b/service/src/main/inse/ranking/logic.py index c1cc33b4..1b0b4241 100644 --- a/service/src/main/inse/ranking/logic.py +++ b/service/src/main/inse/ranking/logic.py @@ -8,7 +8,13 @@ from inse.integrations import elasticsearch from inse.model import ESCluster from inse.persons.logic import _classify_career_stage from .. import config -from .model import CareerStage, KeywordOperator, RankedAuthor, RankingWeights +from .model import ( + CareerStage, + KeywordOperator, + PublicationType, + RankedAuthor, + RankingWeights, +) logger = logging.getLogger(__name__) @@ -72,25 +78,38 @@ def _institution_filter(uni_id: str) -> dict: } +def _publication_type_filter(publication_types: List[PublicationType]) -> dict: + """Keeps only works whose type is one of the given publication types.""" + type_values = [] + for publication_type in publication_types: + type_values.append(publication_type.value) + return {"terms": {"type": type_values}} + + def _build_query( keywords: List[str], operator: KeywordOperator, uni_id: str, weights: RankingWeights, + publication_types: List[PublicationType], ) -> dict: """Builds the Elasticsearch query for ranking authors by keywords.""" score_script = ( f"params.works * {weights.works} + params.citations * {weights.citation}" ) + work_filters = [ + _build_keyword_query(keywords, operator), + _institution_filter(uni_id), + ] + if publication_types: + work_filters.append(_publication_type_filter(publication_types)) + return { "size": 0, "query": { "bool": { - "filter": [ - _build_keyword_query(keywords, operator), - _institution_filter(uni_id), - ], + "filter": work_filters, }, }, "aggs": { @@ -262,13 +281,14 @@ async def rank_authors_by_keywords( uni_id: str, weights: RankingWeights, career_stages: List[CareerStage], + publication_types: List[PublicationType], ) -> List[RankedAuthor]: """Ranks the university's authors by a weighted score over works and citations. When career_stages is not empty, the ranked list is filtered down to authors whose career stage is one of the given stages. """ - query_body = _build_query(keywords, operator, uni_id, weights) + query_body = _build_query(keywords, operator, uni_id, weights, publication_types) response: dict[str, Any] = await elasticsearch.search( index=config.settings.openalex_works_search_index, diff --git a/service/src/main/inse/ranking/model.py b/service/src/main/inse/ranking/model.py index f857afc1..a118496e 100644 --- a/service/src/main/inse/ranking/model.py +++ b/service/src/main/inse/ranking/model.py @@ -20,6 +20,33 @@ class CareerStage(StrEnum): ESTABLISHED = "Established" SENIOR = "Senior" + +class PublicationType(StrEnum): + ARTICLE = "article" + OTHER = "other" + DATASET = "dataset" + BOOK_CHAPTER = "book-chapter" + BOOK = "book" + DISSERTATION = "dissertation" + PREPRINT = "preprint" + PARATEXT = "paratext" + LIBGUIDES = "libguides" + REVIEW = "review" + LETTER = "letter" + REFERENCE_ENTRY = "reference-entry" + EDITORIAL = "editorial" + PEER_REVIEW = "peer-review" + REPORT = "report" + STANDARD = "standard" + ERRATUM = "erratum" + RETRACTION = "retraction" + SUPPLEMENTARY_MATERIALS = "supplementary-materials" + BOOK_SECTION = "book-section" + DATABASE = "database" + SOFTWARE = "software" + REPORT_COMPONENT = "report-component" + GRANT = "grant" + class RankingWeights(BaseModel): citation: float = Field( ge=0.0, @@ -53,6 +80,10 @@ class KeywordToAuthorRequest(BaseModel): default_factory=list, description="Optional filter: only keep authors in one of these career stages. Empty = no filter.", ) + publication_types: List[PublicationType] = Field( + default_factory=list, + description="Optional filter: only count works of these publication types. Empty = no filter.", + ) @field_validator("operator", mode="before") @classmethod diff --git a/service/src/main/inse/ranking/router.py b/service/src/main/inse/ranking/router.py index 74b73a57..56d27069 100644 --- a/service/src/main/inse/ranking/router.py +++ b/service/src/main/inse/ranking/router.py @@ -18,11 +18,12 @@ router = APIRouter( @router.post("/rank-author", response_model=List[RankedAuthor]) async def rank_author(request: KeywordToAuthorRequest): logger.info( - "rank-author keywords=%s operator=%s uni_id=%s career_stages=%s", + "rank-author keywords=%s operator=%s uni_id=%s career_stages=%s publication_types=%s", request.keywords, request.operator, request.uni_id, request.career_stages, + request.publication_types, ) return await logic.rank_authors_by_keywords( request.keywords, @@ -30,4 +31,5 @@ async def rank_author(request: KeywordToAuthorRequest): request.uni_id, request.weights, request.career_stages, + request.publication_types, )