add filter publication_types to filter works
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user