fixed filters for BUND projects

This commit is contained in:
2026-07-28 00:31:51 +02:00
parent 0642a3704d
commit 93ba39f9b0
@@ -21,7 +21,7 @@ from inse.models.dfg_project_details import DfgProjectDetails
from inse.models.bund_project_details import BundProjectDetails
from inse.integrations import elasticsearch
from inse.model import ESCluster, ESIndex
from inse.projects.logic import get_person_ids_by_institution_ids, get_dfg_project_ids_by_topic_ids, get_openalex_topics_for_dfg_projects, get_person_institutes, get_openalex_topics_for_bund_projects
from inse.projects.logic import get_person_ids_by_institution_ids, get_dfg_project_ids_by_topic_ids,get_bund_project_ids_by_topic_ids, get_openalex_topics_for_dfg_projects, get_person_institutes, get_openalex_topics_for_bund_projects
settings = get_settings()
@@ -29,66 +29,6 @@ logger = logging.getLogger(__name__)
logger.setLevel(settings.logging_level)
DFG_PERSON_FIELDS: List[str] = [
"Antragsteller",
"Antragstellerin",
"Antragstellerinnen",
"Antragstellerinnen / Antragsteller",
"Beteiligte Person",
"Beteiligte Personen",
"Co-Sprecher",
"Co-Sprecherinnen / Co-Sprecher",
"Ehemalige Antragsteller",
"Ehemalige Antragstellerin",
"Ehemalige Antragstellerinnen",
"Ehemalige Antragstellerinnen / Ehemalige Antragsteller",
"Ehemaliger Antragsteller",
"Gastgeber",
"Gastgeberin",
"Gastgeberinnen",
"Gastgeberinnen / Gastgeber",
"Kooperationspartner",
"Kooperationspartnerin",
"Kooperationspartnerinnen",
"Kooperationspartnerinnen / Kooperationspartner",
"Leiter",
"Leiterin",
"Leiterinnen",
"Leiterinnen / Leiter",
"Mitantragsteller",
"Mitantragstellerin",
"Mitantragstellerinnen",
"Mitantragstellerinnen / Mitantragsteller",
"Mitverantwortlich",
"Mitverantwortlich(e)",
"Mitverantwortliche",
"Sprecher",
"Sprecher (IGK-Partner)",
"Sprecherin",
"Sprecherin (IGK-Partner)",
"Sprecherinnen",
"Sprecherinnen (IGK-Partner) / Sprecher (IGK-Partner)",
"Sprecherinnen / Sprecher",
"Teilprojektleiter",
"Teilprojektleiterin",
"Teilprojektleiterinnen",
"Teilprojektleiterinnen / Teilprojektleiter",
"ausländ. Mitantragstellerin",
"ausländ. Mitantragstellerinnen",
"ausländ. Mitantragstellerinnen / ausländische Mitantragsteller",
"ausländische Mitantragsteller",
"ausländischer Mitantragsteller",
"beteiligte Wissenschaftler",
"beteiligte Wissenschaftlerin",
"beteiligte Wissenschaftlerinnen",
"beteiligte Wissenschaftlerinnen / beteiligte Wissenschaftler",
"beteiligter Wissenschaftler",
"person_creater_name",
"stellvertr. Sprecher",
"stellvertr. Sprecherin",
"stellvertr. Sprecherinnen / stellvertr. Sprecher",
]
# BUND
@@ -701,40 +641,35 @@ async def get_dfg_project_by_id(project_id: str) -> Optional[DfgProjectDetails]:
#BUND
async def get_bund_project_ids_by_topic_ids(topic_ids: List[str]) -> List[str]:
"""
Query the `projects_bund_openalex_topics` index to find BUND project IDs
that match any of the given OpenAlex topic IDs.
"""
body: Dict[str, Any] = {
"size": 0,
"query": {
"terms": {
"topic_matches.topic_id": topic_ids
}
},
"aggs": {
"project_ids": {
"terms": {
"field": "project_id.keyword",
"size": 10000
def _bund_year_filters(field: str, years: List[str]) -> Dict[str, Any]:
should = []
for year in years:
year = str(year).strip()
if not year:
continue
should.append(
{
"range": {
field: {
"gte": f"01.01.{year}",
"lte": f"31.12.{year}",
}
}
}
)
return {
"bool": {
"should": should,
"minimum_should_match": 1,
}
}
response = await elasticsearch.search(
index=ESIndex.PROJECTS_BUND_TOPICS,
body=body,
cluster=ESCluster.INSE
)
buckets = response.get("aggregations", {}).get("project_ids", {}).get("buckets", [])
return [bucket["key"] for bucket in buckets]
async def search_bund_projects_with_filters(
executing_agency: Optional[List[str]] = None,
institution_ids: Optional[List[int]] = None,
institution_ids: Optional[List[str]] = None,
funding_arts: Optional[List[str]] = None, # actually EuCordisProjectType, but treated as strings
title: Optional[str] = None,
keywords: Optional[List[str]] = None,
@@ -753,7 +688,7 @@ async def search_bund_projects_with_filters(
Main search function for BUND projects, analogous to the DFG search.
"""
from_ = page * size
#institution_ids = ["I62318514"]
# Fields we need to build BundProject objects (validation aliases match these exact names)
source_fields = [
"Förderkennzeichen",
@@ -784,7 +719,6 @@ async def search_bund_projects_with_filters(
"datum",
"neu",
"Laufzeit",
# Also include funding_start/end for sorting and filtering (they may also be needed)
"funding_start",
"funding_end",
]
@@ -797,13 +731,12 @@ async def search_bund_projects_with_filters(
}
must_clauses: List[Dict[str, Any]] = []
# === Primary search clause: fuzzy(topic_ids) vs keyword phrase search ===
if fuzzy and topic_ids:
matching_project_ids = await get_bund_project_ids_by_topic_ids(topic_ids=topic_ids)
if not matching_project_ids:
return BundProjectPage(elements=[], total=0, page=page, size=size)
must_clauses.append({"terms": {"Förderkennzeichen.keyword": matching_project_ids}})
must_clauses.append({"terms": {"_id": matching_project_ids}})
elif keywords:
keyword_clauses = []
@@ -813,7 +746,7 @@ async def search_bund_projects_with_filters(
keyword_clauses.append({
"multi_match": {
"query": kw,
"fields": ["Verbundprojekt"], # searching title/acronym field
"fields": ["Verbundprojekt","Thema"], # searching title/acronym field
"type": "phrase_prefix",
"max_expansions": 100,
}
@@ -826,21 +759,38 @@ async def search_bund_projects_with_filters(
matching_filter_ids = await get_bund_project_ids_by_topic_ids(topic_ids=filter_topic_ids)
if not matching_filter_ids:
return BundProjectPage(elements=[], total=0, page=page, size=size)
must_clauses.append({"terms": {"Förderkennzeichen.keyword": matching_filter_ids}})
must_clauses.append({"terms": {"_id": matching_filter_ids}})
if title:
must_clauses.append({
"multi_match": {
"query": title,
"fields": ["Verbundprojekt"],
"fields": ["Verbundprojekt","Thema"], # searching title field
"type": "phrase_prefix",
"max_expansions": 100,
}
})
# print(f"Executing BUND project search with institution_ids: {institution_ids}, executing_agency: {executing_agency}, funding_arts: {funding_arts}, countries: {countries}, funding_start_years: {funding_start_years}, funding_end_years: {funding_end_years}")
if executing_agency:
must_clauses.append({"terms": {"Ausführende Stelle.keyword": executing_agency}})
# institution_names = await institutions_search_client.get_institution_aliases_by_oa_institution_id(executing_agency[0].split("/")[-1])
# institution_page = await institutions_search_client.get_institutions_by_alias(alias=institution_names[0])
# print(f"Retrieved institution names for IDs {executing_agency}: {institution_names}")
# print(f"Retrieved institution page for names {institution_names}: {institution_page}")
# if not institution_names:
# return BundProjectPage(elements=[], total=0, page=page, size=size)
must_clauses.append({
"bool": {
"should": [
clause
for name in executing_agency
for clause in (
{"match": {"Zuwendungsempfänger": {"query": name, "operator": "and"}}},
{"match": {"Ausführende Stelle": {"query": name, "operator": "and"}}},
)
],
"minimum_should_match": 1,
}
})
if funding_arts:
must_clauses.append({"terms": {"Förderart.keyword": funding_arts}})
@@ -848,10 +798,10 @@ async def search_bund_projects_with_filters(
must_clauses.append({"terms": {"Staat.keyword": countries}})
if funding_start_years:
must_clauses.append({"terms": {"funding_start": funding_start_years}})
must_clauses.append(_bund_year_filters("funding_start", funding_start_years))
if funding_end_years:
must_clauses.append({"terms": {"funding_end": funding_end_years}})
must_clauses.append(_bund_year_filters("funding_end", funding_end_years))
body["query"] = {"bool": {"must": must_clauses}} if must_clauses else {"match_all": {}}