refactor(dashboard): use shared active institution membership query for person count
LocationBasedServices/smartguide/pipeline/head There was a failure building this commit
LocationBasedServices/smartguide/pipeline/head There was a failure building this commit
This commit is contained in:
@@ -9,6 +9,9 @@ from sqlmodel import Session
|
||||
|
||||
from inse import config
|
||||
from inse.auth.model import AccessToken, Organization
|
||||
from inse.authors.institution_membership_query import (
|
||||
build_active_institution_membership_query,
|
||||
)
|
||||
from inse.integrations import elasticsearch
|
||||
from inse.institutions.institutions_search_client import InstitutionsSearchClient
|
||||
from inse.model import ESCluster, ESIndex
|
||||
@@ -150,74 +153,9 @@ async def get_dashboard_persons(
|
||||
organization_name: str,
|
||||
) -> DashboardStatisticValue:
|
||||
normalized_id = _normalize_institution_id(institution_id)
|
||||
|
||||
target_year = datetime.now().year - 1
|
||||
|
||||
recent_affiliation_query = {
|
||||
"nested": {
|
||||
"path": "affiliations",
|
||||
"query": {
|
||||
"bool": {
|
||||
"filter": [
|
||||
{
|
||||
"match_phrase": {
|
||||
"affiliations.institution.id": normalized_id
|
||||
}
|
||||
},
|
||||
{"range": {"affiliations.years": {"gte": target_year}}},
|
||||
]
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
matching_last_known_institution_query = {
|
||||
"nested": {
|
||||
"path": "last_known_institutions",
|
||||
"query": {
|
||||
"match_phrase": {
|
||||
"last_known_institutions.id": normalized_id
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
missing_last_known_institutions_query = {
|
||||
"bool": {
|
||||
"must_not": [
|
||||
{
|
||||
"nested": {
|
||||
"path": "last_known_institutions",
|
||||
"query": {"exists": {"field": "last_known_institutions.id"}},
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
search_query = {
|
||||
"bool": {
|
||||
"should": [
|
||||
{
|
||||
"bool": {
|
||||
"filter": [
|
||||
matching_last_known_institution_query,
|
||||
recent_affiliation_query,
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"bool": {
|
||||
"filter": [
|
||||
missing_last_known_institutions_query,
|
||||
recent_affiliation_query,
|
||||
]
|
||||
}
|
||||
},
|
||||
],
|
||||
"minimum_should_match": 1,
|
||||
}
|
||||
}
|
||||
response = await elasticsearch.search(
|
||||
index=ESIndex.OPENALEX_AUTHORS,
|
||||
query=search_query,
|
||||
query=build_active_institution_membership_query(normalized_id),
|
||||
size=0,
|
||||
track_total_hits=True,
|
||||
cluster=ESCluster.NES,
|
||||
|
||||
Reference in New Issue
Block a user