diff --git a/service/src/main/inse/model.py b/service/src/main/inse/model.py index a417cb81..5fa44433 100644 --- a/service/src/main/inse/model.py +++ b/service/src/main/inse/model.py @@ -60,7 +60,7 @@ class ESIndex(StrEnum): ORG_PATENTS = "org_patents" PROJECTS_BUND_V1 = "projects_bund_v1" - PROJECTS_DFG_ALL = "projects_dfg_all" + PROJECTS_DFG_ALL = "dfg_projects_2.0" CORDIS_EU_ORGANIZATIONS_WILDCARD = "cordis_eu_*_organizations" CORDIS_EU_PROJECTS_WILDCARD = "cordis_eu_*_projects" @@ -68,6 +68,7 @@ class ESIndex(StrEnum): CORDIS_EU_FP7_OGANIZATIONS = "cordis_eu_fp7_organizations" # FP7 CORDIS_EU_FP7_ORGANIZATIONS = "cordis_eu_fp7_organizations" + CORDIS_EU_FP7_PROJECTS = "cordis_eu_fp7_projects" CORDIS_EU_FP7_PUBLICATIONS = "cordis_eu_fp7_publications" CORDIS_EU_FP7_PROJECT_IPRS = "cordis_eu_fp7_project_iprs" CORDIS_EU_FP7_REPORT_SUMMARIES = "cordis_eu_fp7_report_summaries" @@ -78,6 +79,7 @@ class ESIndex(StrEnum): # Horizon 2020 CORDIS_EU_HORIZON_2020_ORGANIZATIONS = "cordis_eu_horizon_2020_organizations" + CORDIS_EU_HORIZON_2020_PROJECTS = "cordis_eu_horizon_2020_projects" CORDIS_EU_HORIZON_2020_PUBLICATIONS = "cordis_eu_horizon_2020_publications" CORDIS_EU_HORIZON_2020_DELIVERABLES = "cordis_eu_horizon_2020_deliverables" CORDIS_EU_HORIZON_2020_REPORT_SUMMARIES = "cordis_eu_horizon_2020_report_summaries" @@ -89,6 +91,7 @@ class ESIndex(StrEnum): # Horizon Europe CORDIS_EU_HORIZON_EUROPE_ORGANIZATIONS = "cordis_eu_horizon_europe_organizations" + CORDIS_EU_HORIZON_EUROPE_PROJECTS = "cordis_eu_horizon_europe_projects" CORDIS_EU_HORIZON_EUROPE_PUBLICATIONS = "cordis_eu_horizon_europe_publications" CORDIS_EU_HORIZON_EUROPE_DELIVERABLES = "cordis_eu_horizon_europe_deliverables" CORDIS_EU_HORIZON_EUROPE_REPORT_SUMMARIES = ( diff --git a/service/src/main/inse/models/dfg_project_details.py b/service/src/main/inse/models/dfg_project_details.py index 96ee5dc9..cbf909cb 100644 --- a/service/src/main/inse/models/dfg_project_details.py +++ b/service/src/main/inse/models/dfg_project_details.py @@ -93,6 +93,23 @@ class DfgProjectDetails(BaseModel): associated_persons: Set[str] = set() institutions: Set[str] = set() + # --- Collect all persons and institutions from nested persons array --- + persons_list = source.get("persons", []) + if isinstance(persons_list, list): + for p in persons_list: + if isinstance(p, dict): + full_name = p.get("full_name") + if full_name: + associated_persons.add(full_name.strip()) + + aff_de = p.get("affiliation_de") + if aff_de: + institutions.add(aff_de.strip()) + + aff_en = p.get("affiliation_en") + if aff_en: + institutions.add(aff_en.strip()) + # --- Collect all persons from every person-related field --- # Applicant fields @@ -229,22 +246,23 @@ class DfgProjectDetails(BaseModel): ]: if field in auslaend and auslaend[field]: institutions.add(auslaend[field].strip()) + return cls( id=hit["_id"], - link=source.get("link"), - title=source.get("title"), - description=source.get("description"), - project_identifier=source.get("Projektkennung"), - funding_start_date=source.get("funding_start"), - funding_end_date=source.get("funding_end"), - dfg_procedure=source.get("DFG-Verfahren"), - subject_area=source.get("Fachliche Zuordnung"), - funding_type=source.get("Förderung"), - international_reference=source.get("Internationaler Bezug"), + link=source.get("link") or source.get("page_url"), + title=source.get("title") or source.get("title_de") or source.get("title_en"), + description=source.get("description") or source.get("abstract_de") or source.get("abstract_en"), + project_identifier=source.get("Projektkennung") or source.get("project_identifier_de") or source.get("project_identifier_en"), + funding_start_date=str(source.get("funding_start")) if source.get("funding_start") is not None else None, + funding_end_date=str(source.get("funding_end")) if source.get("funding_end") is not None else None, + dfg_procedure=source.get("DFG-Verfahren") or source.get("template_type"), + subject_area=source.get("Fachliche Zuordnung") or source.get("subject_area_de") or source.get("subject_area_en"), + funding_type=source.get("Förderung") or source.get("funding_type_sort"), + international_reference=source.get("Internationaler Bezug") or source.get("international_relation_de") or source.get("international_relation_en"), website=source.get("Webseite"), associated_persons=sorted(associated_persons), # sorted for consistency institutions=sorted(institutions), - subproject_of=source.get("Teilprojekt zu"), + subproject_of=source.get("Teilprojekt zu") or source.get("parent_project_id"), ) diff --git a/service/src/main/inse/projects/model.py b/service/src/main/inse/projects/model.py index c95ada07..3e900e13 100644 --- a/service/src/main/inse/projects/model.py +++ b/service/src/main/inse/projects/model.py @@ -113,14 +113,26 @@ class DfgProjectSummary(BaseModel): source = hit.get("_source", {}) fields = hit.get("fields", {}) + # Extract names from nested persons array + names = [] + persons = source.get("persons", []) + if isinstance(persons, list): + for p in persons: + if isinstance(p, dict): + full_name = p.get("full_name") + if full_name: + names.append(full_name.strip()) + if not names: + names = fields.get("all_found_names", []) + return cls( id=hit.get("_id", ""), - link=source.get("link"), - title=source.get("title"), - professional_classification=source.get("Fachliche Zuordnung"), - funding_start=source.get("funding_start"), - funding_end=source.get("funding_end"), - names=fields.get("all_found_names", []), + link=source.get("link") or source.get("page_url"), + title=source.get("title") or source.get("title_de") or source.get("title_en"), + professional_classification=source.get("Fachliche Zuordnung") or source.get("subject_area_de") or source.get("subject_area_en"), + funding_start=str(source.get("funding_start")) if source.get("funding_start") is not None else None, + funding_end=str(source.get("funding_end")) if source.get("funding_end") is not None else None, + names=names, ) @@ -260,9 +272,9 @@ class FundingProject(BaseModel): @classmethod def from_dfg_projects_hit(cls, hit: dict) -> "FundingProject": - if hit.get("_index") != "projects_dfg_all": + if hit.get("_index") != ESIndex.PROJECTS_DFG_ALL.value: raise ValueError( - "The provided hit does not belong to the 'projects_dfg_all' index." + f"The provided hit does not belong to the '{ESIndex.PROJECTS_DFG_ALL.value}' index." ) source = hit.get("_source", {}) diff --git a/service/src/main/inse/projects/projects_search_client.py b/service/src/main/inse/projects/projects_search_client.py index 22acb5bd..f169786e 100644 --- a/service/src/main/inse/projects/projects_search_client.py +++ b/service/src/main/inse/projects/projects_search_client.py @@ -602,65 +602,42 @@ async def search_dfg_projects( """ from_ = page * size - painless_fields_string = ", ".join([f"'{f}.keyword'" for f in DFG_PERSON_FIELDS]) - body: Dict[str, Any] = { "size": size, "from": from_, "_source": [ "id", "link", + "page_url", "title", + "title_de", + "title_en", "funding_start", "funding_end", "Fachliche Zuordnung", + "subject_area_de", + "subject_area_en", + "persons", ], "query": {}, - "script_fields": { - "all_found_names": { - "script": { - "lang": "painless", - "source": ( - f"def names = new HashSet(); " - f"def fields = [{painless_fields_string}]; " - f"for (field in fields) {{ " - f" if (doc.containsKey(field) && !doc[field].empty) {{ " - f" for (value in doc[field]) {{ " - f" if (value.contains(';')) {{ " - f" def splitNames = value.splitOnToken(';'); " - f" for (sn in splitNames) {{ names.add(sn.trim()); }} " - f" }} else {{ " - f" names.add(value.trim()); " - f" }} " - f" }} " - f" }} " - f"}} " - f"return new ArrayList(names);" - ), - } - } - }, } must_clauses: List[Dict[str, Any]] = [] - filter_clauses: List[Dict[str, Any]] = [] if topic is not None and topic.strip() != "": must_clauses.append( { - "match_phrase_prefix": { - "title": { - "query": topic, - "max_expansions": 100, - } + "multi_match": { + "query": topic, + "fields": ["title", "title_de", "title_en"], + "type": "phrase_prefix", + "max_expansions": 100, } } ) if names: name_should_clauses = [] - script_conditions = [] - for full_name in names: name_parts = full_name.strip().split() if not name_parts: @@ -668,54 +645,33 @@ async def search_dfg_projects( match_musts = [] for part in name_parts: - match_musts.append( - {"multi_match": {"query": part, "fields": DFG_PERSON_FIELDS}} - ) + match_musts.append({ + "match": { + "persons.full_name": { + "query": part, + "operator": "and" + } + } + }) - name_should_clauses.append({"bool": {"must": match_musts}}) - - script_parts = [ - f"singlePerson.contains('{part.lower()}')" for part in name_parts - ] - script_conditions.append(f"({' && '.join(script_parts)})") + name_should_clauses.append({ + "nested": { + "path": "persons", + "query": { + "bool": { + "must": match_musts + } + } + } + }) if name_should_clauses: must_clauses.append( {"bool": {"should": name_should_clauses, "minimum_should_match": 1}} ) - combined_script_condition = " || ".join(script_conditions) - painless_source = ( - f"def fields = [{painless_fields_string}]; " - f"for (field in fields) {{ " - f" if (doc.containsKey(field) && !doc[field].empty) {{ " - f" for (value in doc[field]) {{ " - f" if (value.contains(';')) {{ " - f" def splitValues = value.splitOnToken(';'); " - f" for (singlePersonRaw in splitValues) {{ " - f" def singlePerson = singlePersonRaw.toLowerCase(); " - f" if ({combined_script_condition}) {{ return true; }} " - f" }} " - f" }} else {{ " - f" def singlePerson = value.toLowerCase(); " - f" if ({combined_script_condition}) {{ return true; }} " - f" }} " - f" }} " - f" }} " - f"}} " - f"return false;" - ) - - filter_clauses.append( - {"script": {"script": {"lang": "painless", "source": painless_source}}} - ) - - if must_clauses or filter_clauses: - body["query"] = {"bool": {}} - if must_clauses: - body["query"]["bool"]["must"] = must_clauses - if filter_clauses: - body["query"]["bool"]["filter"] = filter_clauses + if must_clauses: + body["query"] = {"bool": {"must": must_clauses}} else: body["query"] = {"match_all": {}} @@ -727,25 +683,25 @@ async def search_dfg_projects( if sort_by in ["topic", "title"]: logger.debug(f"Sorting DFG projects by title with direction {sort_direction}") - sort_config = {"title.keyword": {"order": sort_direction}} + sort_config = {"title_de.keyword": {"order": sort_direction}} elif sort_by == "funding_start": logger.debug( f"Sorting DFG projects by funding_start with direction {sort_direction}" ) - sort_config = {"funding_start.keyword": {"order": sort_direction}} + sort_config = {"funding_start": {"order": sort_direction}} elif sort_by == "funding_end": logger.debug( f"Sorting DFG projects by funding_end with direction {sort_direction}" ) - sort_config = {"funding_end.keyword": {"order": sort_direction}} + sort_config = {"funding_end": {"order": sort_direction}} elif sort_by == "professional_classification": logger.debug( f"Sorting DFG projects by Fachliche Zuordnung with direction {sort_direction}" ) - sort_config = {"Fachliche Zuordnung.keyword": {"order": sort_direction}} + sort_config = {"subject_area_de.keyword": {"order": sort_direction}} else: logger.debug(f"Sorting DFG projects by id with direction {sort_direction}") - sort_config = {"id.keyword": {"order": sort_direction}} + sort_config = {"id": {"order": sort_direction}} body["sort"] = [sort_config] diff --git a/service/src/main/inse/projects/test_projects_dfg.py b/service/src/main/inse/projects/test_projects_dfg.py new file mode 100644 index 00000000..c5ab8109 --- /dev/null +++ b/service/src/main/inse/projects/test_projects_dfg.py @@ -0,0 +1,28 @@ +import pytest +from inse.projects import projects_search_client + +@pytest.mark.anyio +async def test_search_dfg_projects(): + # Test search with name + res = await projects_search_client.search_dfg_projects( + page=0, + size=5, + names=["Wolfgang Ahrens"], + ) + assert res is not None + assert res.total > 0 + assert len(res.elements) > 0 + for element in res.elements: + assert element.id is not None + assert any("Wolfgang Ahrens" in name for name in element.names) + +@pytest.mark.anyio +async def test_get_dfg_project_by_id(): + # Get by ID of the project we found or a known one + known_id = "164008977" + project = await projects_search_client.get_dfg_project_by_id(known_id) + assert project is not None + assert project.id == known_id + assert project.title is not None + assert any("Wolfgang Ahrens" in person for person in project.associated_persons) + assert any("Leibniz" in inst for inst in project.institutions)