adjusted query for retrieving DFG projects by institutes #263

Merged
genja merged 1 commits from feature/keywords-analysis into dev 2026-08-04 12:24:52 +02:00
3 changed files with 157 additions and 51 deletions
+47
View File
@@ -359,6 +359,53 @@ async def get_person_institutes(
}
return result
async def get_dfg_institutes_by_ids(
institute_ids: list[int],
) -> dict[str, dict]:
if not institute_ids:
return {}
response = await elasticsearch.search(
index=ESIndex.DFG_INSTITUTES,
cluster=ESCluster.INSE,
body={
"size": len(institute_ids),
"_source": [
"institute_id",
"root_id",
"root_name_de",
"root_name_en",
"institute_name_de",
"institute_name_en",
"address_country_de",
"address_country_en",
],
"query": {
"terms": {
"_id": institute_ids,
}
},
},
)
result = {}
for hit in response["hits"]["hits"]:
source = hit["_source"]
result[source["institute_id"]] = {
"institute_id": source["institute_id"],
"root_id": source.get("root_id"),
"root_name_de": source.get("root_name_de"),
"root_name_en": source.get("root_name_en"),
"institute_name_de": source.get("institute_name_de"),
"institute_name_en": source.get("institute_name_en"),
"address_country_de": source.get("address_country_de"),
"address_country_en": source.get("address_country_en"),
}
return result
#EU Cordis Projects
+58 -29
View File
@@ -250,12 +250,41 @@ class DfgProjectSummary(BaseModel):
def from_hit(cls, hit: dict) -> "DfgProjectSummary":
source = hit.get("_source", {})
fields = hit.get("fields", {})
names = []
institutions = {}
project_persons = []
persons = source.get("persons", [])
institutions_raw= source.get("institutions", [])
if isinstance(institutions_raw, list):
for institution in institutions_raw:
if not isinstance(institution, dict):
continue
institute_id = institution.get("institute_id")
if institute_id is not None:
institutions[f"institute:{institute_id}"] = DfgInstitutionSummary(
id=institute_id,
institute_name_de=institution.get("institute_name_de"),
institute_name_en=institution.get("institute_name_en"),
address_country_de=institution.get("address_country_de"),
address_country_en=institution.get("address_country_en"),
root_id=institution.get("root_id"),
root_name_de=institution.get("root_name_de"),
root_name_en=institution.get("root_name_en"),
)
root_id = institution.get("root_id")
if root_id is not None:
institutions[f"root:{root_id}"] = DfgInstitutionSummary(
id=root_id,
institute_name_de=institution.get("root_name_de"),
institute_name_en=institution.get("root_name_en"),
address_country_de=institution.get("address_country_de"),
address_country_en=institution.get("address_country_en"),
root_id=None,
root_name_de=None,
root_name_en=None,
)
if isinstance(persons, list):
for person in persons:
@@ -266,35 +295,35 @@ class DfgProjectSummary(BaseModel):
if full_name:
names.append(full_name.strip())
# previously we were creating institution objects for each person, but now we are using the institutions from the institutions_raw list
#institution = None
# if person.get("institute_id") is not None:
# institute = DfgInstitutionSummary(
# id=person["institute_id"],
# institute_name_de=person.get("institute_name_de"),
# institute_name_en=person.get("institute_name_en"),
# address_country_de=person.get("address_country_de"),
# address_country_en=person.get("address_country_en"),
# root_id=None,
# root_name_de=None,
# root_name_en=None,
# )
institution = None
if person.get("institute_id") is not None:
institute = DfgInstitutionSummary(
id=person["institute_id"],
institute_name_de=person.get("institute_name_de"),
institute_name_en=person.get("institute_name_en"),
address_country_de=person.get("address_country_de"),
address_country_en=person.get("address_country_en"),
root_id=None,
root_name_de=None,
root_name_en=None,
)
# institutions[f"institute:{person['institute_id']}"] = institute
institutions[f"institute:{person['institute_id']}"] = institute
# if person.get("root_id") is not None:
# root_institution = DfgInstitutionSummary(
# id=person["root_id"],
# institute_name_de=person.get("root_name_de"),
# institute_name_en=person.get("root_name_en"),
# address_country_de=person.get("address_country_de"),
# address_country_en=person.get("address_country_en"),
# root_id=None,
# root_name_de=None,
# root_name_en=None,
# )
if person.get("root_id") is not None:
root_institution = DfgInstitutionSummary(
id=person["root_id"],
institute_name_de=person.get("root_name_de"),
institute_name_en=person.get("root_name_en"),
address_country_de=person.get("address_country_de"),
address_country_en=person.get("address_country_en"),
root_id=None,
root_name_de=None,
root_name_en=None,
)
institutions[f"root:{person['root_id']}"] = root_institution
# institutions[f"root:{person['root_id']}"] = root_institution
if person.get("person_id") is not None:
project_persons.append(
@@ -305,7 +334,7 @@ class DfgProjectSummary(BaseModel):
full_name=full_name,
role_label_en=person.get("role_label_en"),
role_label_de=person.get("role_label_de"),
institution=institution,
institution=None,
)
)
topics_raw = source.get("topics") or []
@@ -15,6 +15,7 @@ from inse.projects.logic import (
get_openalex_topics_for_bund_projects,
get_openalex_topics_for_dfg_projects,
get_person_ids_by_institution_ids,
get_dfg_institutes_by_ids,
get_person_institutes,
)
from inse.projects.model import (
@@ -411,6 +412,7 @@ async def search_dfg_projects(
"subject_area_en",
"persons",
"subject_classifications",
"institutions",
],
"query": {},
}
@@ -522,34 +524,54 @@ async def search_dfg_projects(
)
if not matching_institution_ids:
return DfgProjectPage(elements=[], total=0, page=page, size=size)
matching_person_ids = await get_person_ids_by_institution_ids(
institution_ids=matching_institution_ids
)
#previously we were filtering by person_ids, but now we are filtering by institution_ids directly
# matching_person_ids = await get_person_ids_by_institution_ids(
# institution_ids=matching_institution_ids
# )
# must_clauses.append(
# {
# "nested": {
# "path": "persons",
# "query": {"terms": {"persons.person_id": matching_person_ids}},
# }
# }
# )
must_clauses.append(
{
"nested": {
"path": "persons",
"query": {"terms": {"persons.person_id": matching_person_ids}},
"path": "institutions",
"query": {"terms": {"institutions.institute_id": matching_institution_ids}},
}
}
)
print("MATCHING_INSTITUTION_IDS", matching_institution_ids)
if institution_ids:
matching_person_ids = await get_person_ids_by_institution_ids(
institution_ids=institution_ids
)
#prevously we were filtering by person_ids, but now we are filtering by institution_ids directly
# matching_person_ids = await get_person_ids_by_institution_ids(
# institution_ids=institution_ids
# )
if not matching_person_ids:
return DfgProjectPage(elements=[], total=0, page=page, size=size)
# if not matching_person_ids:
# return DfgProjectPage(elements=[], total=0, page=page, size=size)
# must_clauses.append(
# {
# "nested": {
# "path": "persons",
# "query": {"terms": {"persons.person_id": matching_person_ids}},
# }
# }
# )
must_clauses.append(
{
"nested": {
"path": "persons",
"query": {"terms": {"persons.person_id": matching_person_ids}},
"path": "institutions",
"query": {"terms": {"institutions.institute_id": institution_ids}},
}
}
)
body["query"] = (
{"bool": {"must": must_clauses}} if must_clauses else {"match_all": {}}
@@ -576,6 +598,7 @@ async def search_dfg_projects(
body["sort"] = [sort_config]
# print(f"Executing DFG project search with body: {json.dumps(body, indent=2)}")
# === Execute Search ===
print(f"Executing DFG project search with body: {json.dumps(body, indent=2)}")
response = await elasticsearch.search(
index=ESIndex.DFG_PROJECTS,
body=body,
@@ -599,21 +622,28 @@ async def search_dfg_projects(
source = hit["_source"]
project_id = source["id"]
source["topics"] = topic_lookup.get(str(project_id), [])
institutions = source.get("institutions", [])
for institution in institutions:
institute_id = institution.get("institute_id")
if institute_id is not None:
institute_info = await get_dfg_institutes_by_ids(institute_ids=[institute_id])
if institute_info:
institution.update(institute_info[institute_id])
persons = source.get("persons", [])
for person in persons:
person_id = person.get("person_id")
institute = person_lookup.get(person_id)
if institute:
person["root_id"] = institute.get("root_id")
person["institute_id"] = institute.get("institute_id")
person["root_name_de"] = institute.get("root_name_de")
person["root_name_en"] = institute.get("root_name_en")
person["institute_name_de"] = institute.get("institute_name_de")
person["institute_name_en"] = institute.get("institute_name_en")
person["address_country_de"] = institute.get("address_country_de")
person["address_country_en"] = institute.get("address_country_en")
# if institute:
# person["root_id"] = institute.get("root_id")
# person["institute_id"] = institute.get("institute_id")
# person["root_name_de"] = institute.get("root_name_de")
# person["root_name_en"] = institute.get("root_name_en")
# person["institute_name_de"] = institute.get("institute_name_de")
# person["institute_name_en"] = institute.get("institute_name_en")
# person["address_country_de"] = institute.get("address_country_de")
# person["address_country_en"] = institute.get("address_country_en")
print("ENRIECHED",source.get("institutions", []))
return _complete_response_to_dfg_project_page(response, page, size)
def _complete_response_to_dfg_project_page(